本文实例为大家分享了C语言简易计算器的具体代码,供大家参考,具体内容如下
主要解决了处理负数、小数等的基础运算操作,无图形界面
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#include <iostream>
#include <stack>
using namespace std;
class Calculator{
private :
int Priority( char fuhao);
double CalSuffix(string PostfixExp);
public :
double Calculate(string InfixExp);
string InfixToSuffix(string InfixExp);
};
double Calculator::CalSuffix(string PostfixExp){
double tmpresult,ch1,ch2;
double tmpnum,tmpxiaoshu=1;
int i=0,tmpdashu;
int isfu=0; ///
stack< double > stk2;
while (PostfixExp[i]!= '\0' ){
isfu=0; ///
if (PostfixExp[i]>=48&&PostfixExp[i]<=57){
if (PostfixExp[i-1]== '-' ){ /////
isfu=1;
}
tmpxiaoshu=1;
tmpdashu=10;
tmpnum = PostfixExp[i]-48;
while (PostfixExp[++i]>=48&&PostfixExp[i]<=57){
tmpnum = tmpnum*tmpdashu+ (PostfixExp[i]-48);
}
i=i-1;
if (PostfixExp[++i]== '.' ){
while (PostfixExp[++i]>=48&&PostfixExp[i]<=57){
tmpxiaoshu=tmpxiaoshu*0.1;
tmpnum = tmpnum + (PostfixExp[i]-48)*tmpxiaoshu;
}
i=i-1;
}
else {
i=i-1;
}
if (isfu){ ////
tmpnum=tmpnum*(-1);
}
stk2.push(tmpnum);
}
else if (PostfixExp[i]== '&' ||PostfixExp[i]== ' ' ){
}
else {
if (PostfixExp[++i]>=48&&PostfixExp[i]<=57){
i=i-1;
}
else {
i=i-1;
ch2 = stk2.top();
stk2.pop();
ch1 = stk2.top();
stk2.pop();
switch (PostfixExp[i]){
case '+' : tmpnum = ch1 + ch2; break ;
case '-' : tmpnum = ch1 - ch2; break ;
case '*' : tmpnum = ch1 * ch2; break ;
case '/' : tmpnum = ch1 / ch2;
if (ch2==0) cout<< "除数为零" ; break ;
}
stk2.push(tmpnum);
}
}
i++;
}
if (stk2.empty()!=1){
tmpresult = stk2.top();
stk2.pop();
}
return tmpresult;
}
double Calculator::Calculate(string InfixExp){
double result;
result = CalSuffix(InfixToSuffix(InfixExp));
return result;
}
int Calculator::Priority( char fuhao){
switch (fuhao){
case '+' :
case '-' : return 2;
case '*' :
case '/' : return 3;
case '(' :
case ')' : return 1;
default :
return 0;
}
}
string Calculator::InfixToSuffix(string InfixExp){
stack< char > stk;
string PostfixExp = " " ;
int i=0,j=0;
char tmpfuhao;
int flag = 0; //判断多位数的头数是否为零
while (InfixExp[i]!= '\0' ){
if (InfixExp[i]>=48&&InfixExp[i]<=57){
flag = 0;
PostfixExp[j++]= '&' ;
PostfixExp[j++]=InfixExp[i];
if (InfixExp[i]== '0' ){
flag = 1;
}
while (InfixExp[++i]>=48&&InfixExp[i]<=57){
if (flag==0)
PostfixExp[j++]=InfixExp[i];
else
cout<< "输入错误数字" ;
}
i=i-1;
if (InfixExp[++i]== '.' ){
PostfixExp[j++]= '.' ;
while (InfixExp[++i]>=48&&InfixExp[i]<=57){
PostfixExp[j++]=InfixExp[i];
}
i=i-1;
}
else {
i=i-1;
}
}
else if (InfixExp[i]== '(' ){
stk.push(InfixExp[i]);
}
else if (InfixExp[i]== ')' ){
if (stk.empty()){
cout<< "表达式错误!" ;
}
else {
tmpfuhao = stk.top();
while (tmpfuhao!= '(' ){
if (stk.empty()){
cout<< "表达式错误!" ;
}
else {
PostfixExp[j++] = '&' ;
PostfixExp[j++] = tmpfuhao;
stk.pop();
tmpfuhao = stk.top();
}
}
stk.pop();
}
}
else if (InfixExp[i]== '+' ||InfixExp[i]== '-' ||InfixExp[i]== '*' ||InfixExp[i]== '/' ){
if (i==0||((InfixExp[--i]<48||InfixExp[i]>57)&&InfixExp[i]!= ')' )){
i++;
PostfixExp[j++]= '&' ;
PostfixExp[j++]= '-' ;
while (InfixExp[++i]>=48&&InfixExp[i]<=57){
PostfixExp[j++]=InfixExp[i];
}
i=i-1;
if (InfixExp[++i]== '.' ){
PostfixExp[j++]= '.' ;
while (InfixExp[++i]>=48&&InfixExp[i]<=57){
PostfixExp[j++]=InfixExp[i];
}
i=i-1;
}
else {
i=i-1;
}
}
else {
i++;
if (stk.empty()){
stk.push(InfixExp[i]);
}
else {
tmpfuhao = stk.top();
if (Priority(tmpfuhao)<Priority(InfixExp[i])){
stk.push(InfixExp[i]);
}
else {
while (Priority(tmpfuhao)>=Priority(InfixExp[i])){
PostfixExp[j++] = '&' ;
PostfixExp[j++] = tmpfuhao;
stk.pop();
if (stk.empty()!=1){
tmpfuhao = stk.top();
}
else break ;
}
stk.push(InfixExp[i]);
}
}
}
}
else {
cout<< "符号错误!" ;
break ;
}
i++;
}
while (!stk.empty()){
tmpfuhao = stk.top();
PostfixExp[j++] = '&' ;
PostfixExp[j++] = tmpfuhao;
stk.pop();
}
PostfixExp[j++] = '\0' ;
return PostfixExp;
}
int main( int argc, const char * argv[]) {
string a;
Calculator a1;
cin>>a;
cout<<a1.Calculate(a)<<endl;
cout<<a1.InfixToSuffix(a);
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/thj19980720/article/details/78378154