'commit'
This commit is contained in:
60
TangDou/CSP-J/J2-2022/expr_4_QiuZhi_LuoJi_CopyFromNormal.cpp
Normal file
60
TangDou/CSP-J/J2-2022/expr_4_QiuZhi_LuoJi_CopyFromNormal.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#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> stk;
|
||||
|
||||
void eval() {
|
||||
int b = num.top();
|
||||
num.pop();
|
||||
|
||||
int a = num.top();
|
||||
num.pop();
|
||||
|
||||
char p = stk.top();
|
||||
stk.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])) {
|
||||
int x = 0;
|
||||
while (i < s.size() && isdigit(s[i])) {
|
||||
x = x * 10 + s[i] - '0';
|
||||
i++;
|
||||
}
|
||||
i--;
|
||||
|
||||
num.push(x);
|
||||
} else if (s[i] == '(')
|
||||
stk.push(s[i]);
|
||||
else if (s[i] == ')') {
|
||||
while (stk.top() != '(') eval();
|
||||
stk.pop();
|
||||
} else {
|
||||
while (stk.size() && h[stk.top()] >= h[s[i]]) eval();
|
||||
stk.push(s[i]);
|
||||
}
|
||||
}
|
||||
while (stk.size()) eval();
|
||||
|
||||
printf("%d\n", num.top());
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user