Description
WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules:
p, q, r, s, and t are WFFs
if w is a WFF, Nw is a WFF
if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.
The meaning of a WFF is defined as follows:
p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
Definitions of K, A, N, C, and E
w x Kwx Awx Nw Cwx Ewx
1 1 1 1 0 1 1
1 0 0 1 0 0 0
0 1 0 1 1 1 0
0 0 0 0 1 1 1
A tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the value 0 for p=0, q=1.
You must determine whether or not a WFF is a tautology.
Input
Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.
Output
For each test case, output a line containing tautology or not as appropriate.
Sample Input
ApNp
ApNq
0
Sample Output
tautology
not
大概题意
输入由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:输入格式保证是合法的
我的 代码
#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
bool c(bool a,bool b)
{
if(a&&!b)
{
return false;
}
else
{
return true;
}
}
bool e(bool a,bool b)
{
if(a&&b||!a&&!b)
{
return true;
}
else
{
return false;
}
}
bool solve(string str,int p,int q,int r,int s,int t)
{
stack <bool> ele;
unsigned int i;
for(i=str.size()-1;i>=0;i--)
{
switch(str[i])
{
case 'p':ele.push((bool)p);break;
case 'q':ele.push((bool)q);break;
case 'r':ele.push((bool)r);break;
case 's':ele.push((bool)s);break;
case 't':ele.push((bool)t);break;
case 'K':
{
bool a = ele.top();
ele.pop();
bool b = ele.top();
ele.pop();
ele.push(a&&b);
}
break;
case 'A':
{
bool a = ele.top();
ele.pop();
bool b = ele.top();
ele.pop();
ele.push(a||b);
}
break;
{
case 'N':
bool a = ele.top();
ele.pop();
ele.push(!a);
}
break;
case 'C':
{
bool f = ele.top();
ele.pop();
bool g = ele.top();
ele.pop();
ele.push(c(f,g));
}
break;
case 'E':
{
bool h = ele.top();
ele.pop();
bool j = ele.top();
ele.pop();
ele.push(e(h,j));
break;
}
default:break;
}
}
return ele.top();
}
int main()
{
string str;
bool flag = true;
int i = 0;
vector <string> ans;
while(cin>>str&&str!="0")
{
int p,q,r,s,t;
for(p=0;p<=1;p++)
{
for(q=0;q<=1;q++)
{
for(r=0;r<=1;r++)
{
for(s=0;s<=1;s++)
{
for(t=0;t<=0;t++)
{
flag = solve(str,p,q,r,s,t);
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(flag)
{
ans.push_back("tautology");
}
else
{
ans.push_back("not");
flag = true;
}
}
for(i=0;i!=ans.size();i++)
{
cout<<ans[i]<<endl;
}
return 0;
}