POJ 2106 Boolean Expressions(递归算法)

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

看到网上的大多数都是用栈的方法解决该题的,而我则采用不同的方法,使用递归的思路来解决这道题布尔表达式理解成由项和因子组成,因子可由表达式组成,而表达式由项组成,而项由因子组成,即形成了递归的定义.

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char s[100001]={0};
int my_cnt=0;
bool expression_bool();
bool term_bool();
bool factor_bool();

bool expression_bool(){//求一个表达式的值
	bool result=term_bool();//求第一项的值
	bool more=true;
	while(more){
		char op=s[my_cnt];//看一个字符,不取走
		if(op=='&'||op=='|'){
			my_cnt++;//从数组中取走一个字符
			bool value=term_bool();
			if(op=='&') result=result&value;
			else result=result|value;
		}else{
			more=false;
		}
	}

	return result;
}

bool term_bool(){//因为!的运算优先级更高,所以把!xxx也当成一个项
	bool result;
	char op=s[my_cnt];
	if(op=='!'){
		my_cnt++;
		result=!factor_bool();
	}else{
		result=factor_bool();
	}

	return result;
}

bool factor_bool(){//求一个因子的值
	bool result;
	char c=s[my_cnt];
	if(c=='('){//如果该因子是由括号括起来的表达式组成的话
		my_cnt++;
		result=expression_bool();
		my_cnt++;
	}else if(c=='V'){
		result=true;
		my_cnt++;
	}else if(c=='F'){
		result=false;
		my_cnt++;
	}else if(c=='!'){//出现了!,说明读取到了一个因子
		result=term_bool();
	}

	return result;
}

void oj_3_1(){
	int k=0;
	while(cin.getline(s,100000)){
		char t[100001]={0};
		int len=strlen(s);
		for(int i=0,k=0;i<len;++i){
			if(s[i]!=' '){
				t[k++]=s[i];
			}
		}

		len=strlen(t);
		for(int i=0;i<len;++i){
			s[i]=t[i];
		}
		s[len]='\0';
		
		//到这里输入中的空格已经被去除干净了
		cout<<"Expression "<<++k<<": "<<(expression_bool()?"V":"F")<<endl;
		//初始化工作
		my_cnt=0;
		memset(s,0,100000);
	}

}

int main(){
	oj_3_1();
	return 0;
}



    原文作者:递归算法
    原文地址: https://blog.csdn.net/lovemylife1234/article/details/54918096
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞