-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.h
More file actions
327 lines (284 loc) · 6.8 KB
/
main.h
File metadata and controls
327 lines (284 loc) · 6.8 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#pragma once
#include "BigInt.h"
using namespace std;
BigInt operation(BigInt a, BigInt b, string op)
{
if (op == "+")
return a + b;
else if (op == "-")
return a - b;
else if (op == "*")
return a * b;
else if (op == "/")
return a / b;
cout << "operation error\n";
return a;
}
int getPriority(char cmd)
{
switch (cmd)
{
case ','://Power(a,b)
return 1;
case '+'://**加減 & 正負號 的區分?
case '-':
return 2;
case '*':
case '/':
return 3;
case '^':
return 4;
case '!':
return 5;
case '(':
case ')':
return 6;
default:
return 0;
}
}
BigInt process(string s)
{
string infix;
//清除空格
//infix.erase(std::remove(infix.begin(), infix.end(), ' '), infix.end());//remove space ' '
for (int i = 0; i < s.length(); i++)
if (s[i] != ' ')
infix += s[i];
//-------------------------------------------
// 中敘式 -> 後序式 <Infix to Postfix>
//-------------------------------------------
BigInt nowVar;//an number to save into stack
nowVar.sign = 0;
vector<BigInt> postfix, stack;
BigInt now;//an operation to save into stack
now.sign = 0;
now.type = 2;
bool isNegative = false; //區別負號&減號用
bool doPower = false;
bool check = false;
for (int i = 0; i < infix.length(); i++)
{
switch (infix[i])
{
case'(':
now.digits = "(";
if (isNegative)
{
now.sign = 1;
isNegative = 0;
}
else now.sign = 0;
stack.push_back(now);
break;
case')': //一直做到整個括號包完
while (stack.size() > 0)
{
if (stack.back().digits == "(")
{
stack.pop_back();
break;
}
else
{
postfix.push_back(stack.back());
stack.pop_back();
}
}
break;
default:
int priority = getPriority(infix[i]);
if (priority > 0) //不是數字
{
if ((infix[i] == '+' || infix[i] == '-') && (i == 0 || getPriority(infix[i - 1]) != 0)) //前一個不是數字 or 這在式子最前面,這就是負號
{
if (i > 2 && infix[i - 1] != '(' && infix[i - 2] != '(') {
break;
}
if (infix[i] == '-')
if (isNegative)
isNegative = 0;
else
isNegative = 1;
break; //讓負號不會在stack裡面,判斷stack時才不會混淆負號&減號
}
while (stack.size() > 0)
{
if (stack.back().digits == "(")
break;
if (priority > getPriority(stack.back().digits[0]))
break;
if (priority == 4)//階乘是由右至左運算(2^3^4 == 2^(3^4))
break;
postfix.push_back(stack.back());
stack.pop_back();
}
now.type = 2;
now.digits.clear();
now.digits = infix[i];
stack.push_back(now);
}
else
{
if (i + 1 < infix.length() && getPriority(infix[i + 1]) == 0) //下一格也是同個數字 or 英文
nowVar.digits.push_back(infix[i]);
else { //下一格不是數字 or 英文
nowVar.digits.push_back(infix[i]);
if (nowVar.digits[0] > '9' || nowVar.digits[0] < '0')//是一個變數名稱 or Power
{
if (nowVar.digits[0] == 'P' && nowVar.digits[1] == 'o' && nowVar.digits[2] == 'w' && nowVar.digits[3] == 'e' && nowVar.digits[4] == 'r')
{
//doPower = true;
nowVar.digits.clear();
break;
}
for (int i = 0; i < BigInt::All.size(); i++)
{
if (BigInt::All[i].name == nowVar.digits)
{
nowVar = BigInt::All[i];
break;
}
}
}
else
{
nowVar.dotPlace = nowVar.digits.find('.');
if (nowVar.dotPlace == string::npos) //是整數
nowVar.type = 0;
else
nowVar.type = 1;
nowVar.checkType();
}
if (check) break;
if (isNegative)
{
if (nowVar.sign)
nowVar.sign = 0;
else
nowVar.sign = 1;
isNegative = false;
}
postfix.push_back(nowVar);
nowVar.digits.clear();
nowVar.sign = 0;
}
}
break;
}
}
while (stack.size() > 0)
{
postfix.push_back(stack.back());
stack.pop_back();
}
cout << "\n-------------------final postfix-------------------\n\n";
for (int i = 0; i < postfix.size(); i++)
cout << (postfix[i].sign ? '-' : ' ') << postfix[i].digits << ",\n";
cout << "\n----------------------------------------------------\n";
//cout << "\n-------------------- [運算進行] --------------------\n";
//-------------------------------------------
// 算後序式 <Evaluate Postfix>
//-------------------------------------------
BigInt ERROR;
ERROR.digits = "default";
ERROR.sign = 0;
for (int i = 0; i < postfix.size(); i++)
{
if ((postfix[i].digits == "+") || (postfix[i].digits == "-") || (postfix[i].digits == "*") || (postfix[i].digits == "/") || (postfix[i].digits == "," || (postfix[i].digits == "^")))
{
//運算
BigInt a, b;
if (getPriority(stack.back().digits[0])) //若a,b 不是數字,就輸出錯誤
return ERROR;
else
{
b = stack.back();
stack.pop_back();
}
if (getPriority(stack.back().digits[0]))
return ERROR;
else
{
a = stack.back();
stack.pop_back();
}
if (postfix[i].digits == "+")
stack.push_back(a + b);
else if (postfix[i].digits == "-")
stack.push_back(a - b);
else if (postfix[i].digits == "*")
stack.push_back(a * b);
else if (postfix[i].digits == "/")
stack.push_back(a / b);
else if ((postfix[i].digits == "^") || (postfix[i].digits == ","))
stack.push_back(a ^ b);
else cout << "operation error\n";
if (postfix[i].sign == 1)
{
if (stack.back().sign == 1)
stack.back().sign = 0;
else stack.back().sign = 1;
}
if (stack.back().digits == "default")
{
stack.back().sign = 0;
return stack.back();
}
if (stack.back().digits == "0")
{
stack.back().sign = 0;
}
}
else if (postfix[i].digits == "!")
{
BigInt a = stack.back();
stack.pop_back();
if (a.type != 0 || a.sign != 0) {
for (int i = a.digits.find('.') + 1; i < a.digits.length(); i++)
{
if (a.digits[i] != '0')
{
cout << "Illegal : 階乘運算須為或正整數\n請重新輸入 : ";
a.digits = "default";
a.sign = 0;
return a;
}
}
}
BigInt one;
one.digits = "1"; one.type = 0; one.sign = 0;
if (a.compare("0") == 0) // 0!
stack.push_back(one);
else
{
BigInt times = a - one;
while (times.compare("1") == 1) //a > 1
{
a = a * times;
times = times - one;
}
stack.push_back(a);
}
}
else
{
stack.push_back(postfix[i]);
}
}
if (stack.back().getDigits().find('.') == string::npos) //是整數
stack.back().setType(0);
else
stack.back().setType(1);
stack.back().checkType();
return stack.back();
}
void printAllVar()
{
cout << "--------------------目前所有變數--------------------\n";
for (auto now : BigInt::All)
{
cout << now.getName() << " = " << now << endl;
}
cout << "----------------------------------------------------\n";
}