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也可以过,难道是思路错 ...
随机推荐
- WebSocket握手总结
网址:http://blog.csdn.net/edwingu/article/details/44040961 WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器 ...
- MSP430 PIN 操作寄存器
1.P口端口寄存器: (1).PxDIR 输入/输出方向寄存器 (0:输入模式 1:输出模式) (2).PxIN 输入寄存器 输入寄存器是只读寄存器,用户不能对其写入,只能通过读取该寄 ...
- 看看 SDWebImage内部基本实现过程
一.实现流程 入口 setImageWithURL:placeholderImage:options: 会先把 placeholderImage 显示,然后 SDWebImageManager 根据 ...
- 浅谈IO优化
三层结构 磁盘(存储).VM(卷管理)和文件系统.专有名词不好理解,打个比方说:磁盘就相当于一块待用的空地:LVM相当于空地上的围墙(把空地划分成多个部分):文件系统则相当于每块空地上建的楼房(决定了 ...
- E20170620-hm
dump n. 垃圾场; 仓库; 无秩序地累积; vt. 倾倒; 倾销; 丢下,卸下; 摆脱,扔弃; terminate vt. 结束; 使终结; 解雇; 到达终点站; adj. 结束的; d ...
- 2243: [SDOI2011]染色(树链剖分+线段树)
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 8400 Solved: 3150[Submit][Status ...
- $CF1141A Game 23$
这题很简单啊 可以用\(DFS\)来打 毕竟是 \(2^x*3^y=m 输出x+y啊\) 这是最简单的做法 #include <bits/stdc++.h> using namespace ...
- 【USACO2006 Mar】滑雪缆车 skilift
[USACO2006 Mar] 滑雪缆车 skilift Time Limit 1000 msMemory Limit 131072 KBytes Description 科罗拉多州的罗恩打算为奶牛建 ...
- [转]通过Net Manager 配置Oracle 11g本地监听服务(listener service)
本文转自:http://blog.csdn.net/mozart_cai/article/details/8596504 [Target] 通过ip address 监听orcl服务,而不是通过loc ...
- 学习c语言的感想
其实个人认为无论学习什么语言,最重要的是掌握习编程思想,然而C语言一种学习编程思想的基础语言.所以,C语言的重要性不言而喻. 一.课本 无论用的是什么书,要学好C语言,把书上的每一个例题.习题的代码读 ...