Boolean Expressions
Description
The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next:
Expression: ( V | V ) & F & ( F | V )
where V is for True, and F is for False. The expressions may include the
following operators: ! for not, & for and, | for or, the use of
parenthesis for operations grouping is also allowed. To perform the
evaluation of an expression, it will be considered the priority of the
operators, the not having the highest, and the or the lowest. The
program must yield V or F, as the result for each expression in the
input.
Input
The expressions are of a variable length, although will never exceed
2000 symbols. Symbols may be separated by any number of spaces or no
spaces at all, therefore, the total length of an expression, as a number
of characters, is unknown.
The number of expressions in the input file is variable and will never
be greater than 20. Each expression is presented in a new line, as shown
below.
Output
For each test expression, print “Expression “ followed by its
sequence number, “: ”, and the resulting value of the corresponding test
expression. Separate the output for consecutive test expressions with a
new line.
Use the same format as that shown in the sample output shown below.
Sample Input
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
Sample Output
Expression 1: F
Expression 2: V
Expression 3: V
Source
Mexico and Central America Region 2004
1 #include <stdio.h> 2 #include <string.h> 3 //#include <stack> 4 //using namespace std; 5 char str[2005]; 6 typedef struct stack{ 7 char buf[2000]; 8 int p; 9 stack(){ 10 p=-1; 11 } 12 void push(char a){ 13 buf[++p]=a; 14 } 15 void pop(){ 16 p--; 17 } 18 bool empty(){ 19 if(p==-1) return true; 20 else return false; 21 } 22 char top(){ 23 char tmp=buf[p]; 24 return tmp; 25 } 26 27 }stack; 28 stack op; 29 stack in; 30 char isNOT(char tmp){//push进一个数字之前看看op.top是不是! 31 while(op.top()=='!'){ 32 op.pop(); 33 tmp=(tmp=='V'?'F':'V'); 34 } 35 return tmp; 36 } 37 //stack<char> op;//操作符 38 //stack<char> in;//操作数 39 int main(){ 40 int count=0; 41 while(gets(str)){ 42 int i; 43 while(!op.empty()) op.pop(); 44 while(!in.empty()) in.pop(); 45 for(i=0;i<strlen(str);i++){ 46 while(str[i]==' ') i++; 47 if(str[i]=='F'||str[i]=='V'){ 48 char tmp=str[i]; 49 tmp=isNOT(tmp); 50 in.push(tmp); 51 } 52 else if(str[i]=='('){ 53 op.push(str[i]); 54 } 55 else if(str[i]==')'){ 56 while(op.top()!='('){ 57 char opr=op.top(); 58 op.pop(); 59 char tmp; 60 char a=in.top(); 61 in.pop(); 62 char b=in.top(); 63 in.pop(); 64 if(opr=='&') tmp=(a=='V'&&b=='V')?'V':'F'; 65 else tmp=(a=='V'||b=='V')?'V':'F'; 66 tmp=isNOT(tmp); 67 in.push(tmp); 68 } 69 op.pop();//弹出'(' 70 if(op.empty()==false&&op.top()=='!'){ 71 char tmp=in.top(); 72 in.pop(); 73 tmp=isNOT(tmp); 74 in.push(tmp); 75 } 76 } 77 else if(str[i]=='!'){ 78 op.push(str[i]); 79 } 80 else if(str[i]=='&'){ 81 while(op.empty()==false&&op.top()=='&'){ 82 op.pop(); 83 char a=in.top(); 84 in.pop(); 85 char b=in.top(); 86 in.pop(); 87 char tmp=(a=='V'&&b=='V')?'V':'F'; 88 in.push(tmp); 89 } 90 op.push(str[i]); 91 } 92 else if(str[i]=='|'){ 93 while(op.empty()==false&&(op.top()=='&'||op.top()=='|')){ 94 char opr=op.top(); 95 op.pop(); 96 char tmp; 97 char a=in.top(); 98 in.pop(); 99 char b=in.top(); 100 in.pop(); 101 if(opr=='&') tmp=(a=='V'&&b=='V')?'V':'F'; 102 else tmp=(a=='V'||b=='V')?'V':'F'; 103 in.push(tmp); 104 } 105 op.push(str[i]); 106 } 107 } 108 while(!op.empty()){ 109 char opr=op.top(); 110 op.pop(); 111 char tmp; 112 char a=in.top(); 113 in.pop(); 114 char b=in.top(); 115 in.pop(); 116 if(opr=='&') tmp=(a=='V'&&b=='V')?'V':'F'; 117 else tmp=(a=='V'||b=='V')?'V':'F'; 118 in.push(tmp); 119 } 120 count++; 121 printf("Expression %d: %c\n",count,in.top()); 122 123 } 124 return 0; 125 }

浙公网安备 33010602011771号