'commit'
This commit is contained in:
53
TangDou/CSP-J/J2-2022/expr_4_QiuZhi_LuoJi_JianHuaVersion.cpp
Normal file
53
TangDou/CSP-J/J2-2022/expr_4_QiuZhi_LuoJi_JianHuaVersion.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
0&(1|0)|(1|1|1&0)
|
||||
答案:1
|
||||
|
||||
(0|1&0|1|1|(1|1))&(0&1&(1|0)|0|1|0)&0
|
||||
答案:0
|
||||
*/
|
||||
unordered_map<char, int> h{{'|', 1}, {'&', 2}};
|
||||
stack<int> num;
|
||||
stack<char> op;
|
||||
|
||||
void eval() {
|
||||
int a = num.top();
|
||||
num.pop();
|
||||
|
||||
int b = num.top();
|
||||
num.pop();
|
||||
|
||||
char p = op.top();
|
||||
op.pop();
|
||||
|
||||
int r;
|
||||
if (p == '|')
|
||||
r = b | a;
|
||||
else if (p == '&')
|
||||
r = b & a;
|
||||
num.push(r);
|
||||
}
|
||||
|
||||
int main() {
|
||||
string s;
|
||||
cin >> s;
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
if (isdigit(s[i]))
|
||||
num.push(s[i] - '0');
|
||||
else if (s[i] == '(')
|
||||
op.push(s[i]);
|
||||
else if (s[i] == ')') {
|
||||
while (op.top() != '(') eval();
|
||||
op.pop();
|
||||
} else {
|
||||
while (op.size() && h[op.top()] >= h[s[i]]) eval();
|
||||
op.push(s[i]);
|
||||
}
|
||||
}
|
||||
while (op.size()) eval();
|
||||
|
||||
printf("%d\n", num.top());
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user