POJ 3295 constructs a stack to evaluate the value of an expression

Source: Internet
Author: User
Tags bool logical operators

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; }


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.