#import "ViewController.h" #import "jsq.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *label; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; jsqq = [[jsq alloc] init]; //初始化对象jsqq } - (IBAction)v:(UIButton *)sender { if(jsqq.op==0) //判断是否已经按了运算符 { jsqq.op1 = jsqq.op1*10 + sender.tag; self.label.text = [NSString stringWithFormat:@"%f",jsqq.op1]; //将浮点数转化为字符串,并显示在label上 } else { jsqq.op2 = jsqq.op2*10 + sender.tag; self.label.text = [NSString stringWithFormat:@"%f",jsqq.op2]; } } - (IBAction)c:(UIButton *)sender { if(sender.tag==11) { self.label.text =@"+";//在label上显示字符“+” jsqq.op = ‘+‘; } else if(sender.tag==12) { self.label.text =@"-"; jsqq.op = ‘-‘; } else if(sender.tag==13) { self.label.text =@"*"; jsqq.op = ‘*‘; } else if(sender.tag==14) { self.label.text =@"/"; jsqq.op = ‘/‘; } else if(sender.tag==15) //“=”键 { float resurt; if(jsqq.op == ‘+‘){ resurt = [jsqq add]; //调用方法,算出结果 } else if(jsqq.op == ‘-‘){ resurt = [jsqq sub]; } else if(jsqq.op == ‘*‘){ resurt = [jsqq mu]; } else if(jsqq.op == ‘/‘){ resurt = [jsqq div]; } self.label.text = [NSString stringWithFormat:@"%f",resurt]; jsqq.op1 = resurt; jsqq.op2 = 0; } else if(sender.tag==16) //清零 “c”键 { jsqq.op = 0; jsqq.op1 = 0; jsqq.op2 = 0; self.label.text =@"0"; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
实现的效果如下:
Xcode 制作简易计算器
时间: 2024-11-09 22:27:06