您好,登錄后才能下訂單哦!
這篇文章主要講解了“iOS開發(fā)如何實現(xiàn)計算器功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“iOS開發(fā)如何實現(xiàn)計算器功能”吧!
效果圖
Masonry
使用數(shù)組來自動約束
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的數(shù)字對齊
計算部分:
+ (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) { //剛計算過括號,符號前可以沒有數(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; }
感謝各位的閱讀,以上就是“iOS開發(fā)如何實現(xiàn)計算器功能”的內容了,經過本文的學習后,相信大家對iOS開發(fā)如何實現(xiàn)計算器功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。