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
就是离散数学的一个模拟,判断所给的式子是不是永真公式
用栈逆序推还不算难
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<stack> 5 using namespace std; 6 stack<int>sta; 7 int p,q,r,s,t,a,b; 8 char str[102]; 9 int f) 10 { 11 int len=strlenstr); 12 forint i=len-1; i>-1; i--) 13 { 14 ifstr[i]=='p')sta.pushp);//是字符直接入栈 15 else ifstr[i]=='q')sta.pushq); 16 else ifstr[i]=='r')sta.pushr); 17 else ifstr[i]=='s')sta.pushs); 18 else ifstr[i]=='t')sta.pusht); 19 else ifstr[i]=='K')//是运算就进行运算 20 { 21 a=sta.top); 22 sta.pop); 23 b=sta.top); 24 sta.pop); 25 sta.pusha&&b); 26 } 27 else ifstr[i]=='A') 28 { 29 a=sta.top); 30 sta.pop); 31 b=sta.top); 32 sta.pop); 33 sta.pusha||b); 34 } 35 else ifstr[i]=='C') 36 { 37 a=sta.top); 38 sta.pop); 39 b=sta.top); 40 sta.pop); 41 ifa&&!b)sta.push0); 42 else sta.push1); 43 } 44 else ifstr[i]=='E') 45 { 46 a=sta.top); 47 sta.pop); 48 b=sta.top); 49 sta.pop); 50 ifa==b)sta.push1); 51 else sta.push0); 52 } 53 else ifstr[i]=='N') 54 { 55 a=sta.top); 56 sta.pop); 57 sta.push!a); 58 } 59 } 60 return sta.top); 61 } 62 int sf)//枚举各种情况 63 { 64 forp=0; p<2; p++) 65 forq=0; q<2; q++) 66 forr=0; r<2; r++) 67 fors=0; s<2; s++) 68 fort=0; t<2; t++) 69 if!f)) 70 return 0; 71 return 1; 72 } 73 int main) 74 { 75 whilegetsstr)) 76 { 77 if!strcmpstr,"0")) 78 break; 79 else 80 { 81 ifsf)) 82 printf"tautology "); 83 else 84 printf"not "); 85 } 86 } 87 return 0; 88 }
View Code