# 1003. Boolean Expressions

1. 遇到括号递归调用计算布尔值函数
2. 处理！运算符：可能出现在字母前、括号前或多次出现

#include <bits/stdc++.h>
using namespace std;
char input[2005];
char *p;
bool solve(void)
{
bool a=true, b=true, op=true, f=false;//op：操作判断，true为&，false为|；f：非判断，true时取非
//a是在这个字符之前的运算所得的布尔值，b是这步运算所得的布尔值
while(*p && *p!=')')//')'字符结束递归
{
if(*p=='(') {p++; b=f?!solve():solve();}
else if(*p=='V') {b=f?false:true; f=false;}
else if(*p=='F') {b=f?true:false; f=false;}
else if(*p=='&') {op=true; f=false;}
else if(*p=='|') {op=false; f=false;}
else if(*p=='!') f=f?false:true;
if(*p=='V' || *p=='F' || *p==')') a=op?a&b:a|b;
p++;
}
return a;
}
int main()
{
int cnt=1;
while(cin.getline(input, 2001, '\n'))
{
p=input;
if(solve()) cout<<"Expression "<<cnt++<<": V"<<endl;
else cout<<"Expression "<<cnt++<<": F"<<endl;
}
return 0;
}


i=0
while True:
i+=1
try:
x=input()
a = ‘’.join(x.split())
a=”and”.join(a.split(“&”))
a=”not”.join(a.split(“!”))
a=”or”.join(a.split(“|”))
a=”True”.join(a.split(“V”))
a=”False”.join(a.split(“F”))
s=”“;S=”“
for e in a:
s+=e
if s==”True” or s==”or” or s==”and” or s==”False” or s==”)” or s==”not”:
S+=s
S+=” “
s=”“
if s==”(“:
S+=s
s=”“
result=eval(S)
if result==False:
print(“Expression %d: F”%i)
if result==True:
print(“Expression %d: V”%i)
except:
break