This commit is contained in:
HuangHai
2025-08-30 18:35:01 +08:00
commit 1f397eca87
6258 changed files with 1487241 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
#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;
}