#原题
1:Boolean Expressions
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
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.
样例输入
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
样例输出
Expression 1: F
Expression 2: V
Expression 3: V
来源
México and Central America 2004
##题目大意
V代表真,F代表非,&代表与,|代表或,!代表取反,输入多组数据,空格的数量不限,得出最后的结果。
##题目分析
采用递归的方法
很简单的一道题,如果知道了方法,就很容易解决。
我们先分析输入的样例。
我觉得大家至少也是大学生或者高中生(初中生,小学生的话那都是天才了)下面讲一下基本的数学知识。
表达式是由项和因子构成,项由因子构成。就这么简单。
而本题,表达式和项是一样的意思。因为没有像乘除加减这样优先次序。
从输入例子可以分析出 这一长串的字符可以分解成两部分 表达式和因子,或者说项和因子也行。
而项又是由一个个因子组成的。
所以逐层分解可以看出
其实就是因子与因子之间作逻辑运算。
最后的因子的结果在做逻辑运算。
最后得出整个表达式的结果
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
string s;
int expression_value(int result);//表达式求值
int factor_value(int result);//因子求值
int ptr = 0;//数组指针
int main()
{
string ori;
int ncase=0;
while(getline(cin,ori))//删除空格
{
for(int i = 0;i<ori.size();i++)
{
char ch = ori[i];
if(ori[i]!=' ')
s=s+ch;
}
ptr = 0;//指针回到字符串头
if(expression_value(0))
cout<<"Expression "<<++ncase<<": V"<<endl;
else
cout<<"Expression "<<++ncase<<": F"<<endl;
s.clear();ori.clear();//清空,以备下一组使用
}
return 0;
}
int expression_value(int result)
{
result = factor_value(0);//表达式是由因子构成
char ch = s[ptr];//查看字符
while(ch == '&' || ch == '|')
{
ptr++;//指针往后移一位
int tmp = factor_value(0);//读出一个因子的值
if(ch == '&')
result = result && tmp;//表达式与表达式做运算
if(ch == '|')
result = result || tmp;
ch = s[ptr];//查看下一个字符
}
return result;
}
int factor_value(int result)
{
char ch = s[ptr];
while(ch =='(' || ch == '!' || ch == 'V' || ch == 'F' )
{
ptr++;
if(ch == '(')
{
result = expression_value(0);// ()内相当于一个表达式
ptr++;// 跳过 ")"
}
if(ch == '!')
{
result = ! factor_value(0);
}
if(ch == 'V')
{
return 1;
}
if(ch == 'F')
{
return 0;
}
ch = s[ptr];//查看下一个字符
}
return result;
}
2001



被折叠的 条评论
为什么被折叠?



