OpenJ_Bailian - 2106:Boolean Expressions

本文介绍了一个用于评估布尔表达式的程序设计,该程序能够处理包括与、或、非在内的多种运算符,支持括号操作,并考虑运算符优先级。通过样例输入输出展示了程序的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <cstdio>

using namespace std;

char wholeExp[200]; //表达式
bool exp();
bool factor();
bool item();
//bool notExp();
int ptr = 0;

bool exp() {
	bool result = item();
	while(wholeExp[ptr] == '|' ) {
		++ptr;
		result = result | item();
	}
	return result;
}

bool item() {
	bool result = factor();
	while(wholeExp[ptr] == '&') {
		++ptr;
		result = result & factor();
	}
	return result;
}

bool notExp()
{
	//wholeExp[ptr] == '!' when called;
	ptr++;
	bool result;
	switch(wholeExp[ptr]) {
		case 'F':
			++ptr;
			return true;
			break;
		case 'V':
			++ptr;
			return false;
			break;
		case '(':
			++ptr;
			result = exp();
			++ptr;  //skip ')'
			return !result;
			break;
		case '!':
			result = notExp();
			return !result;
			break;
	}
}


bool factor() {
	bool result;
	switch( wholeExp[ptr]) {
		case 'F':
			++ptr;
			return false;
			break;
		case 'V':
			++ptr;
			return true;
			break;
		case '(':
			++ptr;
			result = exp();
			++ptr;
			return result;
			break;
		case '!':
			result = notExp();
			return result;
			break;
	}
}


int main()
{
	char c;  //输入载体
	int i = 0;
	int t = 1;  //计数
	int n = EOF + 1;
	while(n != EOF)
    {
        n =  scanf("%c",&c); //成功读入时返回1,否则返回0

		if(	n == EOF || c == '\n') {//文件要结束时
			wholeExp[i] = 0;
			if( i > 0) {
				ptr = 0;
				bool r = exp();
				if (r) {
					printf("Expression %d: V\n",t++);
				}
				else
					printf("Expression %d: F\n",t++);
			}
			i = 0;
		}
		else if( c != ' ') //将数据存入数组
			wholeExp[i++] = c;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱事顺利、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值