Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Im trying to do the calculations using NSNumbers and keep track of the numbers that the user inputs..

The problem is that my app is not doing any calculations now after updated my code to append . the decimal value to value whenever the user press the dot button. Which can be found Append or Add the decimal point functionality in calculator.

I have seen that the proper way to keep track of the inputs from the user and do the calculation is using the NSNumber but as I'm really new to Objective-c I have been struggling understanding the use and implementing it.

So, hopefully someone could walk me through to find the proper solution.

This is the header...

int Method;
float SelectNumber;
float RunningTotal;
bool DecimalActived;

@interface ViewController : UIViewController{
    IBOutlet UILabel *Screen;
}

-(IBAction)Number9:(UIButton *)sender;
-(IBAction)Dot:(UIButton *)sender;  

@end

This is the the implementation file..

-(IBAction)Number9:(UIButton *)sender{

    [self appendDigit:@"9"];

}

- (IBAction)Dot:(UIButton *)sender {

    NSString *currentText = Screen.text;
    if ([currentText rangeOfString:@"." options:NSBackwardsSearch].length == 0) {
        [self appendDigit:@"."];
    }

}

- (void)appendDigit:(NSString *)digit {
    // handle two special cases: append to only zero means just replace
    // but append decimal point to zero is a regular append
    if ([self->Screen.text isEqualToString:@"0"] && ![digit isEqual:@"."]) {
        self->Screen.text = digit;
    } else {
        self->Screen.text = [Screen.text stringByAppendingString:digit];
    }
}

- (IBAction)Percent:(UIButton *)sender {

    [self MySwitch];

    Method = 5;
    SelectNumber = 0;
    DecimalActived = FALSE;
    Screen.text = [NSString stringWithFormat:@"%.2g", RunningTotal];

}


- (IBAction)PositiveOrNegative:(UIButton *)sender {

    [self MySwitch];

    Method = 6;
    SelectNumber = 0;
    DecimalActived = FALSE;
    Screen.text = [NSString stringWithFormat:@"%g", RunningTotal];

}


-(IBAction)Equals:(UIButton *)sender{

    [self MySwitch];

    Method = 0;
    SelectNumber = 0;
    DecimalActived = FALSE;
    Screen.text = [NSString stringWithFormat:@"%g", RunningTotal];

}


-(IBAction)AllClear:(UIButton *)sender{

    Method = 0;
    RunningTotal = 0;
    SelectNumber = 0;

    Screen.text = [NSString stringWithFormat:@"0"];

}

- (double) MySwitch {

     NSNumberFormatter SelectNumber = [[NSNumberFormatter alloc] init];
     [SelectNumber setNumberStyle:NSNumberFormatterDecimalStyle];
     NSNumber RunningTotal = [SelectNumber numberFromString:self->Screen.text];

     if (RunningTotal == 0) {
         RunningTotal = SelectNumber;
     } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            case 6:
                if(RunningTotal > 0){
                    RunningTotal = - RunningTotal;
                } else{
                    RunningTotal = + RunningTotal;
                }
                break;
            default:
                break;
        }
     }

    return RunningTotal;
}

If you guys have any question or need more information regarding my program please feel free to ask and I will provide as much information as possible or answer any questions that you guys may have.. :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
119 views
Welcome To Ask or Share your Answers For Others

1 Answer

The header should look more like this:

// this uses more conventional (lowercase) property names
// and removes a couple that looked extraneous
@interface ViewController : UIViewController
@property(strong,nonatomic) IBOutlet UILabel *screen;
@property(assign,nonatomic) NSInteger method;
@property(strong,nonatomic) NSNumber *runningTotal;
@end

As the user presses digits, decimal, minus sign, append to the screen label as you have it. When the user presses an operation button, record an integer for the operation and record the current (NSNumber) value of the screen label. To do a computation...

- (void)doComputation {

    float screenF = [[self screenValue] floatValue];
    float runningF = [self.runningTotal floatValue];
    float result;

    switch (self.method) {
        case 1:
            result = runningF * screenF;
            break;
        case 2:
            result = (screenF == 0.0)? 0.0 : runningF / screenF;
            break;
        case 3:
            result = runningF - screenF;
            break;
        case 4:
            result = runningF + screenF;
            break;
        default:
            break;
    }
    self.screen.text = [NSString stringWithFormat:@"%0.8f", result];
    self.runningTotal = [NSNumber numberWithFloat:result];
}

(screen value, as you have it...)

- (NSNumber *)screenValue {
    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    return [f numberFromString:self.screen.text];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...