-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
167 lines (161 loc) · 4.46 KB
/
calculator.cpp
File metadata and controls
167 lines (161 loc) · 4.46 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
#include <iostream>
#include "Stack/Stack.h"
#include "String/String.h"
using namespace std;
template<typename T>
T baseCal(T left, T right, char c) {
switch (c) {
case '+': {
return left + right;
}
case '-': {
return left - right;
}
case '*': {
return left * right;
}
case '/': {
return left / right;
}
}
if (typeid(T) == typeid(int) && c == '%') {
return (int) left % (int) right;
}
throw std::runtime_error("OPERATOR_ERROR");
}
template<typename T>
void calculate(Stack<T> &number, Stack<char> &operatorChar) {
T sum;
sum = number.top();
number.pop();
//cout << number.top() << char(operatorChar.top()) << sum;
sum = baseCal<T>(number.top(), sum, operatorChar.top());
number.pop();
number.push(sum);
//cout << "=" << sum << endl;
operatorChar.pop();
}
template<typename T>
void cal(Stack<T> &number, istream &in) {
Stack<char> operatorChar;
char c;
bool flag = false;
T num;
while ((c = in.peek()) != '\n') {
if (c == ' ') {
getchar();
continue;
} else if (isdigit(c)) {
in >> num;
if (flag) {
num = -num;
flag = false;
}
number.push(num);
} else {
in >> c;
if (c == '(') {
operatorChar.push(c);
while (in.peek() == ' ') {
getchar();
}
if (in.peek() == '-') {
flag = true;
getchar();
}
} else if (c == ')') {
while (operatorChar.top() != '(') {
calculate<T>(number, operatorChar);
}
operatorChar.pop();
} else if (c == '-' && number.empty()) {
flag = true;
} else if (!operatorChar.empty() &&
(operatorChar.top() == '*' || operatorChar.top() == '/' || operatorChar.top() == '%' ||
c == '-' || c == '+')) {
calculate<T>(number, operatorChar);
operatorChar.push(c);
} else {
operatorChar.push(c);
}
}
}
while (!operatorChar.empty()) {
calculate<T>(number, operatorChar);
}
cout << "ans=" << number.top();
}
void putBack(istream &in, String str) {
in.putback('\n');
auto it = str.end();
for (it--; it != str.begin(); it--) {
in.putback(*it);
}
in.putback(*it);
}
int main() {
int typeNum;
String str(unsigned(100));
cout << "请输入表达式(推荐数字或变量与符号之间用空格间隔,请使用半角符号)" << endl;
str.getLine(cin);
if (str.find(String("%")) != -1) {
typeNum = 1;
} else if (str.find(String(".")) != -1) {
typeNum = 2;
} else {
for (auto it = str.begin(); it != str.end(); it++) {
if (isalpha(*it)) {
typeNum = 3;
break;
}
}
if (typeNum != 3) {
typeNum = 1;
}
}
switch (typeNum) {
case 1: {
Stack<int> number;
putBack(cin, str);
try {
cal<int>(number, cin);
} catch (runtime_error error) {
cout << "输入的表达式有误";
}
break;
}
case 2: {
Stack<double> number;
putBack(cin, str);
try {
cal<double>(number, cin);
} catch (runtime_error error) {
cout << "输入的表达式有误";
}
break;
}
case 3: {
Stack<double> number;
String str_;
String s;
double num;
char num_[100];
cout << "请输入变量值(变量与数值用:间隔,变量间用空格间隔)" << endl;
while (cin.peek() != '\n') {
str_.read(cin, ':');
getchar();
cin >> num;
sprintf(num_, "%lf", num);
str.replaceAll(str_, String(num_));
}
putBack(cin, str);
try {
cal<double>(number, cin);
} catch (runtime_error error) {
cout << "输入的表达式有误";
}
break;
}
}
return 0;
}