[iOS开发]计算器小结

39 篇文章 0 订阅

效果图

请添加图片描述

Masonry

使用数组来自动约束

	NSArray *buttonArrayOne = @[_buttonAC, _buttonLeftBracket, _buttonRightBracket, _buttonDivide];
    //withFixedSpacing: 每个view中间的间距
    //leadSpacing: 左最开始的间距
    //tailSpacing:; 右边最后的的间距
    [buttonArrayOne mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:15 tailSpacing:15];
    [buttonArrayOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(selfHeight - (buttonHeight * 5 + 110)));
        make.height.equalTo(@(buttonHeight));
    }];

对最后一行单独处理

	[_buttonZero mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.top.equalTo(@(selfHeight - (buttonHeight + 50)));
        make.width.equalTo(@(buttonWidth * 2 + 15));
        make.height.equalTo(@(buttonHeight));
    }];
    
    [_buttonZero.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_buttonOne.titleLabel);
    }];
    //使0的数字对齐

计算部分:

+ (Result)CalculateFor:(char*) formula andLen: (long) length {
    Result result = {0, 0.0f};
    int numberOfDots = 0;
    int index;
    int digitsNum = 0;
    float digits[CALCULATE_MAX_DIGITS];
    memset(digits, 0, sizeof(digits));
    int optNum = 0;
    char operator[CALCULATE_MAX_OPERATOR];
    memset(operator, 0, sizeof(operator));
    int digitNum = 0;
    char digit[CALCULATE_MAX_DIGIT];
    memset(digit, 0, sizeof(digit));
    char *p = formula;
    while (length--) {
        switch (*p) {
            case '+':
            case '-':
            case '*':
            case '/':
                numberOfDots = 0;
                if (0 == digitNum && '-' == *p) {
                    digit[digitNum++] = *p;
                } else {
                    if (-1 == digitNum) {
                        //刚计算过括号,符号前可以没有数字读入
                    } else if (0 == digitNum || CALCULATE_MAX_DIGITS == digitsNum - 1) {
                        result.error = CALCULATE_ERR;
                        return result;
                    } else {
                        digits[digitsNum++] = atof(digit);
                        memset(digit, '\0', sizeof(digit));
                    }
                    digitNum = 0;
                    operator[optNum++] = *p;
                }
                break;

            case '(': {
                char *pointer_son;
                int ExistEnd = 0;
                pointer_son = ++p;
                while(length--) {
                    if ('(' == *p) {
                        ExistEnd--;
                    } else if (')' == *p) {
                        ExistEnd++;
                    }
                    if (1 == ExistEnd) {
                        break;
                    }
                    p++;
                }
                Result result_son = [self CalculateFor:pointer_son andLen:p - pointer_son];
                if (CALCULATE_ERR == result_son.error) {
                    result.error = result_son.error;
                    return result;
                }
                digits[digitsNum++] = result_son.value;
                memset(digit, 0, sizeof(digit));
                digitNum = -1;
                break;
            }
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '.':
                digit[digitNum++] = *p;
                if (numberOfDots == 0 && *p == '.') {
                    numberOfDots = 1;
                } else if (numberOfDots == 1 && *p == '.') {
                    result.error = CALCULATE_ERR;
                    return result;
                }
                break;

            default:
                result.error = CALCULATE_ERR;
                return result;

        }
        if (0 == length && 0 < digitNum) {
            digits[digitsNum++] = atof(digit);
            memset(digit, 0, sizeof(digit));
            digitNum = 0;
        }
        p ++;
    }
    if (digitsNum != optNum + 1) {
        result.error = CALCULATE_ERR;
        return result;
    }
    for (index = 0; index < optNum; index ++) {
        if ('*' == operator[index]) {
            digits[index + 1] = digits[index] * digits[index + 1];
            digits[index] = 0;
            operator[index] = '?';
        } else if ('/' == operator[index]) {
            if (digits[index + 1] == 0) {
                result.error = CALCULATE_ERR;
                return result;
            }
            digits[index + 1] = digits[index] / digits[index + 1];
            digits[index] = 0;
            operator[index] = '?';
        }
    }
    for (index = 0; index < optNum; index ++) {
        if ('?' == operator[index]) {
            if (0 == index) {
                operator[index] = '+';
            } else {
                operator[index] = operator[index - 1];
            }
        }
    }
    result.value = digits[0];
    for (index = 0; index < optNum; index ++) {
        if ('+' == operator[index]) {
            result.value += digits[index + 1];
        } else if ('-' == operator[index]) {
            result.value -= digits[index + 1];
        }
    }
    return result;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值