005:Boolean Expressions

Posted MalcolmMeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了005: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))

样例输出

Expression 1: F
Expression 2: V
Expression 3: V

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<cstring>
#define DEBUG(x) cout << #x << " = " << x << endl
using namespace std;
bool exp();
bool item();
bool factor();
void getSpace()
{
    while(cin.peek()== ){
        cin.get();
    }
}
bool exp()
{
    bool result=item();
    bool flag=true;
    while(flag){
        getSpace();
        char c=cin.peek();
        if(c==|){
            cin.get();
            bool t=item();
            result=result||t;
        }
        else{
            flag=false;
        }
    }
    return result;
}
bool item()
{
    bool result=factor();
    while(true){
        getSpace();
        char c=cin.peek();
        if(c==&){
            cin.get();
            bool t=factor();
            result=result&&t;
        }
        else break;
    }
    return result;
}
bool factor()
{
    bool result;
    getSpace();
    char c=cin.peek();
    if(c==(){
        cin.get();
        result=exp();
        cin.get();
    }
    else{
        if(c==!){
            cin.get();
            bool f=factor();
            result=!f;
        }
        else {
            char t=cin.get();
            if(t==F)result=false;
            else if(t==V)result=true;
        }
    }
    return result;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int cnt=1;
    do{
        bool f=exp();
        char c;
        if(f)c=V;
        else c=F;
        printf("Expression %d: %c\n",cnt,c);
        cnt++;
        cin.get();
    }while(cin.peek()!=-1);
    return 0;
}

类似表达式求值,一样的递归定义,然后按照递归定义写递归程序。

表达式:项,项和项 | 

项:因子,因子和因子 &

因子:(表达式),!因子,逻辑值

以上是关于005:Boolean Expressions的主要内容,如果未能解决你的问题,请参考以下文章

使用 Linq.Expressions.Expression 排序

ReactJs 函数调用,而是看到一个表达式 no-unused-expressions

: Expressions and Flow Control

期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions-ReactJs

第 103:9 行:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions

Return 语句导致:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions