博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Boolean Expressions
阅读量:4935 次
发布时间:2019-06-11

本文共 3264 字,大约阅读时间需要 10 分钟。

Boolean Expressions
Time Limit: 1000MS   Memory Limit: 30000K
     

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: FExpression 2: VExpression 3: V 分析:表达式求值,中缀转后缀再求值;    当前操作符是(时,直接入栈;)时,一直出栈到(结束;!时,直接进栈,待进数时,直接出栈;其余按优先级来即可; 代码:
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,m,n) for(i=m;i<=n;i++)#define mod 1000000007#define inf 0x3f3f3f3f#define vi vector
#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)#define pii pair
#define sys system("pause")const int maxn=1e5+10;const int N=5e4+10;const int M=N*10*10;using namespace std;inline ll gcd(ll p,ll q){ return q==0?p:gcd(q,p%q);}inline ll qpow(ll p,ll q){ll f=1;while(q){ if(q&1)f=f*p;p=p*p;q>>=1;}return f;}inline void umax(ll &p,ll q){ if(p
q)p=q;}int n,m,k,t,ret[maxn],cas;char a[maxn],b[maxn],c[maxn];void gao(){ int top1=0,top2=0; for(int i=0;a[i];i++) { if(a[i]==' ')continue; if(a[i]=='(')c[++top2]=a[i]; else if(a[i]==')') { while(c[top2]!='(')b[top1++]=c[top2--]; top2--; } else if(a[i]=='!') { c[++top2]=a[i]; } else if(a[i]=='&') { while(c[top2]=='&'||c[top2]=='!')b[top1++]=c[top2--]; c[++top2]=a[i]; } else if(a[i]=='|') { while(top2&&c[top2]!='(')b[top1++]=c[top2--]; c[++top2]=a[i]; } else { b[top1++]=a[i]; while(c[top2]=='!')b[top1++]=c[top2--]; } } while(top2)b[top1++]=c[top2--]; b[top1]=0;}int main(){ int i,j; while(gets(a)) { gao(); int top=0; for(i=0;b[i];i++) { if(b[i]=='V')ret[++top]=1; else if(b[i]=='F')ret[++top]=0; else if(b[i]=='!')ret[top]^=1; else if(b[i]=='&') { ret[top-1]&=ret[top]; top--; } else { ret[top-1]|=ret[top]; top--; } } printf("Expression %d: %c\n",++cas,"FV"[ret[1]]); } return 0;}

转载于:https://www.cnblogs.com/dyzll/p/6407510.html

你可能感兴趣的文章
通过自动回复机器人学Mybatis---加强版
查看>>
点击startup.bat启动tomcat出现乱码
查看>>
BUAA_OO第二单元总结性博客作业——多线程电梯架构
查看>>
C++ 11
查看>>
高级软件工程课程总结
查看>>
MySQL对时间的处理总结
查看>>
笔记四:python乱码深度剖析二
查看>>
《PHP程序员面试笔试宝典》——如何回答技术性的问题?
查看>>
【转载】Amit’s A star Page 中译文
查看>>
GitHub Blog创建以及本地管理
查看>>
注册谷歌账号并验证时显示号码无法用于验证的问题
查看>>
hive基本操作与应用
查看>>
(C#)设计模式之状态模式
查看>>
java反射(2.0)
查看>>
详解C# 网络编程系列:实现类似QQ的即时通信程序
查看>>
Hive 变量和属性
查看>>
验证邮箱合法性的一些测试样例
查看>>
Python安装第三方库 xlrd 和 xlwt 。处理Excel表格
查看>>
课后作业-阅读任务-阅读提问-3
查看>>
Asp.Net Core 中利用QuartzHostedService 实现 Quartz 注入依赖 (DI)
查看>>