Approximate test instructions:
Enter a logical expression consisting of 10 letters of P, Q, R, S, T, K, A, N, C, E,
where P, Q, R, S, T value is 1 (true) or 0 (false), that is, the logical variable;
K, A, N, C, E are logical operators,
K--and:x && y
A--Or:x | | Y
N--not:!x
C--implies: (!x) | | Y
E--Equals:x==y
Ask if this logical expression is a perpetual truth.
#include <iostream> using namespace std;
int count=0; struct Stack {int value;}
STACK[101]; int N (int x) {return!x;} int E (int x,int y) {return x==y;} int C (int x,int y) {return (!x) | |
Y } int K (int x,int y) {return x&&y;} int A (int x,int y) {return x| |
Y
} void Insert (int num) {stack[count].value=num;
count++;
} int Pop () {int x;
X=stack[count-1].value;
count--;
return x;
} bool Isvalue (char ch,int p,int q,int r,int s,int t) {switch (ch) {case ' P ': Insert (P);
return true;
Case ' Q ': Insert (q);
return true;
Case ' R ': Insert (R);
return true;
Case ' s ': Insert (s);
return true;
Case ' t ': Insert (t);
return true;
} return false;
} void Compute (char ch) {int x, y;
Switch (CH) {case ' K ': X=pop ();
Y=pop ();
Insert (K (x, y));
Break
Case ' A ': X=pop ();
Y=pop ();
Insert (A (x, y));
Break
Case ' C ': X=pop ();
Y=pop ();
Insert (C (x, y));
Break
Case ' E ': X=pop ();
Y=pop ();
Insert (E (x, y));
Break
Case ' N ': X=pop (); Insert (N (x));
Break
}} int main (int argc,char *argv[]) {char wff[101];
int length,i;
int p,q,r,s,t;
int end;
BOOL Flag;
memset (stack,0,sizeof (stack));
while (cin>>wff&&wff[0]!= ' 0 ') {length=strlen (WFF);
Flag=true;
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<=1;t++) {i=length-1;
while (i>=0) {if (!isvalue (wff[i],p,q,r,s,t)) {compute (wff[i]);
} i--;
} End=stack[count-1].value;
count--;
if (!end) {flag=false;
Break
}}}}} if (flag==true) {cout<< "tautology" <<endl;
} else {cout<< "not" <<endl;
}} return 0; }