计算器(可累加,可清零)

#import "ViewController.h"

 

@interface ViewController ()

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

 

 

@end

    NSString *op1,*op2,*result;

    

    char op;

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog (@"hello world");

    op1=@"";

    op2=@"";

    result=@"";

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

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

- (IBAction)butten1:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/') {

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

        self.resultLabel.text = op1;

    }else{

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

        self.resultLabel.text = op2;

    }

    

}

- (IBAction)butter2:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

        {

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

            self.resultLabel.text = op1;

    }else

        {

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

            self.resultLabel.text = op2;

    }

    

}

- (IBAction)butter3:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

    {

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

        self.resultLabel.text = op1;

    }else

    {

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

        self.resultLabel.text = op2;

    }

}

- (IBAction)butter4:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

    {

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

        self.resultLabel.text = op1;

    }else

    {

        op2=[NSString stringWithFormat:@"%@%d",op2,4];self.resultLabel.text = op2;

    }

}

 

- (IBAction)butter5:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

    {

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

        self.resultLabel.text = op1;

    }else

    {

        op2=[NSString stringWithFormat:@"%@%d",op2,5];self.resultLabel.text = op2;

    }

}

- (IBAction)butter6:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

    {

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

        self.resultLabel.text = op1;

    }else

    {

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

        self.resultLabel.text = op2;

    }

}

- (IBAction)butter7:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

    {

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

        self.resultLabel.text = op1;

    }else

    {

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

        self.resultLabel.text = op2;

    }

}

- (IBAction)butter8:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

    {

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

        self.resultLabel.text = op1;

    }else

    {

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

        self.resultLabel.text = op2;

    }

}

- (IBAction)butter9:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

    {

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

        self.resultLabel.text = op1;

    }else

    {

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

        self.resultLabel.text = op2;

    }

}

- (IBAction)butter0:(id)sender {

    if (op!='+'&&op!='-'&&op!='*'&&op!='/')

    {

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

        self.resultLabel.text = op1;

    }else

    {

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

        self.resultLabel.text = op2;

    }

    

}

- (IBAction)jian:(id)sender {

    op='-';

}

- (IBAction)cheng:(id)sender {

    op='*';

}

- (IBAction)chu:(id)sender {

    op='/';

}

 

- (IBAction)plus:(id)sender {

    op='+';

}

- (IBAction)dengyu:(id)sender {

    if (op == '+') {

        //NSString转int整形数值

        int a1 = [op1 intValue];

        int a2 = [op2 intValue];

        int sum = a1 + a2;

        NSLog(@"%d", sum);

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

        self.resultLabel.text = result;

    }

    else if (op=='-')

    {

        int a1 = [op1 intValue];

        int a2 = [op2 intValue];

        int sum = a1 - a2;

        NSLog(@"%d", sum);

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

        self.resultLabel.text = result;

    }

    else if (op=='*'){

        int a1 = [op1 intValue];

        int a2 = [op2 intValue];

        int sum = a1 * a2;

        NSLog(@"%d", sum);

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

        self.resultLabel.text = result;

    }

    else{

        float a1 = [op1 floatValue];

        float a2 = [op2 floatValue];

        float sum = a1 / a2;

        NSLog(@"%f", sum);

        result = [NSString stringWithFormat:@"%f", sum];

        self.resultLabel.text = result;

        

    }

    if (op!='+'&&op!='-'&&op!='*'&&op!='/') {

        op1=@"";

        op2=@"";

        result=@"";

    }else

    {

    op1=result;

        op2=@"";

    }

    

}

- (IBAction)qingling:(id)sender {

    op1=@"";

    op2=@"";

    op = '\0';

    result=@"";

    self.resultLabel.text = @"0";

}

@end

posted on 2014-09-17 20:54  陈丰波  阅读(855)  评论(0编辑  收藏  举报