码迷,mamicode.com
首页 > 其他好文 > 详细

005:Boolean Expressions

时间:2018-05-30 21:34:22      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:length   输入   ||   not   space   pen   exp   ping   hat   

描述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

标签:length   输入   ||   not   space   pen   exp   ping   hat   

原文地址:https://www.cnblogs.com/MalcolmMeng/p/9112705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!