递归-openjudge-1108:Boolean Expressions

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、那么如何解决这道看似很复杂的题?我想到的是可以用递归的方法解决 3、首先我们可以将这个问题分解为一个个子问题进行解决,若是碰到()括起来的数据,我们就将他看成一个整体进行运算。这样便轻松的将该题解出 4、因为输入每一个字符都需要空格,所以我们在输入字符串时应该调用gets进行输入
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
bool target(char area[], int star, int end)
{
	int mid, i;   //用mid保存起运算符在字符串中的下标 
	bool temp1, temp2;  //temp1保存第一个数据的true of false; 
	if(area[star] == 'V')
	{
		temp1 = 1;
		i = star+2;        //因为每个字符后面都有一个空格所以用i+=2跳过空格 
	}
	else if(area[star] == 'F')
	{
		temp1 = 0;
		i = star+2;
	}
	else if(area[star] == '!')   
	{
		switch(area[star+1])
		{
			case'F':temp1 = 1;
					i = star+3;
					break;
			case'V':temp1 = 0;
					i = star+3;
					break;
			case'(':for(i = star+3; i <= end; i++)
					{
						if(area[i] == ')')
						{
							temp1 = !target(area,star+3,i-2);   //遇见括号时看成整体,调用本身函数得到结果 
							i+=2;
							break;
						}
					}
					break;
		}
	}
	else if(area[star] == '(')
	{
		for(i = star+2; i <= end; i++)
		{
			if(area[i] == ')')
			{
				temp1 = target(area,star+2,i-2);
				i+=2;
				break;
			}
		}
	}
	for(; i <= end;)
	{
		mid = i;
		i+=2;
		if(area[i] == 'V')
		{
			temp2 = 1;
			i+=2;
		}
		else if(area[i] == 'F')
		{
			temp2 = 0;
			i+=2;
		}
		else if(area[i] == '!')
		{
			i++;
			switch(area[i])
			{
				case'F':temp2 = 1;
						i+=2;
						break;
				case'V':temp2 = 0;
						i+=2;
						break;
				case'(':int k;
						for(k = i+2; k <= end; k++)
						{
							if(area[k] == ')')
							{
								temp2 = !target(area, i+2,k-2);
								i = k+2;
								break;
							}
						}
						break;
			}
		}
		else if(area[i] == '(')
		{
			int j;
			for(j = i+2; j <= end; j++)
			{
				if(area[j] == ')')
				{
					temp2 = target(area,i+2,j-2);
					i = j+2;
					break;
				}
			}
		}
		switch(area[mid])
		{
			case'&':temp1 = temp1 & temp2;
					break;
			case'|':temp1 = temp1 | temp2;
					break;
		}
	}
	return temp1;
}
int main()
{
	char s[10001];
	int i = 1;
	while(gets(s)) //使用gets()防止空格结束字符串的输入 
	{
		int len = strlen(s);  //获取字符串长度 
		bool result = target(s, 0, len-1);
		if(result)
		{
			cout << "Expression " << i << ": V" << endl; 
		}
		else
		{
			cout << "Expression " << i << ": F" << endl; 
		}
		i++;
	}
	return 0;
 } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值