-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
191 lines (182 loc) · 5.18 KB
/
Calculator.cpp
File metadata and controls
191 lines (182 loc) · 5.18 KB
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
//
// Created by 汪晓成 on 2019/9/28.
//
#include "Calculator.h"
char * Calculator::infix_to_suffix(char * exp) {
ArrayStack<char> stack(strlen(exp));//栈中只放运算符
char * Pexp = new char[2*strlen(exp)+1];//存取后缀表达式
exp = turnfushu(exp);
int j = 0;
for (int i = 0; i < strlen(exp)+1; ++i) {
if(exp[i]=='\0'){//清栈,把栈中的元素全部输出
char temp;
while (!stack.IsEmpty()) {
stack.Pop(temp);
if(temp == '('){
cout<<"错误"<<endl;
return 0;
}
Pexp[j++] = temp;
}
Pexp[j++] = '\0';
return Pexp;
}
if((exp[i]<='9'&&exp[i]>='0')||exp[i]=='!'||exp[i]=='.')
Pexp[j++] = exp[i];
else {
if(exp[i] == '(' ||exp[i] == ')') {
if (exp[i] == '(')
stack.Push(exp[i]);
else {
if(stack.IsEmpty()){
cout<<"错误"<<endl;
return 0;
}
char temp;
stack.Pop(temp);
while (temp != '(') {
Pexp[j++] = temp;
stack.Pop(temp);
}
}
}
else{
Pexp[j++] = ' ';//可以实现多位数
char temp;
while (!stack.IsEmpty()) {//判断优先级,优先级低则一直出栈加到字符串后面
stack.Pop(temp);
if(cmpop(exp[i],temp)>=0||temp=='(') {//相等要倒续放置,开括号即直接压,优先级高则压入
stack.Push(temp);
break;
}
Pexp[j++] = temp;
}
stack.Push(exp[i]);
}
}
}
return Pexp;
}
int Calculator::turnop(char op1) {
switch (op1){
case '+':
case '-':
return 1;
case '/':
case '*':
return 2;
//其他运算可以拓展
default:
return 0;
}
}
int Calculator::cmpop(char op1, char op2) {
return turnop(op1)-turnop(op2);
}
double Calculator::calculator(char *exp) {
return cal_suffix(infix_to_suffix(exp));
}
double Calculator::cal(double num1, double num2, char op) {
double res;
switch (op){// 可用工厂模式
case '+':
res = num1 + num2;
break;
case '-':
res = num1 - num2;
break;
case '*':
res = num1 * num2;
break;
case '/':
if(num2==0) {
cout << "错误";
exit(1);
}
res = num1 / num2;
break;
default:
return 1;
}
return res;
}
double Calculator::cal_suffix(char *exp) {
int i = 0;
double res = 0;
ArrayStack<double> stack(strlen(exp));//栈中只放数据,碰见一个运算符即将两个栈顶元素计算后继续压入
while (exp[i] != '\0') {
if ((exp[i] <= '9' && exp[i] >= '0')||exp[i]=='!') {
char * temp1= &exp[i],*temp2 = &exp[i];
int count = 0;
while((*temp1 <= '9' && *temp1 >= '0')||*temp1=='!'||*temp1=='.'){
temp1++;
if(*temp1 == '.'||count!=0)
count++;
}
double temp = 0;
for (int j = 0; temp1 != temp2; ++j) {
temp1--;
if(*temp1 == '!') {
temp = -temp;
i++;
break;
}
if(*temp1 == '.'){
i++;
j--;
continue;
}
temp += (*temp1-'0') * pow(10,j);
i++;
}
if(count!=0)
temp *= pow(10,-count+2);
stack.Push(temp);
i--;
}
else if(exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/'){
double temp[2];
if(stack.IsEmpty()) {
cout << "错误";
exit(1);
}
stack.Pop(temp[0]);
if(stack.IsEmpty()) {
cout << "错误";
exit(1);
}
stack.Pop(temp[1]);
stack.Push(cal(temp[1],temp[0],exp[i]));
}
i++;
}
stack.Pop(res);
//stack.Pop(res);
return res;
}
char * Calculator::turnfushu(char *exp) {
char * exp1 = new char[strlen(exp)];
if(exp[0]=='-')
exp1[0]='!';
else
exp1[0] = exp[0];
int i = 1;
while(exp[i]!='\0'){
if(exp[i] == '-'&&!isnumber(exp[i-1])) {
exp1[i] = '!';
continue;
}
exp1[i] = exp[i];
if(exp[i]=='+'||exp[i]=='*'||exp1[i]=='-'||exp[i]=='/') {
if(!isnumber(exp[i-1])||!isnumber(exp[i+1])) {
cout << "错误";
exit(1);
}
}
i++;
}
return exp1;
}
bool Calculator::isnumber(char a) {
return a <= '9' && a >= '0';
}