递归 001:Boolean Expressions

001: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

解题思路
布尔表达式
程序中涉及两个函数:
1.表达式判断函数
2.项判断函数
即V,F,!V,!F是一个项;
表达式为整个布尔表达式或括号内的布尔表达式;

#include
#include
using namespace std;
//定义一个函数——判断项的v or f
bool expression_value();
bool term_value();
bool expression_value(){
	bool result = term_value();
	while(true){                   
		char op =cin.peek();       //看下一个输入是啥
		while (op==' '){              //分类——空格则跳过
			cin.get();
			op=cin.peek();
		}
		if(op=='&'||op=='|'){       //&或|则后面为项
			cin.get();
			bool value = term_value();       //得到后面项的值
			if (op=='&') result= result&&value;      //得到当前与字符后面项的bool值
			else result= result||value;
		}
		else break;
	}
	return result;
} 

bool term_value(){
	bool result = true;
		while(true){
			char op=cin.peek();
			while (op==' '){       //空格跳过
				cin.get();
				op=cin.peek();
			}
			if(op=='!'){        //!非
				cin.get();
				bool value = term_value();
				result =!value;
			}
			else if (op=='V'){    //true
				cin.get();
				result = true;
			}
			else if (op == 'F'){    //false
				cin.get();
				result =false;
			}
			else if (op=='('){        //遇到左括号,则下面是一个表达式
				cin.get();
				result = expression_value();      //递归
				cin.get();				
			}
			else break;				
			}
			return result;
		}
	
int main(){
		int i=1;
		while (cin.peek()=='('||cin.peek()=='V'||cin.peek()=='F'||cin.peek()=='&'||cin.peek()=='|'||cin.peek()=='!'){          //若有表达式输入
		bool t=expression_value();       //调用表达式判断函数 
		if(t==true){
		cout<<"Expression "<<i<<": V"<<endl;        //按格式输出
		i++;
	}
		else{
			cout<<"Expression "<<i<<": F"<<endl;
			i++;
		}
		cin.get();      //删除换行符 
	}
	return 0;
}

技巧:
1.cin.peek可以看到下一个输入,而不进行输入;
2.将括号内的表达式也看作表达式——运用递归思想

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