#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *res;
@property(nonatomic,assign)double op1;
@property(nonatomic,assign)double op2;
@property(nonatomic,assign)int op;
@property(nonatomic,assign)char sign;
@end
@implementation ViewController
- (IBAction)onClick:(UIButton*)sender {
static BOOL p=NO;//判断是否第二个操作数
static BOOL q=NO;//小数操作
static int i=0;
static double a=0;
static double b=0;
static double c=0;
static double d=0;
double r=0;
switch(sender.tag)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 0:
if(NO==p)
{
if(YES==q)
{
i++;
c=self.op1+(double)sender.tag/pow(10,i);
self.op1=c;
self.res.text=[NSString stringWithFormat:@"%g",self.op1];
}else{
a=a*10+sender.tag;
self.op1=a;
self.res.text=[NSString stringWithFormat:@"%g",self.op1];
}
}else{
if(YES==q)
{
i++;
d=self.op2+(double)sender.tag/pow(10,i);
self.op2=d;
self.res.text=[NSString stringWithFormat:@"%g",self.op2];
}else{
b=b*10+sender.tag;
self.op2=b;
self.res.text=[NSString stringWithFormat:@"%g",self.op2];
}
}
break;
case 10://+
p=YES;
q=NO;
i=0;
//判断上一次的运算符
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
self.sign='+';
break;
case 11://-
p=YES;
q=NO;
i=0;
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
self.sign='-';
break;
case 12://*
p=YES;
q=NO;
i=0;
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
self.sign='*';
break;
case 13:// /
p=YES;
q=NO;
i=0;
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
self.sign='/';
break;
case 14://=
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSString alloc] initWithFormat:@"%g",r];
self.op1=r;
}
self.sign='\0';
self.op2=0;
b=0;
break;
case 15://AC
p=NO;
q=NO;
a=0;
b=0;
c=0;
d=0;
i=0;
self.op1=0;
self.op2=0;
self.sign='\0';
self.res.text=[NSString stringWithFormat:@"0"];
break;
case 16://.
q=YES;
break;
case 17://%
self.op1=self.op1/100;
self.res.text=[NSString stringWithFormat:@"%g",self.op1];
break;
case 18://±
self.op1=-1*self.op1;
self.res.text=[NSString stringWithFormat:@"%g",self.op1];
break;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end