您的位置:首页 > 其它

poj 3295

2012-06-20 15:25 399 查看
大意是给定一个前缀布尔表达式,最多有5个变量,判断其是否恒为真。

这个确实不难,0到31地枚举5个变量的所有取值,计算表达式的值即可。

从右到左扫描前缀表达式,遇操作数则入栈,遇n元操作符则弹出n个操作数进行运算并将运算结果入栈。

最终在栈中的唯一操作数即为值。

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int p,q,r,s,t,slen;
char inStr[101];
stack<int> sta;
bool Solve()
{
while(!sta.empty())    sta.pop();
int i,fir,sec;
for(i=slen-1;i>=0;--i){
switch(inStr[i]){
case 'p':    sta.push(p);    break;
case 'q':    sta.push(q);    break;
case 'r':    sta.push(r);    break;
case 's':    sta.push(s);    break;
case 't':    sta.push(t);    break;
case 'K':    fir=sta.top();    sta.pop();    sec=sta.top();    sta.pop();    sta.push(fir && sec);    break;
case 'A':    fir=sta.top();    sta.pop();    sec=sta.top();    sta.pop();    sta.push(fir || sec);    break;
case 'N':    fir=sta.top();    sta.pop();    sta.push(!fir);    break;
case 'C':    fir=sta.top();    sta.pop();    sec=sta.top();    sta.pop();    sta.push( sec<= fir);    break;
                                     //  sec<= fir  等价于 sec||!fir
case 'E':    fir=sta.top();    sta.pop();    sec=sta.top();    sta.pop();    sta.push( sec== fir);    break;    break;
}
}
return sta.top();
}

int main()
{
int okn;
while(cin>>inStr,inStr[0]!='0'){
slen=strlen(inStr);
okn=0;
for(p=0;p<2;++p)
for(q=0;q<2;++q)
for(r=0;r<2;++r)
for(s=0;s<2;++s)
for(t=0;t<2;++t){
if(Solve()==0)
break;
else ++okn;
}
if(okn==32)    cout<<"tautology"<<endl;
else cout<<"not"<<endl;
}
return 0;
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c