最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開發(fā)實(shí)現(xiàn)計(jì)算器功能

 更新時(shí)間:2021年10月10日 12:28:35   作者:Billy Miracle  
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下

效果圖

Masonry

使用數(shù)組來(lái)自動(dòng)約束

NSArray *buttonArrayOne = @[_buttonAC, _buttonLeftBracket, _buttonRightBracket, _buttonDivide];
    //withFixedSpacing: 每個(gè)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));
    }];

對(duì)最后一行單獨(dú)處理

 [_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的數(shù)字對(duì)齊

計(jì)算部分:

+ (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) {
                        //剛計(jì)算過(guò)括號(hào),符號(hào)前可以沒(méi)有數(shù)字讀入
                    } 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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

巴东县| 新化县| 涡阳县| 邢台县| 陆丰市| 正镶白旗| 安平县| 孝昌县| 昆明市| 洛浦县| 同江市| 辽中县| 准格尔旗| 明水县| 海城市| 榆树市| 安达市| 客服| 安泽县| 正蓝旗| 金平| 屯门区| 洛浦县| 渭南市| 江门市| 启东市| 马公市| 香格里拉县| 濮阳县| 明光市| 伊吾县| 色达县| 穆棱市| 宜君县| 礼泉县| 青浦区| 通海县| 安溪县| 东光县| 黄冈市| 江达县|