程序设计与算法(二)Boolean Expressions

题目
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))

样例输出

pression 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;
}

官方代码

#include <iostream>
#include <cstdio>
using namespace std;
char wholeExp[200];
bool exp();
bool factor();
bool item();
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);
		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;
	}
	return 0;
}

发布了26 篇原创文章 · 获赞 11 · 访问量 2305

猜你喜欢

转载自blog.csdn.net/qq_41731507/article/details/88921133
今日推荐