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: 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的更多相关文章

  1. POJ | Boolean Expressions

    总时间限制: 1000ms  内存限制: 65536kB 描述The objective of the program you are going to produce is to evaluate ...

  2. Boolean Expressions POJ - 2106 (表达式求值)

    The objective of the program you are going to produce is to evaluate boolean expressions as the one ...

  3. [poj 2106] Boolean Expressions 递归

    Description The objective of the program you are going to produce is to evaluate boolean expressions ...

  4. POJ 2106 Boolean Expressions

    总时间限制: 1000ms  内存限制: 65536kB 描述 The objective of the program you are going to produce is to evaluate ...

  5. shorthand trick with boolean expressions

    https://stackoverflow.com/questions/2802055/what-does-the-construct-x-x-y-mean --------------------- ...

  6. (栈的应用5.2.2)POJ 2106 Boolean Expressions(表达式求值)

    /* * POJ_2106.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...

  7. POJ 2106 Boolean Expressions (布尔表达式求值)

    题意:关于!,&,| 的运算,表达式中V代表true,F代表false. 思路:见代码吧,很详细了. 要注意 !!!F,!(...) 的情况. #include <iostream> ...

  8. poj 2106 Boolean Expressions 课本代码

    #include<cstdio> const int maxn=100 +10; int val[maxn],vtop; int op[maxn],otop; void insert(in ...

  9. POJ 2106-Boolean Expressions,双栈运用类似表达式求值!

    Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错 ...

随机推荐

  1. HTTP协议下可拖动时间轴播放FLV的实现(伪流媒体)

    HTTP协议下实现FLV的播放其实并不复杂,当初实现的原理是使用了flowPlayer插件实现的,效果还不错.但仍有两大问题影响着客户的访问情绪: 1.预加载时页面卡死,似乎没有边下边播. 2.偶尔边 ...

  2. hibernate缓存机制详细分析(一级、二级、查询缓存,非常清晰明白)

    本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题. 随笔虽长,但我相信 ...

  3. 91. Ext中获取combobox中的valueField和displayField的值

    转自:https://blog.csdn.net/jcy472578/article/details/42113119Ext.getCmp("schemaVersion").val ...

  4. Django - 自定义请求头

    收藏一下以后学习 博客搬运地址 Django接收自定义http header(转)

  5. [App Store Connect帮助]二、 添加、编辑和删除用户(1)用户帐户和职能概述

    您可以在 App Store Connect 的“用户和访问”部分管理用户.添加沙盒测试员以及管理 API 密钥. 用户职能决定了用户对 App Store Connect 和 Apple Devel ...

  6. Educational Codeforces Round 24 题解

    A: 考你会不会除法 //By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long long ...

  7. ACM_统计字符串

    统计字符串 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定n个字符串,统计字符串的个数. 如给定 5 sss ab sss ...

  8. bootstrap 字体颜色 对齐方式

    一:字体代码:作用--颜色 1..text-muted:提示--浅灰色 2..text-primary:主要--蓝色 3..text-success:成功--浅绿色 4..text-info:     ...

  9. Android热修复方案比较

    热修复的特点:无需重新发版,实时高效热修复:用户无感知修复,无需下载新的应用,代价小: 修复成功率高,把损失降到最低. 一.热修复开源方案和使用情况 方案名称 方案开发公司 开发时间 Github星评 ...

  10. Android:用签名打包后微信分享失效

    刚开始使用微信分享,申请的微信appid也可以在直接使用,分享成功! 当我使用自己的签名打包分享时却分享失败,一闪而过,好郁闷的说,为什么之前没有打包就可以,签名打包后就不可以了... 开始查找各种资 ...