You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
931 B
43 lines
931 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
/*
|
|
中缀逻辑表达式转后缀逻辑表达式
|
|
|
|
测试用例:
|
|
0&(0|1|0)
|
|
|
|
答案:
|
|
001|0|&
|
|
*/
|
|
unordered_map<char, int> g{{'|', 1}, {'&', 2}};
|
|
string s, t;
|
|
stack<char> stk;
|
|
|
|
int main() {
|
|
cin >> s;
|
|
for (int i = 0; i < s.size(); i++) {
|
|
if (isdigit(s[i]) || isalpha(s[i]))
|
|
t += s[i];
|
|
else if (s[i] == '(')
|
|
stk.push(s[i]);
|
|
else if (s[i] == ')') {
|
|
while (stk.top() != '(') {
|
|
t += stk.top();
|
|
stk.pop();
|
|
}
|
|
stk.pop();
|
|
} else {
|
|
while (stk.size() && g[s[i]] <= g[stk.top()]) {
|
|
t += stk.top();
|
|
stk.pop();
|
|
}
|
|
stk.push(s[i]);
|
|
}
|
|
}
|
|
while (stk.size()) {
|
|
t += stk.top();
|
|
stk.pop();
|
|
}
|
|
cout << t << endl;
|
|
return 0;
|
|
} |