您的位置:首页 > 其它

poj3295Tautology

2016-05-29 17:15 239 查看
http://poj.org/problem?id=3295

表达式运算符合栈的结构特点,构造一个栈进行运算。

p,q,r,s,t的值共有2的五次方种可能组合,当成二进制不断加一进行枚举。

<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
char wff[200];
int operand[200],value[6];
int Caculate(char ch,int u,int v)
{
switch(ch)
{
case'K':return u&&v;
case'A':return u||v;
case'C':return !u||v;
case'E':return !(u^v);
}
}
int main()
{
int i,top,x,y,z,tag,c,n,len;
while(scanf("%s",wff)!=EOF)
{
if(wff[0] == '0')
break;
memset(value,0,sizeof(value));
n=0;
tag=1;
len=strlen(wff);
while(n<32)//枚举pqrst的所有情况 (二进制)
{
top=0;
memset(operand,0,sizeof(operand));
for(i=len-1;i>=0;i--)//逆向扫描表达式
{
if(wff[i]>='p'&&wff[i]<='t')
operand[top++]=value[wff[i]-112];
else if(wff[i]=='N')
{
z=operand[--top];
operand[top++]=!z;
}
else if(wff[i]=='K'|| wff[i]=='A'|| wff[i]=='C'|| wff[i]=='E')
{
x=operand[--top];
y=operand[--top];
operand[top++]=Caculate(wff[i],y,x);
}
}
if(operand[top-1]==0)//取出结果
{
tag=0;
break;//跳出枚举循环
}
value[0]++;
c=0;
while(value[c]>1)
{
value[c]=0;
c++;
value[c]++;
}
n++;
}
if(tag)
printf("tautology\n");
else
printf("not\n");
}
return 0;
}

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