简易计算器Demo

计算器的功能实现的并不是很完善,仅供参考

#import "MainViewController.h"
<pre name="code" class="plain">#import "MainViewController.h"

@interface MainViewController ()
@property (nonatomic, retain) UILabel *label;

@property (nonatomic, assign) int a;
@property (nonatomic, assign) int b;
@property (nonatomic, assign) int total;
@property (nonatomic, retain) NSString *total1;
@property (nonatomic, retain) NSString *total2;
@property (nonatomic, retain) NSString *total3;
@property (nonatomic, retain) NSString *total4;
@property (nonatomic, retain) NSMutableString *string;
@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.string = [NSMutableString stringWithString:@""];
    }
    return self;
}

- (void)viewDidLoad

{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
 // 创建底层label
    _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 40, 200, 40)];
    
    _label.userInteractionEnabled = NO;
    
    [self.view addSubview:_label];
    
    [_label release];
    
    NSArray * array = [NSArray arrayWithObjects:@"1",@"2",@"3",@"+",@"4",@"5",@"6",@"-",@"7",@"8",@"9",@"*",@"0",@".",@"=",@"/", nil];
// 创建button
    int a = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

            button.frame = CGRectMake(50 + j * 50, 150 + i * 50, 40, 40);
            
            button.backgroundColor = [UIColor yellowColor];
            
            button.tag = 10000 + i * 4 + j;
// 将数组中的数据传到button上显示
            [button setTitle:[array objectAtIndex:a] forState:UIControlStateNormal];
            a++;

            [self.view addSubview:button];
            
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            //NSLog(@"%d",button.tag);
        }
    }
// 创建清除button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    button.frame = CGRectMake(60, 380, 170, 50);
    
    [button setTitle:@"清空" forState:UIControlStateNormal];
    
    button.backgroundColor = [UIColor yellowColor];
    
    [button addTarget:self action:@selector(clearButton:) forControlEvents:UIControlEventTouchUpInside];
    
    button.tag = 1000;
    
    [self.view addSubview:button];
    
}
// 相应点击button事件
- (void)buttonClick:(UIButton *)button
{
    self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:button.titleLabel.text]];
    self.label.text = self.string;
    if ( [button.titleLabel.text isEqual: @"="]) {
        
        
        
        NSRange x = [self.string rangeOfString:@"+"];
        if (x.location != NSNotFound) {
            // 加号后面的
            _a = [[self.string substringFromIndex:x.location + 1] intValue];
            // 加号之前的
            _b = [[self.string substringToIndex:x.location] intValue];
            _total = _a + _b;
            _total1 = [NSString stringWithFormat:@"%d",_total];
            self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:_total1 ]];
            self.label.text = self.string;
        }
        NSRange jian = [self.string rangeOfString:@"-"];
 // 减法
        if (jian.location != NSNotFound){
            // 减号之前的
            _a = [[self.string substringFromIndex:jian.location + 1] intValue];
            _b = [[self.string substringToIndex:jian.location] intValue];
            _total = _b - _a;
            _total2 = [NSString stringWithFormat:@"%d",_total];
            self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:_total2]];
            self.label.text = self.string;
        }
    // 乘法
    NSRange cheng = [self.string rangeOfString:@"*"];
        if (cheng.location != NSNotFound) {
            // 乘号后面的
            _a = [[self.string substringFromIndex:cheng.location + 1] intValue];
            // 乘号之前的
            _b = [[self.string substringToIndex:cheng.location] intValue];
            _total = _b * _a;
            _total3 = [NSString stringWithFormat:@"%d",_total];
            self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:_total3]];
            self.label.text = self.string;
        }
    // 除法
    NSRange chu = [self.string rangeOfString:@"/"];
        if (chu.location != NSNotFound) {
            // 除号后面的
            _a = [[self.string substringFromIndex:chu.location + 1] intValue];
            // 除号之前的
            _b = [[self.string substringToIndex:chu.location] intValue];
            _total = _b / _a;
            _total4 = [NSString stringWithFormat:@"%d",_total];
            self.string = [NSMutableString stringWithString:[self.string stringByAppendingString:_total4]];
            self.label.text = self.string;
        }
    }
}

// 相应清除button事件
- (void)clearButton:(UIButton *)button
{
    if (button.tag == 1000) {
        self.string = @" ";
        
        self.label.text = @" ";
    } else {
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值