R
R
random2015-02-11 18:54:16
Objective-C
random, 2015-02-11 18:54:16

Objective-c calculator?

I am writing a calculator on an iPhone (objective - c), I ran into such a problem:
Example: 2+2=4, after I want to enter a number, but it adds this number to the end of the line (I found the case 4xxx, where x is the input numbers), not as a new value.
How can it be implemented?

// Так вставляю цифры(0-9)
-(void)setNumber:(int)num{
    
    if(!isDecimal){
        displayNumber *= 10;
        displayNumber += num;
        mainLabel.text = [NSString stringWithFormat:@"%g",displayNumber];
    }
    else{
        mainLabel.text = [mainLabel.text stringByAppendingString:[NSString stringWithFormat:@"%d", num]];
    }
    displayNumber = [mainLabel.text doubleValue];
}

// При нажатие баттона, цифра добавляется в лабел
- (IBAction)num1:(id)sender {
    [self setNumber:1];
}

// При нажатие на баттон равно
- (IBAction)equals:(id)sender{
    [self operations:operation];
    mainLabel.text = [NSString stringWithFormat:@"%g",resultNumber];
    displayNumber = [mainLabel.text doubleValue];
    resultNumber = 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2015-04-05
@DaskOFF

Mark with a flag that the result is received, by clicking on the number, reset this flag and clear the input line, by clicking on the operation, simply reset the flag and work further by entering 2 operands

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question