zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
    
    
    
    
    
    
  • 相关阅读:
    python分包写入文件,写入固定字节内容,当包达到指定大小时继续写入新文件
    java 封装及this 用法
    [效率提升] 记一次使用工具编辑正则表达式快速输出匹配结果
    java用星星符号打印出一个直角三角形
    java按行和列进行输出数据
    java 三种循环及注意事项
    数据的运算,求和,两数求最大,三数求最大,两数是否相等
    采用位异或方式将两个变量数值调换
    今天遇到一件开心事,在eclipse编写的代码在命令窗口中编译后无法运行,提示 “错误: 找不到或无法加载主类”
    定义 java 基本数据类型
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/7264848.html
Copyright © 2011-2022 走看看