Boolean Expressions
Time Limit: 1000MS | Memory Limit: 30000K | |
Description
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 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
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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#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<int,int>
#define sys system("pause")
const int maxn=1e5+;
const int N=5e4+;
const int M=N**;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(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=,top2=;
for(int i=;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]=;
}
int main()
{
int i,j;
while(gets(a))
{
gao();
int top=;
for(i=;b[i];i++)
{
if(b[i]=='V')ret[++top]=;
else if(b[i]=='F')ret[++top]=;
else if(b[i]=='!')ret[top]^=;
else if(b[i]=='&')
{
ret[top-]&=ret[top];
top--;
}
else
{
ret[top-]|=ret[top];
top--;
}
}
printf("Expression %d: %c\n",++cas,"FV"[ret[]]);
}
return ;
}
Boolean Expressions的更多相关文章
- POJ | Boolean Expressions
总时间限制: 1000ms 内存限制: 65536kB 描述The objective of the program you are going to produce is to evaluate ...
- Boolean Expressions POJ - 2106 (表达式求值)
The objective of the program you are going to produce is to evaluate boolean expressions as the one ...
- [poj 2106] Boolean Expressions 递归
Description The objective of the program you are going to produce is to evaluate boolean expressions ...
- POJ 2106 Boolean Expressions
总时间限制: 1000ms 内存限制: 65536kB 描述 The objective of the program you are going to produce is to evaluate ...
- shorthand trick with boolean expressions
https://stackoverflow.com/questions/2802055/what-does-the-construct-x-x-y-mean --------------------- ...
- (栈的应用5.2.2)POJ 2106 Boolean Expressions(表达式求值)
/* * POJ_2106.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...
- POJ 2106 Boolean Expressions (布尔表达式求值)
题意:关于!,&,| 的运算,表达式中V代表true,F代表false. 思路:见代码吧,很详细了. 要注意 !!!F,!(...) 的情况. #include <iostream> ...
- poj 2106 Boolean Expressions 课本代码
#include<cstdio> const int maxn=100 +10; int val[maxn],vtop; int op[maxn],otop; void insert(in ...
- POJ 2106-Boolean Expressions,双栈运用类似表达式求值!
Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错 ...
随机推荐
- AAC的ADTS头文件信息介绍
遵循:BY-SA 署名-相同方式共享 4.0协议 作者:谭东 时间:2016年10月28日 环境:Windows 7 ADTS是Audio Data Transport Stream的简称. ...
- astgo 老版本免激活完整安装包带安装命令脚本
astgo是个国产的老牌经典软交换服务器,主要用来当回拨网络电话服务端,同时也具备群呼.传真等功能! 这个需要安装在centos 5.x 32位系统.带安装脚本,上传到root目录后执行安装脚本即可等 ...
- [Swift通天遁地]四、网络和线程-(7)检测服务器接口的访问状态:验证请求结果和可访问性
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 【转】Java中特殊的String类型
Java中String是一个特殊的包装类数据有两种创建形式: String s = "abc"; String s = new String("abc"); 第 ...
- jmeter关联、下载文件、简单压测
关联 一.什么是关联 关联是请求与请求之间存在数据依赖关系,需要从上一个请求获取下一个请求需要回传回去的数据. 简单地说就是在测试过程中有些数据的值会经常发生变化,要获取并使用这些数据,把这个动态的信 ...
- Oracle 10g RAC的负载均衡配置[转载]
Oracle 10g RAC的负载均衡配置 负载均衡是指连接的负载均衡.RAC的负载均衡主要是指新会话连接到RAC数据库时,如何判定这个新的连接要连到哪个节点进行工作.在RAC中,负载均衡分为两种,一 ...
- 大数据~说说ZooKeeper
一些概念 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase和Kafka重要组件.它是一个为分布式应用提供一致性 ...
- 查看当前linux版本
lsb_release -a 如果命令不存在,则yum安装 yum install redhat-lsb
- 将class类对象转化成json的数据格式
直接上代码: JSONObject的的使用需要导入json-lib-2.4-jdk15.jar包,下载地址:http://mvnrepository.com/artifact/net.sf.json- ...
- java学习笔记_序列化
如果父类没有实现Serializable接口,子类实现了Serializable接口,那么子类是可以序列化的. 但是如果想要反序列化,那么就需要父类支持默认构造函数. 因为在反序列化的过程中不会调用子 ...