How to realize Calculator function in iOS Development
This article mainly explains "iOS development how to achieve calculator function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "iOS development how to achieve calculator function" bar!
Effect picture
Masonry
Use arrays to automatically constrain
NSArray * buttonArrayOne = @ [_ buttonAC, _ buttonLeftBracket, _ buttonRightBracket, _ buttonDivide]; / / withFixedSpacing: the spacing in the middle of each view / / leadSpacing: the first spacing on the left / / the last spacing on the right side of the 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));}]
Deal with the last line separately
[_ 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) }]; / / align the numbers of 0
Calculation part:
+ (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 [Calcuate _ 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) {/ / parentheses have just been calculated, and no digits can be read before the symbol} else if (0 = = digitNum | | CALCULATE_MAX_DIGITS = = digitsNum-1) {result.error = CALCULATE_ERR; return result } else {digits [digitsNum++] = atof (digit); memset (digit,'\ 0mm, 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;} paired + } 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 Thank you for your reading, the above is the content of "how to achieve calculator function in iOS development". After the study of this article, I believe you have a deeper understanding of how to achieve calculator function in iOS development, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!