简单计算器

//
//  ViewController.m
//  Calc
//
//  Created by apple on 14-7-10.
//  Copyright (c) 2014年 dengshiru. All rights reserved.
//


#import "ViewController.h"


@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *textFid;
            


@end
int i;
i = 10;


@implementation ViewController
- (IBAction)click1:(id)sender   //利用if语句判断本次输入的数应在哪个位置
{
    if (calc.op== '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 + 1;
        self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue2];
    }
    else
    {
        calc.opValue1 = calc.opValue1 * 10 +1;
      self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
    }
}


- (IBAction)click2:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
            calc.opValue2 = calc.opValue2 * 10 + 2;
            self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
        }
    else{
        calc.opValue1 = calc.opValue1 * 10 + 2;
        self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
    }
    
    
}
- (IBAction)click3:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 + 3;
        self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
    }
    else {
        calc.opValue1 = calc.opValue1 * 10 + 3;
        self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
    }
    
}
- (IBAction)click4:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 + 4;
        self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
    }
else {
    calc.opValue1 = calc.opValue1 * 10 + 4;
    self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
}


}
- (IBAction)click5:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 + 5;
        self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
    }
else {
    calc.opValue1 = calc.opValue1 * 10 + 5;
    self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
}


}
- (IBAction)click6:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 + 6;
        self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
    }
else {
    calc.opValue1 = calc.opValue1 * 10 + 6;
    self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
}


}
- (IBAction)click7:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 + 7;
        self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
    }
else {
    calc.opValue1 = calc.opValue1 * 10 + 7;
    self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
}


}


- (IBAction)click8:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 + 8;
        self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
    }
    else {
    calc.opValue1 = calc.opValue1 * 10 + 8;
    self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
}


}
- (IBAction)click9:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 + 9;
        self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
    }
else {
    calc.opValue1 = calc.opValue1 * 10 + 9;
    self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
}


}
- (IBAction)click0:(id)sender {
    if (calc.op == '+'
        || calc.op == '-'
        || calc.op == '*'
        || calc.op == '/') {
        
        calc.opValue2 = calc.opValue2 * 10 ;
        self.textFid.text = [NSString stringWithFormat:@"%F",calc.opValue2];
    }
    else {
    calc.opValue1 = calc.opValue1 * 10;
    self.textFid.text = [NSString stringWithFormat:@"%f",calc.opValue1];
}


}
- (IBAction)add:(id)sender {
    calc.op = '+';
}
- (IBAction)cut:(id)sender {
    calc.op = '-';
}
- (IBAction)ride:(id)sender {
    calc.op = '*';
}
- (IBAction)div:(id)sender {
    calc.op = '/';
}
- (IBAction)dian:(id)sender {
    calc.op = '.';
}


- (IBAction)dengyu:(id)sender {
    //结果计算
    float dengyu = [calc work];
    self.textFid.text = [NSString stringWithFormat:@"%f",dengyu];
    //计算完成结果清零
    calc.op = 0;
    calc.opValue1 = 0;
    calc.opValue2 = 0;
}
- (IBAction)clear:(id)sender {
    //结果清除、清零
    self.textFid.text = [NSString stringWithFormat:@""];
    calc.op = 0;
    calc.opValue1 = 0;
    calc.opValue2 = 0;
}
            
- (void)viewDidLoad {
    [super viewDidLoad];
    //在程序运行之前生成计算器
    calc = [[Caculator alloc] init];
    // 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值