-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
308 lines (285 loc) · 6.25 KB
/
Source.cpp
File metadata and controls
308 lines (285 loc) · 6.25 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
#include<iostream>
using namespace std;
#include<string>
// ============================= KHỞI TẠO CẤU TRÚC STACK =============================
// khai báo cấu trúc 1 cái node
struct NODE
{
string data; // dữ liệu của node
NODE* pNext; // con trỏ liên kết đến các node khác trong stack
};
// khai báo cấu trúc STACK
struct STACK
{
NODE* pTop; // con trỏ quản lý đầu STACK
};
// hàm khởi tạo node
NODE* KhoiTaoNode(string x)
{
NODE* p = new NODE;
p->data = x;
p->pNext = NULL;
return p;
}
// hàm kiểm tra xem STACK có rỗng hay không, trả về true nếu STACK rỗng, ngược lại trả về false
bool IsEmpty(STACK& s)
{
if (s.pTop == NULL)
{
return true;
}
return false;
}
// hàm thêm node p vào đầu danh sách - hàm thêm phần tử vào STACK
bool Push(STACK& s, NODE* p)
{
if (IsEmpty(s) == true)
{
s.pTop = p;
}
else
{
p->pNext = s.pTop;
s.pTop = p;
}
return true;
}
// hàm lấy phần tử đầu STACK ra và xóa phần tử đầu STACK
bool Pop(STACK& s, string& x)
{
if (IsEmpty(s) == true)
{
return false;
}
else
{
x = s.pTop->data; // lưu dữ liệu lại trước khi xóa
NODE* k = s.pTop;
s.pTop = s.pTop->pNext;
delete k;
}
return true;
}
// hàm lấy phần tử đầu STACK ra xem
bool Top(STACK s, string& x)
{
if (IsEmpty(s) == true)
{
return false;
}
else
{
x = s.pTop->data;
}
return true;
}
struct QUEUE
{
NODE* pTop; // con trỏ quản lý đầu QUEUE
};
// hàm kiểm tra xem QUEUE có rỗng hay không, trả về true nếu QUEUE rỗng, ngược lại trả về false
bool IsEmpty(QUEUE& q)
{
if (q.pTop == NULL)
{
return true;
}
return false;
}
// hàm thêm node p vào cuối danh sách - hàm thêm phần tử vào QUEUE
bool Push(QUEUE& q, NODE* p)
{
if (IsEmpty(q) == true)
{
q.pTop = p;
}
else
{
for (NODE* k = q.pTop; k != NULL; k = k->pNext)
{
if (k->pNext == NULL)
{
k->pNext = p;
return true;
}
}
}
return true;
}
// hàm lấy phần tử đầu QUEUE ra và xóa phần tử đầu QUEUE
bool Pop(QUEUE& q, string& x)
{
if (IsEmpty(q) == true)
{
return false;
}
else
{
x = q.pTop->data; // lưu dữ liệu lại trước khi xóa
NODE* k = q.pTop;
q.pTop = q.pTop->pNext;
delete k;
}
return true;
}
// hàm lấy phần tử đầu QUEUE ra xem
bool Top(QUEUE& q, string& x)
{
if (IsEmpty(q) == true)
{
return false;
}
else
{
x = q.pTop->data;
}
return true;
}
int getPriority(string op) {
if (op == "*" || op == "/") {
return 2;
}
else if (op == "+" || op == "-") {
return 1;
}
else {
return 0;
}
}
int isOperator(string op) {
if (getPriority(op) == 0) { // != +, -, *, /
if (op != "(" && op != ")") {
return 0; // so 0 - 9
}
else { // ( || )
return 1;
}
}
return 2; // +, -, *, /
}
void Resolve(string str, STACK& s, QUEUE& q) {
NODE* p;
string number = ""; // luu toan hang co nhieu chu so
for (int i = 0; i < str.length(); i++) {
string sCurrent(1, str[i]);// char => string
if (sCurrent != " ") {
if (isOperator(sCurrent) == 0) { // la so 0 - 9
number += sCurrent; // them toan hang vao chuoi number
}
else { // la toan tu or dau ngoac
if (number.length() > 0) {
p = KhoiTaoNode(number);
Push(q, p); // push number <=> toan hang vao Queue
number = ""; // cap nhat lai number = rong tiep tuc vong lap
}
if (isOperator(sCurrent) == 1) { // ( or )
if (sCurrent == "(") {
p = KhoiTaoNode(sCurrent);
Push(s, p); // push vao Stack
}
else if (sCurrent == ")") {
string pop;
Pop(s, pop); // lay phan tu dau Stack ra kiem tra
while (pop != "(") { // chay While den khi nao gap dau (
p = KhoiTaoNode(pop);
Push(q, p); // Push vao queue
Pop(s, pop); // tiep tuc vong lap
}
}
}
else { // la toan tu
string topS;
Top(s, topS); // kiem tra phan dau dau Stack
while (!IsEmpty(s) && getPriority(topS) >= getPriority(sCurrent)) { // kiem tra toan tu dau Stack voi toan tu hien tai
Pop(s, topS);
p = KhoiTaoNode(topS);
Push(q, p); // push vao Queue
Top(s, topS); // tiep tuc lay phan tu dau Stack kiem tra
}
p = KhoiTaoNode(sCurrent);
Push(s, p); // push toan tu hien tai vao Stack
}
}
}
}
// kiem tra xem con toan hang nao khong
if (number.length() > 0) {
p = KhoiTaoNode(number);
Push(q, p);
number = "";
}
// kiem tra cac phan tu con lai trong Stack
while (!IsEmpty(s)) {
string data;
Pop(s, data);
p = KhoiTaoNode(data);
Push(q, p);
}
}
int Calculate(STACK& s, QUEUE& q) {
string data;
string tmp;
string res;
NODE* p = new NODE;
int val1, val2;
while (!IsEmpty(q)) {
Pop(q, data);
if (isOperator(data) == 0) { // la so 0 - 9
p = KhoiTaoNode(data);
Push(s, p); // push vao STACK
}
else if (isOperator(data) == 2) { // toan tu +, -, *, /
Pop(s, tmp);
val1 = stoi(tmp); // string to int
Pop(s, tmp);
val2 = stoi(tmp);
if (data == "+") {
res = to_string(val2 + val1); // int to string
p = KhoiTaoNode(res);
Push(s, p);
}
else if (data == "-") {
res = to_string(val2 - val1);
p = KhoiTaoNode(res);
Push(s, p);
}
else if (data == "*") {
res = to_string(val2 * val1);
p = KhoiTaoNode(res);
Push(s, p);
}
else {
res = to_string(val2 / val1);
p = KhoiTaoNode(res);
Push(s, p);
}
}
}
Pop(s, data);
return stoi(data);
}
int main() {
STACK s;
s.pTop = NULL; // STACK ĐANG RỖNG
QUEUE q;
q.pTop = NULL; // QUEUE ĐANG RỖNG
string str;
cout << "\n Nhap bieu thuc trung to can tinh: ";
getline(cin, str);
Resolve(str, s, q);
QUEUE cal; // giu bieu thuc hau to de tinh toan
cal.pTop = NULL;
NODE* p = new NODE;
cout << "\n Bieu thuc hau to: ";
while (!IsEmpty(q)) {
string data;
Pop(q, data);
p = KhoiTaoNode(data);
Push(cal, p);
cout << data << " ";
}
cout << endl;
cout << "\n\t KET QUA BIEU THUC = " << Calculate(s, cal) << endl;
system("pause");
return 0;
}