【iOS开发】—— 仿写计算器

所需知识

一、Masonry

二、MVC设计模式

三、中缀转后缀

四、四则运算(加括号)

基本思路

首先在M、V和C的相关内容,然后通过点击按钮并向字符串中添加对应的字符,然后在M中对字符串进行相关数据处理。

按钮布局处理

使用Masony:

    _buttonArray = [[NSArray alloc] initWithObjects:_buttonDelete, _buttonLeft, _buttonRight, _buttonDivide, _buttonSeven, _buttonEight, _buttonNine, _buttonMultiply, _buttonFour, _buttonFive, _buttonSix, _buttonSubtract, _buttonOne, _buttonTwo, _buttonThree, _buttonAdd, _buttonZero, _buttonPoint, _buttonEqual,nil];
    
    _contentArray = [[NSArray alloc] initWithObjects:@"AC", @"(", @")", @"/", @"7", @"8", @"9", @"*", @"4", @"5", @"6", @"-", @"1", @"2", @"3", @"+", @"0", @".", @"=", nil];
    
    for (int i = 0; i < 5; i++) {
        if (i < 4) {
            for (int j = 0; j < 4; j++) {
                UIButton* button = [[UIButton alloc] init];
                button = [_buttonArray objectAtIndex:4 * i + j];
                if (j < 3 && i > 0) {
                    button.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.6];
                } else if (j == 3) {
                    button.backgroundColor = [UIColor orangeColor];
                } else if (i == 0 && j < 3) {
                    button.backgroundColor = [UIColor grayColor];
                }
                button.layer.cornerRadius =  w / 2;
                NSString* string = [_contentArray objectAtIndex:4 * i +j];
                [button setTitle:string forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:40];
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [button mas_makeConstraints:^(MASConstraintMaker *make) {
                                make.bottom.equalTo(self).offset(- H / 9 * (4 - i) - 40);
                                make.left.equalTo(self).offset( (W - 20) / 4 * j + 10);
                                make.height.equalTo(@(w));
                                make.width.equalTo(@(w));
                }];
            }
        } else {
            for (int j = 0; j < 3; j++) {
                UIButton* button = [[UIButton alloc] init];
                button = [_buttonArray objectAtIndex:j + 16];
                button.layer.cornerRadius =  w / 2;
                NSString* string = [_contentArray objectAtIndex:j + 16];
                [button setTitle:string forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:40];
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                if (j == 0) {
                    button.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.6];
                    [button mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.bottom.equalTo(_buttonOne).offset(H / 9);
                        make.left.equalTo(self).offset(10);
                        make.width.equalTo(@(2 * w));
                        make.height.equalTo(@w);
                    }];
                } else {
                    if (j == 1) {
                        button.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.6];
                    } else {
                        button.backgroundColor = [UIColor orangeColor];
                    }
                    [button mas_makeConstraints:^(MASConstraintMaker *make) {
                                    make.bottom.equalTo(_buttonOne).offset(H / 9);
                                    make.left.equalTo(self).offset( (W - 20) / 4 * (j + 1) + 10);
                                    make.height.equalTo(@(w));
                                    make.width.equalTo(@(w));
                    }];
                }
            }
        }
    }
    return self;
    
}

【iOS开发】—— 仿写计算器_第1张图片

数据处理

我的数据处理中有两个主要方法,一个是判断括号是否匹配,一个是对字符串的计算。

判错处理:

我在按钮点击事件中增加了判断,对于不合法的操作禁止加入到目标字符串中。

GitHub:https://github.com/HaoQianbiao/Counter

你可能感兴趣的:(OC,计算器,masonry,ios)