注意处理'('前的 '!',设置优先级

Boolean Expressions

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4697   Accepted: 1461

Description

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

Source

México and Central America 2004

#include<iostream>
using namespace std;const int N=100+10;int val[N],vtop;
int op[N],otop;void insert(int b){while(otop&&op[otop-1]==3){b=!b;--otop;}val[vtop++]=b;
}void calc(){int b=val[--vtop];int a=val[--vtop];int opr=op[--otop];int c=a&b;if(opr==1)c=a|b;insert(c);
}int main(){int loop=0;char c;while((c=getchar())!=EOF){vtop=otop=0;do{if(c==' ')continue;else if(c=='(')op[otop++]=0;else if(c==')'){while(otop&&op[otop-1]!=0)calc();--otop;insert(val[--vtop]);}else if(c=='!')op[otop++]=3;else if(c=='&'){while(otop&&op[otop-1]>=2)calc();op[otop++]=2;}else if(c=='|'){while(otop&&op[otop-1]>=1)calc();op[otop++]=1; }else if(c=='V'||c=='F'){insert(c=='V'?1:0);}}while((c=getchar())!='\n'&&c!=EOF);while(otop)calc();printf("Expression %d: %c\n",++loop,val[0]==1?'V':'F');}
}

2018_2_3_Boolean Expressions_栈_模拟相关推荐

  1. 学习笔记:SpringCloud 微服务技术栈_实用篇①_基础知识

    若文章内容或图片失效,请留言反馈.部分素材来自网络,若不小心影响到您的利益,请联系博主删除. 前言 学习视频链接 SpringCloud + RabbitMQ + Docker + Redis + 搜 ...

  2. 学习笔记:SpringCloud 微服务技术栈_高级篇⑤_可靠消息服务

    若文章内容或图片失效,请留言反馈.部分素材来自网络,若不小心影响到您的利益,请联系博主删除. 前言 学习视频链接 SpringCloud + RabbitMQ + Docker + Redis + 搜 ...

  3. Colorful Lecture Note(栈的模拟)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm lecture note for Little Ho. ...

  4. 学习笔记:SpringCloud 微服务技术栈_实用篇②_黑马旅游案例

    若文章内容或图片失效,请留言反馈.部分素材来自网络,若不小心影响到您的利益,请联系博主删除. 前言 学习视频链接 SpringCloud + RabbitMQ + Docker + Redis + 搜 ...

  5. 2020年上海市高等学校信息技术水平考试试卷_三级_数据科学技术及应用_模拟卷_四、操作题_答案

    2020年上海市高等学校信息技术水平考试试卷_三级_数据科学技术及应用_模拟卷_四.操作题_答案 (本试卷考试时间 150 分钟) 答案是自己做的,经验证,可成功运行. 内容仅供学习交流,不可转载. ...

  6. java模拟加速匀速减速_【JavaSE练习】面向对象_练习_模拟汽车的自动驾驶系统

    [JavaSE练习]面向对象_练习_模拟汽车的自动驾驶系统 [JavaSE练习]面向对象_练习_模拟汽车的自动驾驶系统 模拟汽车驾驶系统 定义一个交通工具的类,包括: 属性:速度 方法:设置速度,加速 ...

  7. 模板_单调栈_AcWing_830. 单调栈_底顶递增栈

    模板_单调栈_AcWing_830. 单调栈_底顶递增栈 830. 单调栈 题目 提交记录 讨论 题解 视频讲解 给定一个长度为 NN 的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出 − ...

  8. UJNOJ_1307: 数独 [for ACMer]_模拟

    UJNOJ_1307: 数独 [for ACMer]_模拟 1307: 数独 [for ACMer] 内存限制:128 MB时间限制:10 S 评测方式:文本比较命题人:20151222169 提交: ...

  9. 2020年上海市高等学校信息技术水平考试试卷_三级_数据科学技术及应用_模拟卷_三、程序填空题_答案

    2020年上海市高等学校信息技术水平考试试卷_三级_数据科学技术及应用_模拟卷_三.程序填空题_答案 (本试卷考试时间 150 分钟) 答案是自己做的,经验证,可成功运行. 内容仅供学习交流,不可转载 ...

最新文章

  1. java 方法泛型_Java基础学习,什么是泛型概念,使用泛型有哪些好处?
  2. 《HBase权威指南》一导读
  3. Dubbo项目基本业务基础构建
  4. ES6环境搭建及react-router学习
  5. 信息传递(luogu 2661)
  6. 第一章 .NET的原理(2.0)
  7. Python基础十七:IO编程一:读写文件
  8. 安卓dj专业打碟机软件_学DJ打碟必备设备
  9. 计算机技术在通信应用文献,通信论文参考文献
  10. 个人主页网页设计模板
  11. Linux常用命令分享
  12. 005. C++智能指针
  13. 把握SDN研发方向,展望未来发展趋势
  14. 天池大赛-心跳信号分类预测:探索性数据分析
  15. idea修改单行注释颜色
  16. pythonista免费下载-pythonista 3ios
  17. 【互联网的恩怨情仇】盘点2015年互联网十大撕逼事件
  18. JSON parse error: Cannot construct instance of “xxx“(although at least one Creator exists)
  19. element-plus打包报错/vue3.0+vite+ts别名引入ts文件报错
  20. 五大地形等高线特征_五种基本地形特征

热门文章

  1. Android手机与PC端进行通信
  2. Codeforces 833D Red-Black Cobweb 边分治
  3. HTML 全页面内容自由编辑与 HTML 页面全部黑白风格处理
  4. 据消息称小米显示器34寸带鱼屏即将暂停销售:因为三星屏幕停止供应。
  5. 3DMAX绘室内装修三维效果图
  6. NPC内网穿透教程-入门
  7. warning: TCG doesn‘t support requested feature: CPUID.01H:ECX.vmx [bit 5]Could not initialize SDL
  8. 启动oracle时报错:PuTTY X11 proxy: unable to connect to forwarded X server: Network error:
  9. trove 基本介绍
  10. 【Verilog基础】【总线协议】AHB和AHB-Lite的区别?AMBA2.0和AMBA3.0的区别?目前常见的几种AMBA协议内容?