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

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

    表达式:项,项和项 | 

    项:因子,因子和因子 &

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

  • 相关阅读:
    ASP.NET Core Web API 帮助页
    SQL SERVER 被锁住的表,以及解锁。
    关键字查找相关存储过程,函数和视图
    MsSql 生成数据文档
    WebApiTestClient 接口测试页
    sql日期
    为什么同样的Sql语句在SqlServer RDS 查询得到的和自己本机SqlServer 查询的不一样呢?
    VS 无法在web服务器上启动调试。您没有调试web服务器进程的权限
    ROS学习之创建工作空间
    QT学习之forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget"
  • 原文地址:https://www.cnblogs.com/MalcolmMeng/p/9112705.html
Copyright © 2011-2022 走看看