转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1309062835
大致题意:
输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式,
其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量;
K、A、N、C、E为逻辑运算符,
K --> and: x && y
A --> or: x || y
N --> not : !x
C --> implies : (!x)||y
E --> equals : x==y
问这个逻辑表达式是否为永真式。
PS:输入格式保证是合法的
解题思路:
p, q, r, s, t不同的取值组合共32种情况,枚举不同取值组合代入逻辑表达式WFF进行计算。
如果对于所有的取值组合,WFF值都为 true, 则结果为 tautology,否则为 not。
WFF的计算方法:
从字符串WFF的末尾开始依次向前读取字符。
构造一个栈stack,当遇到逻辑变量 p, q, r, s ,t 则将其当前的值压栈;
遇到 N 则取栈顶元素进行非运算,运算结果的值压栈;
遇到K, A, C, E则从栈顶中弹出两个元素进行相应的运算,将结果的值压栈。
由于输入是合法的,当字符串WFF扫描结束时,栈stack中只剩一个值,该值就是逻辑表达式WFF的值。
栈可以自己构造,也可以利用STL的<stack>,都一样,下面我两种代码都贴出:
/** \brief poj 3295
*
* \param date 2014/8/3
* \param state AC
* \return memory 708k time 0ms
*
*/
#include <iostream>
#include <stack>
#include <fstream>
#include <cstring>
using namespace std;
int pp,qq,rr,ss,tt;//各个逻辑变量的值
stack<int> s;
bool isVariables(char ch);//判断ch是否为变量p q r s t,若是则把其当前值入栈
void operators(char op);//根据操作符op对栈执行操作
int main()
{
//cout << "Hello world!" << endl;
//freopen("input.txt","r",stdin);
char WFF[110];
while(cin>>WFF && WFF[0]!='0')
{
int len=strlen(WFF);
bool flag=true;//标记逻辑表达式是否为永真式
for(pp=0;pp<=1;pp++)
{
for(qq=0;qq<=1;qq++)
{
for(rr=0;rr<=1;rr++)
{
for(ss=0;ss<=1;ss++)
{
for(tt=0;tt<=1;tt++)
{
//processing
for(int pw=len-1;pw>=0;pw--)
{
if(!isVariables(WFF[pw]))
operators(WFF[pw]);
}
int ans=s.top();//最后栈剩一个值,即为逻辑表达式的值
s.pop();
if(!ans)//只要表达式有一个值为假,它就不是永真式
{
flag=false;
break;
}
}
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(flag)
cout<<"tautology"<<endl;
else
cout<<"not"<<endl;
}
return 0;
}
bool isVariables(char ch)
{
switch(ch)
{
case 'p':s.push(pp);return true;
case 'q':s.push(qq);return true;
case 'r':s.push(rr);return true;
case 's':s.push(ss);return true;
case 't':s.push(tt);return true;
}
return false;
}
void operators(char op)
{
switch(op)
{
case 'K':
{
int x=s.top();
s.pop();
int y=s.top();
s.pop();
s.push(x&&y);
break;
}
case 'A':
{
int x=s.top();
s.pop();
int y=s.top();
s.pop();
s.push(x||y);break;
}
case 'C':
{
int x=s.top();
s.pop();
int y=s.top();
s.pop();
s.push((!x)||y);
break;
}
case 'E':
{
int x=s.top();
s.pop();
int y=s.top();
s.pop();
s.push(x==y);
break;
}
case 'N':
{
int x=s.top();
s.pop();
s.push(!x);
break;
}
}
return ;
}
下面是比较经典的递归,自己没想到这么做。。。。学习了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int state[5];
char s[205];
int l=0;
int ind()
{
char ch=s[l++];
printf("");
switch(ch)
{
case 'p':
case 'q':
case 'r':
case 's':
case 't':
return state[ch-'p'];
break;
case 'K':
return ind()&ind();
break;
case 'A':
return ind()|ind();
break;
case 'N':
return !ind();
break;
case 'C':
return !ind()|ind();
break;
case 'E':
return ind()==ind();
break;
}
}
int main()
{
scanf("%s", s);
while(s[0]!='0')
{
int len=strlen(s);
int mark=1;
for(state[0]=0; state[0]<=1 && mark; state[0]++)
{
for(state[1]=0; state[1]<=1 && mark; state[1]++)
{
for(state[2]=0; state[2]<=1 && mark; state[2]++)
{
for(state[3]=0; state[3]<=1 && mark; state[3]++)
{
for(state[4]=0; state[4]<=1 && mark; state[4]++)
{
l=0;
if(ind()==0)
mark=0;
}
}
}
}
}
if(mark==1)
printf("tautology\n");
else
printf("not\n");
scanf("%s", s);
}
return 0;
}