码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-17 21:43:52      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   for   sp   cti   log   on   c   

#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

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

标签:io   os   ar   for   sp   cti   log   on   c   

原文地址:http://www.cnblogs.com/a514875560/p/3978025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!