HDU 2106 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 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

思路

一个布尔表达式可以看作多个布尔表达式经过逻辑运算符连接而成。涉及到的逻辑运算符有三个,!为单目运算符,后面跟着一个表达式,&&和||为双目运算符,左右各一个表达式。在拆分时需要分情况。主要有三个函数递归而成,主体的表达式函数,拆分表达式的函数,还需一个判断!的函数。对于(),可以将括号内的看成一个完整的表达式。
另外,需要一个变量记录表达式计算到哪个位置,如果用到该位置的数则变量加一,如果只是查看该位置的符号,则不用加一。
题中多次输入,注意每次使有变量要重置。我有一个变量忘了,卡了两三个小时(╥╯^╰╥)

代码

#include<iostream>
#include<string>
using namespace std;

int i,n;							//i用于记录表达式运算到哪个位置 
string s;

bool expression();
bool factor();
bool term();

bool expression()
{
	bool result=term();            //先拆分第一部分 
	while(i<n)
	{
		if(s[i]=='&')				
		{
			i++;
			bool value=term();
			result=result&&value;
		}
		if(s[i]=='|')
		{
			i++;
			bool value=term();
			result=result||value;
		}
		if(s[i]==')')				//出现)代表前一部分的表达式结束,返回结果 
		{
			i++;
			break;
		}
	}
	return result;
}

bool term()
{
	bool result;
	if(s[i]=='!')					//有!,需要对后面的一部分表达式取反 
	{
		i++;
		result=!factor();
	}
	else
		result=factor();
	return result;
}

bool factor()
{
	bool result;
	if(s[i]=='!')
	{
		result=term();
	}
	if(s[i]=='(')					//出现(后面又是一部分表达式 
	{
		i++;
		result=expression();
	}
	if(s[i]=='V')
	{
		i++;
		result=true;
	}
	if(s[i]=='F')
	{
		i++;
		result=false;
	}
	return result;
}

int main()
{
	string tmp;
	int t=0;
	while(getline(cin,tmp))
	{
		t++;
		s.clear();					
		i=0;						//将i清零 
		for(int j=0;j<tmp.length();j++)
		{
			if(tmp[j]!=' ')			//去除括号 
				s+=tmp[j];
		}
		n=s.length();
		if(expression())
			cout<<"Expression "<<t<<": V"<<endl;
		else
			cout<<"Expression "<<t<<": F"<<endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值