zoukankan      html  css  js  c++  java
  • 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
    ",cnt,c);
            cnt++;
            cin.get();
        }while(cin.peek()!=-1);
        return 0;
    }

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

    表达式:项,项和项 | 

    项:因子,因子和因子 &

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

  • 相关阅读:
    STM32 + RT Thread OS 学习笔记[三]
    全代码实现ios-1
    HTML5 Web Speech API 结合Ext实现浏览器语音识别以及输入
    全代码实现ios-2
    从零开始学C++之虚函数与多态(一):虚函数表指针、虚析构函数、object slicing与虚函数
    二进制程序分析工具Pin在Windows系统中的安装和使用方法
    使用U盘安装Ubuntu系统的实践小结
    HDU 1874 畅通工程续
    JSP页面上用横线代替文本框
    Mysql设置编码
  • 原文地址:https://www.cnblogs.com/MalcolmMeng/p/9112705.html
Copyright © 2011-2022 走看看