欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发实现计算器功能

程序员文章站 2022-03-08 15:38:05
本文实例为大家分享了ios实现计算器功能的具体代码,供大家参考,具体内容如下效果图masonry使用数组来自动约束nsarray *buttonarrayone = @[_buttonac, _but...

本文实例为大家分享了ios实现计算器功能的具体代码,供大家参考,具体内容如下

效果图

iOS开发实现计算器功能

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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: iOS 计算器