• Boolean Expressions POJ


    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.
    Input
    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.
    Output
    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.
    Sample Input

    ( V | V ) & F & ( F| V)
    !V | V & V & !F & (F | V ) & (!F | F | !V & V)
    (F&F|V|!V&!F&!(F|F&V))

    Sample Output

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

    //栈
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<math.h>
    #include<cstdio>
    #include<sstream>
    #include<numeric>//STL数值算法头文件
    #include<stdlib.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<functional>//模板类头文件
    using namespace std;
    
    const int INF=1e9+7;
    const int maxn=110;
    typedef long long ll;
    
    char xsk[maxn],sum[maxn];
    int cnt,pos;
    char c;
    
    int insert(int t)
    {
        while(cnt&&xsk[cnt-1]==3)
        {
            t=!t;
            --cnt;
        }
        sum[pos++]=t;
    }
    
    void solve()
    {
        int a=sum[--pos];
        int b=sum[--pos];
        int d=xsk[--cnt];
        int ss=(a&b);
        if(d==1) ss=(a|b);
        insert(ss);
    }
    
    int main()
    {
        int k=1;
        while((c=getchar())!=EOF)
        {
            cnt=pos=0;
            do
            {
                if(c=='(')
                {
                    xsk[cnt++]=0;
                }
                else if(c==')')
                {
                    while(cnt&&xsk[cnt-1]!=0)
                        solve();
                    --cnt;
                    insert(sum[--pos]);
                }
                else if(c=='!')
                {
                    xsk[cnt++]=3;
                }
                else if(c=='&')
                {
                    while(cnt&&xsk[cnt-1]>=2)
                        solve();
                    xsk[cnt++]=2;
                }
                else if(c=='|')
                {
                    while(cnt&&xsk[cnt-1]>=1)
                        solve();
                    xsk[cnt++]=1;
                }
                else if(c=='V'||c=='F')
                {
                    insert(c=='V'?1:0);
                }
            }
            while((c=getchar())!='
    '&&c!=EOF);
            while(cnt) solve();
            printf("Expression %d: %c
    ",k++,(sum[0]?'V':'F'));
        }
        return 0;
    }
    
    
    //递归,看的别人的代码,能看就看,不懂就算了
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    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;
    }
    
    int main()
    {
        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]='';
            //到这里输入中的空格已经被去除干净了
            cout<<"Expression "<<++k<<": "<<(expression_bool()?"V":"F")<<endl;
            //初始化工作
            my_cnt=0;
            memset(s,0,100000);
        }
        return 0;
    }
    
    
    
    
    
    
    
    
  • 相关阅读:
    装备购买 线性基+贪心
    花园 状压DP+矩阵快速幂
    数学作业 递推+矩阵快速幂
    石头游戏 构造+矩阵快速幂
    sumdiv 算术基本定理的推论
    huffman
    Integer 类型比较大小
    java 中的 String 相加
    Java 中的 static 关键字
    JAVA 基础
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/7264848.html
Copyright © 2020-2023  润新知