首页 > 代码库 > OC实现简单的计算器

OC实现简单的计算器

<p>//注:只能实现简单的加减乘除,不可以连续运算</p><p>#define kButton_Color [UIColor cyanColor]//定义键的背景颜色宏,便于更改颜色</p>@interface YJAppDelegate ()
{
    UIView *_containter;
    UIButton *_numberButton;
    UILabel *_label;
    CGFloat num1,operators ,result,digital;
    NSMutableString *_string;
    NSString *_symbolText;
}
@end<pre name="code" class="objc">_containter = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    _containter.backgroundColor = [UIColor grayColor];
    [self.window addSubview:_containter];
    [_containter release];
    //设置数字
    NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
    int n = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            _numberButton = [UIButton buttonWithType:UIButtonTypeSystem];
            _numberButton.frame = CGRectMake( (j * 80), 180 + (i * 80), 78, 78);
            [_numberButton setTitle:[array objectAtIndex:n++] forState:UIControlStateNormal];
            _numberButton.backgroundColor = kButton_Color;
            _numberButton.titleLabel.font = [UIFont systemFontOfSize:22];
            _numberButton.layer.cornerRadius = 30;
            [_numberButton addTarget:self action:@selector(number:) forControlEvents:UIControlEventTouchUpInside];
            [_containter addSubview:_numberButton];
        }
    }
    //设置返回键
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(240, 180, 78, 78);
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    backButton.backgroundColor = kButton_Color;
    backButton.titleLabel.font = [UIFont systemFontOfSize:22];
    backButton.layer.cornerRadius = 30;
    [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside
     ];
    [_containter addSubview:backButton];
    //设置符号键
    NSArray *array2 = [NSArray arrayWithObjects:@"+",@"-",@"*",@"/", nil];
    for (int i = 0; i < 4; i++) {
        UIButton *symbolButton = [UIButton buttonWithType:UIButtonTypeSystem];
        symbolButton.frame = CGRectMake(240, 260+ (i * 80), 78, 78);
        [symbolButton setTitle:[array2 objectAtIndex:i] forState:UIControlStateNormal];
        symbolButton.backgroundColor = kButton_Color;
        symbolButton.titleLabel.font = [UIFont systemFontOfSize:22];
        symbolButton.layer.cornerRadius = 30;
        [symbolButton addTarget:self action:@selector(symbol:) forControlEvents:UIControlEventTouchUpInside];
        [_containter addSubview:symbolButton];
    }
    //设置0和.
    NSArray *array3 = [NSArray arrayWithObjects:@"0",@".", nil];
    for (int i = 0; i < 2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake( 80 + (i * 80), 420, 78, 78);
        button.backgroundColor = kButton_Color;
        button.titleLabel.font = [UIFont systemFontOfSize:22];
        button.layer.cornerRadius = 30;
        [button setTitle:[array3 objectAtIndex:i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        [_containter addSubview:button];
    }
    //设置归零键
    UIButton *acButton = [UIButton buttonWithType:UIButtonTypeSystem];
    acButton.frame = CGRectMake(0, 420, 78, 78);
    acButton.backgroundColor = kButton_Color;
    acButton.layer.cornerRadius = 30;
    acButton.titleLabel.font = [UIFont systemFontOfSize:22];
    [acButton setTitle:@"AC" forState:UIControlStateNormal];
    [acButton addTarget:self action:@selector(ac:) forControlEvents:UIControlEventTouchUpInside];
    [_containter addSubview:acButton];
    //设置等号键
    UIButton *equalButton = [UIButton buttonWithType:UIButtonTypeSystem];
    equalButton.frame = CGRectMake(0, 500, 240, 68);
    equalButton.backgroundColor = kButton_Color;
    equalButton.titleLabel.font = [UIFont systemFontOfSize:22];
    equalButton.layer.cornerRadius = 30;
    [equalButton setTitle:@"=" forState:UIControlStateNormal];
    [equalButton addTarget:self action:@selector(equal:) forControlEvents:UIControlEventTouchUpInside];
    [_containter addSubview:equalButton];
    //设置显示窗口
    _label = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 300, 60)];
    _label.backgroundColor = [UIColor whiteColor];
    _label.textColor = [UIColor blueColor];
    _label.layer.cornerRadius = 3;
    
    _label.textAlignment = NSTextAlignmentLeft;
    _label.font = [UIFont systemFontOfSize:20];
    [_containter addSubview:_label];
    [_label release];
    //设置显示的文字为字符串
    _string = [[NSMutableString alloc ]init];

//配置数字键触发
- (void)number:(UIButton *)number
{
    [_string appendString:[number currentTitle]];
    _label.text = [NSString stringWithString:_string];
    num1 = [_label.text doubleValue];

}
//配置0和.的触发
- (void)button:(UIButton *)button
{
    [_string appendString:[button currentTitle]];
    _label.text = [NSString stringWithString:_string];
    num1 = [_label.text doubleValue];

}
//配置运算符键的触发
- (void)symbol:(UIButton *)symbol
{
    
    [_string setString:@""];
    [_string appendString:[symbol currentTitle]];
    _label.text = [NSString stringWithString:_string];
   
     if ([_string hasPrefix:@"+"]) {
         digital = num1;//存储第一次输入的数字
         operators = 1;//存储输入的运算符
         [_string setString:@""];//输入运算符后将界面清空
    } else if ([_string hasPrefix:@"-"]) {
        digital = num1;
        operators = 2;
//        [_string setString:@""];
    } else if ([_string hasPrefix:@"*"]) {
        digital = num1;
        operators = 3;
        [_string setString:@""];
    } else if ([_string hasSuffix:@"/"]) {
        digital = num1;
        operators = 4;
        [_string setString:@""];
    }
    
}
//配置等号键的触发
- (void)equal:(UIButton *)equal
{
    if (operators == 1) {
        result = digital + [_label.text doubleValue];//[_label.text doubleValue]为第二次输入的数字
        _label.text = [NSString stringWithFormat:@"%g",result];//显示结果
        [_string setString:@""];//将结果清空
    } else if (operators == 2) {
        result = digital - ( -[_label.text doubleValue] );
        _label.text = [NSString stringWithFormat:@"%g",result];
         [_string setString:@""];
    } else if (operators == 3) {
        result = digital * [_label.text doubleValue];
        _label.text = [NSString stringWithFormat:@"%g",result];
         [_string setString:@""];
    }  else if (operators == 4) {
        result = digital / [_label.text doubleValue];
        _label.text = [NSString stringWithFormat:@"%g",result];
         [_string setString:@""];
    }
}
//配置归零键的触发
- (void)ac:(UIButton *)ac
{
    [_string setString:@""];
    result = 0;
    operators = 0;
    _label.text = @"0";
}
//配置返回键的触发
- (void)back:(UIButton *)back
{
   if (![_label.text isEqual:@""])
   {
       [_string deleteCharactersInRange:NSMakeRange([_string length] - 1, 1)];//删除最后一个字符
       _label.text = [NSString stringWithString:_string];//显示删除了的字符串
       num1 = [_label.text floatValue];//将删除了的数字接收
   }

}