-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseBoolExpr.cpp
More file actions
44 lines (44 loc) · 1.28 KB
/
parseBoolExpr.cpp
File metadata and controls
44 lines (44 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
bool parseBoolExpr(string expression) {
stack<char> st;
bool ht = false, hf = false;
for (char c: expression) {
ht = false;
hf = false;
if (c == ')') {
while (st.top() != '(') {
if (st.top() == 't') ht = true;
else if (st.top() == 'f') hf = true;
st.pop();
}
st.pop();
char op = st.top();
st.pop();
switch (op) {a
case '&': {
if (hf) st.push('f');
else st.push('t');
break;
}
case '|': {
if (ht) st.push('t');
else st.push('f');
break;
}
case '!': {
if (ht) st.push('f');
else st.push('t');
break;
}
default:
break;
}
}
else if (c != ',') {
st.push(c);
}
}
return st.top() == 'f' ? false : true;
}
};