栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 2106 Boolean Expressions

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 2106 Boolean Expressions

#include<cstdio>#include<cstring>#include<algorithm>#include<cctype>using namespace std;const int N=1000;char comp(char a,char b){    if(b=='')    {        if(a=='') return '=';        else return '>';    }    else if(a=='')        return '<';    else if(b=='(')        return '<';    else if(b==')')    {        if(a=='(') return '=';        else return '>';    }    else if(a=='(')        return '<';    else        return '>';}bool cac(bool a,bool b,char op){    if(op=='|')        return a||b;    else if(op=='&')        return a&&b}bool solve(char s[]){    int len=strlen(s),optop=0,botop=0,i=0;    char opstk[N];    bool bostk[N];    opstk[optop++]='';    while(s[i]!=''||opstk[optop-1]!='')    {        if(s[i]==' '||s[i]=='t')        { i++; continue;        }        else if(s[i]=='!'||s[i]=='(') opstk[optop++]=s[i++];        else if(s[i]=='V'||s[i]=='F') bostk[botop++]=(s[i]=='V'),i++;        else        { switch(comp(opstk[optop-1],s[i])) {     case '>':         if(opstk[optop-1]=='!')         {  optop--;  bostk[botop-1]^=true;         }         else         {  bool b=bostk[--botop],a=bostk[--botop];  char ch=opstk[--optop];  bostk[botop++]=cac(a,b,ch);         }         break;     case '<':         opstk[optop++]=s[i++];         break;     case '=':         optop--;         i++;         break; }        }    }    return bostk[0];}int main(){    char s[200];    int ca=0;    while(gets(s))        printf("expression %d: %cn",++ca,solve(s)?'V':'F');    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378197.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号