poj 2106 Boolean Expressions(特殊的表达式求值)
Time Limit: 1000ms Memory Limit: 65536kB
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 file.
Input
The expressions are of a variable length, although will never exceed 100 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
México and Central America 2004
犹记得学习计算概论时候,布尔表达式和表达式求值那些题目就是不会用栈做,只能自己硬扫描做,美其名曰自己思考,不能很快学习助教的代码也不愿意问别人学习,不原意看书或自己钻研代码。表面上一直自己在写代码,其实很慢效率很低,很易错,大部分时间都浪费在Debug上了,以战术上的勤奋掩饰我战略上的懒惰。以至于那个时候看见这类题就头皮发麻。
暑假了硬着头皮看了表达式求职,那些以前望而生畏的东西现在看来不过如此,其实其他知识也是这样,要勇于善于学习新东西,不畏艰险,善于钻研。
那么这个题就是个表达式求值了,没什么难度,唯一要注意一元表达式,以及!!V情况。
另外discuss区有人指出可以用LL(1)文法,先mark一下,如果看看学的会的话学习一下, 不然就到《编译原理》再学。
Accepted 136kB 1ms 1384 B G++
#include<stdio.h>
int cases=0;
char stack[120];
int top;
char infix[10000],post[120];
int len;
inline int rank(char ch)
{
if (ch=='!')
return 3;
else if (ch=='&')
return 2;
else if (ch=='|')
return 1;
else
return 0;
}
void change()
{
char stack[120];
int top=0;
len=0;
for (char* ch=infix;*ch;ch++)
if (*ch!=' ')
{
if (*ch=='V' || *ch=='F')
post[len++]=*ch;
else if (rank(*ch))
{
if (*ch=='!' && stack[top-1]=='!')
{
top--;
continue;
}
while (top && stack[top-1]!='(' && rank(stack[top-1])>=rank(*ch))
post[len++]=stack[--top];
stack[top++]=*ch;
}
else if (*ch=='(')
stack[top++]=*ch;
else if (*ch==')')
{
while (stack[top-1]!='(')
post[len++]=stack[--top];
top--;
}
}
while (top)
post[len++]=stack[--top];
post[len]=0;
return;
}
void calcu()
{
top=0;
for (char* ch=post;*ch;ch++)
{
if (*ch=='!')
{
if (stack[top-1]!='!')
stack[top-1]='V'+'F'-stack[top-1];
}
else if (*ch=='&')
{
if (stack[top-2]=='V' && stack[top-1]=='F')
stack[top-2]='F';
top--;
}
else if (*ch=='|')
{
if (stack[top-2]=='F' && stack[top-1]=='V')
stack[top-2]='V';
top--;
}
else
stack[top++]=*ch;
}
return;
}
int main()
{
while (gets(infix))
{
cases++;
change();
calcu();
printf("Expression %d: %c\n",cases,stack[0]);
}
return 0;
}