oc带顺序连算不带优先级计算器(作为备忘,完成度:95%)

#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zwarwolf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值