POJ005:Boolean Expressions 布尔表达式

005: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.
V是真,F是假,表达式可能包含以下操作符:!是非,&是与,|是或,表达式还可以用括号
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.
为了计算表达式的值,需要考虑操作符的优先级,非优先级最高,或优先级最低,程序需要输出V或F作为每个表达式的结果
输入
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.
表达式字符不大于100个(不包括空格),因此表达式的总长不定
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.
表达式的个数不大于20,每个表达式占一行
输出
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

#include 
#include 
#include 
using namespace std;
list express;
bool notExpress();
bool orExpress();
bool andExpress();
bool factorExpress();

bool orExpress(){//计算或表达式的值,优先级最低的操作符最后算
	bool result=andExpress();
	while(true){
		char exp= express.front();
		if(exp=='|'){
			express.pop_front();
			result|=andExpress();
		}
		else if (exp == ' ') {
			express.pop_front();
		}
		else	break;
	}
	return result;
}
bool andExpress(){//计算与操作符的值
	bool result=notExpress();
	while(true){
		char exp= express.front();
		if(exp=='&'){
			express.pop_front();
			result&=notExpress();
		}
		else if (exp == ' ') {
			express.pop_front();
		}
		else	break;
	}
	return result;
}
bool notExpress(){//计算非的值,非是单目运算符,因此和前两个运算方式不一样
	bool result;
	while (true) {
		char exp= express.front();	
		if (exp == ' ') {
			express.pop_front();
			exp = express.front();
		}
		if(exp=='!'){
			express.pop_front();
			result=factorExpress();
			if(result)	return false;
			else	return true;
		}else{
			result=factorExpress();
			break;
		}
	}
	return result;
}
bool factorExpress(){
	char exp= express.front();
	bool result;
	if (exp == ' ') {
		express.pop_front();
		exp = express.front();
	}
	if(exp=='('){
		express.pop_front();
		result=orExpress();
		express.pop_front();
		return result;
	}
	else if (exp == '!') {//出现多个!的情况
		result = notExpress();
		return result;
	}
	else{
		if (exp == 'V') {
			express.pop_front();
			return true;
		}
		else if (exp == 'F') {
			express.pop_front();
			return false;
		}
	}
	
}

int main(){
	int i=1;
	string exps;
	while (getline(cin,exps)) {
		express.clear();
		for (int i = 0; i < exps.length(); i++) {
			express.push_back(exps[i]);
		}
		express.push_back('x');
		bool result=orExpress();
		express.pop_front();
		if(result)	cout<<"Expression "<

你可能感兴趣的:(算法)