[openjudge 6263] 布尔表达式(栈) 2017-05-12 19:05 105人阅读 评论(0) 收藏

描述
输入一个布尔表达式,请你输出它的真假值。
比如:( V | V ) & F & ( F | V )
V表示true,F表示false,&表示与,|表示或,!表示非。
上式的结果是F

输入
输入包含多行,每行一个布尔表达式,表达式中可以有空格,总长度不超过1000
输出
对每行输入,如果表达式为真,输出”V”,否则输出”F”
样例输入
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
样例输出
F
V
V

与表达式求值一样
特殊判断一下‘!’运算就行
括号优先级最高,其次是 取反 ,然后是

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>//栈
#include<iostream>
using namespace std;
int r[7][7]={
        {' ','|','&','!','(',')','='},

        {'|','>','>','<','<','>','>'},

        {'&','>','>','<','<','>','>'},

        {'!','>','>','=','<','>','>'},

        {'(','<','<','<','<','=',' '},

        {')','>','>','>',' ','>','>'},

        {'=','<','<','<','<',' ','='},
//为了防止!!V这样的情况 因为两个!可以抵消掉 所以 !和!写成= 不然只有5分
};
stack<int>opd;//使用系统栈
stack<char>opr;
char ss(char x,char y){
    switch(x){
        case '|':x=1;break;
        case '&':x=2;break;
        case '!':x=3;break;
        case '(':x=4;break;
        case ')':x=5;break;
        case '=':x=6;break;
    }
    switch(y){
        case '|':y=1;break;
        case '&':y=2;break;
        case '!':y=3;break;
        case '(':y=4;break;
        case ')':y=5;break;
        case '=':y=6;break;
    }
    return r[x][y];
}
char  s[1020];
char temp[1020];
void makeinput(char *temp){//此函数是为了读入 过滤其空格
    int p=0;
    int len=strlen(temp);
    for(int i=0;i<len;i++){
        if(temp[i]==' ') continue;
        s[p++]=temp[i];
    }
    s[p]='=';
    s[p+1]='';
}
nt ct1(int x,char ch,int y){
    switch(ch){
        case '|':return x|y;break;
        case '&':
            return x&y;break;
    }
}
int ct2(int x){
    return !x;
}
void deal(){
    while(!opr.empty())
        opr.pop();
    int t=0;
    opr.push('=');
    char ch;
    ch=s[t++];
    while(ch!='='||opr.top()!='='){
        if(ch=='V'||ch=='F'){
            if(ch=='V') opd.push(1),ch=s[t++];
            else if(ch=='F') opd.push(0),ch=s[t++];
        }
        else 
        switch(ss(opr.top(),ch)){
            case '<':opr.push(ch);ch=s[t++];break;
            case '>':{
                char cch=opr.top();
                opr.pop();
                //可能出现由!开始的情况 所以opd空的时候不能算
                else if(cch=='!'&&!(opd.empty())
                {
                    int x=opd.top();opd.pop();
                    opd.push(ct2(x));
                }
                else if(cch=='&'||cch=='|'){
                    int y=opd.top();opd.pop();
                    int x=opd.top();opd.pop();
                    opd.push(ct1(x,cch,y));
    //cout<<x<<" "<<cch<<" "<<y<<"="<<opd.top()<<endl;
                }
                break;
            }
            case '=':
                opr.pop();
                ch=s[t++];
                break;
        }
    }
    int c=opd.top();
    opd.pop();
    if(c==1) cout<<"V"<<endl;
    else cout<<"F"<<endl;
}
int main(){
    while(gets(temp)){
        makeinput(temp);
        deal();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xljxlj/p/7183668.html