gpt4 book ai didi

iOS开发实现计算器功能

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章iOS开发实现计算器功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

效果图 。

iOS开发实现计算器功能

Masonry 。

使用数组来自动约束 。

?
1
2
3
4
5
6
7
8
9
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));
     }];

对最后一行单独处理 。

?
1
2
3
4
5
6
7
8
9
10
11
[_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的数字对齐

计算部分:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
+ (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;
}

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

原文链接:https://blog.csdn.net/weixin_52192405/article/details/120642536 。

最后此篇关于iOS开发实现计算器功能的文章就讲到这里了,如果你想了解更多关于iOS开发实现计算器功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com