-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq3.cpp
More file actions
143 lines (122 loc) · 2.93 KB
/
q3.cpp
File metadata and controls
143 lines (122 loc) · 2.93 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
#include <iostream>
#include <map>
#include <functional>
#include <string>
#include <stack>
using namespace std;
// 3. 等价表达式
// 利用两个栈,一个栈储存符号,一个栈储存数字。把中缀表达式转化为后缀表达式并求解,这两个过程耦合在一起完成
// 把字母的ASCII值带入计算,这样会简单很多
// 但是似乎有问题, a+1 和 b 这两个式子就会错误的相等,可以通过将字母用多种值表示,多次比较表达式的值
// 这个解法通过了 coursera 的测试
int arithmetic(int num1, int num2, char operation) {
int res = 0;
switch (operation) {
case '+':
res = num1 + num2;
break;
case '-':
res = num2 - num1;
break;
case '*':
res = num1 * num2;
break;
default:
break;
}
return res;
}
int calculate(string s) {
int n = s.size();
// 添加负号的前导0
for (int i = 0; i < n; i++) {
char ch = s[i];
if (ch == '-') {
if (i == 0) {
s = '0' + s;
} else if (s[i - 1] == '(') {
s = (s.substr(0, i) + '0' + s.substr(i, n - i));
}
}
}
stack<int> num = stack<int>();
stack<char> symbol = stack<char>();
// 符号优先度映射表
map<char, int> operationPriority;
operationPriority.insert(make_pair('+', 0));
operationPriority.insert(make_pair('-', 0));
operationPriority.insert(make_pair('*', 1));
n = s.size();
for (int i = 0; i < n; i++) {
char ch = s[i];
if (ch == ' ' || ch == '\t') {
continue;
} else if (ch >= 48 && ch <= 57) {
// 数字压栈
num.push(ch - 48);
} else if (ch >= 97) {
// 变量的ASCII值压入栈
num.push(ch);
} else if (ch == ')') {
// 优先计算小括号的内容
while (symbol.top() != '(') {
int num1 = num.top();
num.pop();
int num2 = num.top();
num.pop();
num.push(arithmetic(num1, num2, symbol.top()));
symbol.pop();
}
// 去掉左小括号
symbol.pop();
} else if (ch == '(') {
symbol.push(ch);
} else {
// 符号
while (!symbol.empty()) {
char currSymbol = symbol.top();
// 计算表达式直到遇到比其优先度小的符号或括号
if (currSymbol == '(') {
break;
} else if (operationPriority[ch] <= operationPriority[currSymbol]) {
int num1 = num.top();
num.pop();
int num2 = num.top();
num.pop();
num.push(arithmetic(num1, num2, currSymbol));
symbol.pop();
} else {
break;
}
}
symbol.push(ch);
}
}
// 计算剩余的表达式
while (!symbol.empty()) {
int num1 = num.top();
num.pop();
int num2 = num.top();
num.pop();
num.push(arithmetic(num1, num2, symbol.top()));
symbol.pop();
}
return num.top();
}
int main(int argc, char *argv[]) {
int n = 0;
cin >> n;
cin.get(); // 丢弃换行符
for (int i = 0; i < n; i++) {
char s1[100];
char s2[100];
cin.getline(s1, 100);
cin.getline(s2, 100);
if (calculate(s1) == calculate(s2)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}