首页 > 代码库 > 9月17日 设计一个计算器

9月17日 设计一个计算器

//

//  ViewController.m

//  计算器

//

//  Created by apple on 14-9-17.

//  Copyright (c) 2014年 apple. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

{

    NSString *op1,*op2;

    char op;

}

@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

@end

 

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    op1 = @"";

    op2 = @"";

    // Do any additional setup after loading the view, typically from a nib.

}

- (IBAction)equal:(id)sender {

 

    //NSString转int整形数值

    int a1 = [op1 intValue];

    int a2 = [op2 intValue];

    int result = 0;

    if (op == ‘+‘) {

 

        result = a1 + a2;

 

    } else if ( op ==‘-‘){

        

        result = a1-a2;

    

    }else if(op==‘*‘){

    

        result = a1*a2;

   

    }else if(op==‘/‘){

    

        result=a1/a2;

    

    }

    

    NSLog(@"%d", result);

    NSString *resultText = [NSString stringWithFormat:@"%d", result];

    self.resultLabel.text = resultText;

}

- (IBAction)divide:(id)sender {

    

        op= ‘/‘;

}

- (IBAction)multiply:(id)sender {

    

        op = ‘*‘;

}

- (IBAction)subtracting:(id)sender {

    

        op = ‘-‘;

}

- (IBAction)add:(id)sender {

    

        op = ‘+‘;

}

- (IBAction)num0:(id)sender {

    

    

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,0];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,0];

        

    }

 

}

- (IBAction)num9:(id)sender {

    

    

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,9];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,9];

        

    }

 

}

- (IBAction)num8:(id)sender {

    

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,8];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,8];

        

    }

 

}

- (IBAction)num7:(id)sender {

 

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,7];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,7];

        

    }

 

}

- (IBAction)num6:(id)sender {

    

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,6];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,6];

        

    }

 

}

- (IBAction)num5:(id)sender {

    

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,5];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,5];

        

    }

 

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (IBAction)num4:(id)sender {

    

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,4];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,4];

        

    }

 

}

- (IBAction)num3:(id)sender {

 

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,3];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,3];

        

    }

}

- (IBAction)num2:(id)sender {

    

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,2];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,2];

        

    }

}

- (IBAction)num1:(id)sender {

    

    if (op != ‘+‘ && op != ‘-‘

        && op != ‘*‘ && op != ‘/‘) {

        //1231

        op1 = [NSString stringWithFormat:@"%@%d", op1,1];

    }else

    {

        op2 = [NSString stringWithFormat:@"%@%d", op2,1];

}

}

@end

9月17日 设计一个计算器