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.
44 lines
989 B
44 lines
989 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
/*
|
|
中缀的逻辑表达式 转 后缀的逻辑表达式
|
|
|
|
测试用例:
|
|
0&(0|1|0)
|
|
|
|
答案:
|
|
001|0|&
|
|
*/
|
|
unordered_map<char, int> h{{'|', 1}, {'&', 2}};
|
|
string s;
|
|
string t;
|
|
stack<char> stk;
|
|
|
|
int main() {
|
|
cin >> s;
|
|
for (int i = 0; i < s.size(); i++) {
|
|
if (isdigit(s[i]) || isalpha(s[i]))
|
|
t.push_back(s[i]);
|
|
else if (s[i] == '(')
|
|
stk.push(s[i]);
|
|
else if (s[i] == ')') {
|
|
while (stk.top() != '(') {
|
|
t.push_back(stk.top());
|
|
stk.pop();
|
|
}
|
|
stk.pop();
|
|
} else {
|
|
while (stk.size() && h[s[i]] <= h[stk.top()]) {
|
|
t.push_back(stk.top());
|
|
stk.pop();
|
|
}
|
|
stk.push(s[i]);
|
|
}
|
|
}
|
|
while (stk.size()) {
|
|
t.push_back(stk.top());
|
|
stk.pop();
|
|
}
|
|
printf("%s", t.c_str());
|
|
return 0;
|
|
} |