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<map>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const int INF=1e9+7;
const int maxn=110;
typedef long long ll; char xsk[maxn],sum[maxn];
int cnt,pos;
char c; int insert(int t)
{
while(cnt&&xsk[cnt-1]==3)
{
t=!t;
--cnt;
}
sum[pos++]=t;
} void solve()
{
int a=sum[--pos];
int b=sum[--pos];
int d=xsk[--cnt];
int ss=(a&b);
if(d==1) ss=(a|b);
insert(ss);
} int main()
{
int k=1;
while((c=getchar())!=EOF)
{
cnt=pos=0;
do
{
if(c=='(')
{
xsk[cnt++]=0;
}
else if(c==')')
{
while(cnt&&xsk[cnt-1]!=0)
solve();
--cnt;
insert(sum[--pos]);
}
else if(c=='!')
{
xsk[cnt++]=3;
}
else if(c=='&')
{
while(cnt&&xsk[cnt-1]>=2)
solve();
xsk[cnt++]=2;
}
else if(c=='|')
{
while(cnt&&xsk[cnt-1]>=1)
solve();
xsk[cnt++]=1;
}
else if(c=='V'||c=='F')
{
insert(c=='V'?1:0);
}
}
while((c=getchar())!='\n'&&c!=EOF);
while(cnt) solve();
printf("Expression %d: %c\n",k++,(sum[0]?'V':'F'));
}
return 0;
} //递归,看的别人的代码,能看就看,不懂就算了
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[100001]= {0};
int my_cnt=0;
bool expression_bool();
bool term_bool();
bool factor_bool(); bool expression_bool() //求一个表达式的值
{
bool result=term_bool();//求第一项的值
bool more=true;
while(more)
{
char op=s[my_cnt];//看一个字符,不取走
if(op=='&'||op=='|')
{
my_cnt++;//从数组中取走一个字符
bool value=term_bool();
if(op=='&') result=result&value;
else result=result|value;
}
else
{
more=false;
}
}
return result;
} bool term_bool() //因为!的运算优先级更高,所以把!xxx也当成一个项
{
bool result;
char op=s[my_cnt];
if(op=='!')
{
my_cnt++;
result=!factor_bool();
}
else
{
result=factor_bool();
} return result;
} bool factor_bool() //求一个因子的值
{
bool result;
char c=s[my_cnt];
if(c=='(') //如果该因子是由括号括起来的表达式组成的话
{
my_cnt++;
result=expression_bool();
my_cnt++;
}
else if(c=='V')
{
result=true;
my_cnt++;
}
else if(c=='F')
{
result=false;
my_cnt++;
}
else if(c=='!') //出现了!,说明读取到了一个因子
{
result=term_bool();
} return result;
} int main()
{
int k=0;
while(cin.getline(s,100000))
{
char t[100001]= {0};
int len=strlen(s);
for(int i=0,k=0; i<len; ++i)
{
if(s[i]!=' ')
{
t[k++]=s[i];
}
} len=strlen(t);
for(int i=0; i<len; ++i)
{
s[i]=t[i];
}
s[len]='\0';
//到这里输入中的空格已经被去除干净了
cout<<"Expression "<<++k<<": "<<(expression_bool()?"V":"F")<<endl;
//初始化工作
my_cnt=0;
memset(s,0,100000);
}
return 0;
}

Boolean Expressions POJ - 2106 (表达式求值)的更多相关文章

  1. 利用Z.Expressions.Eval表达式求值

    Z.Expression.Eval是一个开源的(OpenSource),可扩展的(Extensible),超轻量级(Super lightweight)的公式化语言解析执行工具包. 使用方法:1.从n ...

  2. 利用栈实现算术表达式求值(Java语言描述)

    利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...

  3. java实现算术表达式求值

    需要根据配置的表达式(例如:5+12*(3+5)/7.0)计算出相应的结果,因此使用java中的栈利用后缀表达式的方式实现该工具类. 后缀表达式就是将操作符放在操作数的后面展示的方式,例如:3+2 后 ...

  4. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  5. Aviator 表达式求值引擎开源框架

    简介¶ Aviator是一个高性能.轻量级的java语言实现的表达式求值引擎,主要用于各种表达式的动态求值.现在已经有很多开源可用的java表达式求值引擎,为什么还需要Avaitor呢? Aviato ...

  6. 蓝桥杯算法训练 java算法 表达式求值

    问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的值. 样例输入 1-2+3*(4-5) 样例输出 - ...

  7. leetcode算法学习----逆波兰表达式求值(后缀表达式)

    下面题目是LeetCode算法:逆波兰表达式求值(java实现) 逆波兰表达式即后缀表达式. 题目:  有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式.同 ...

  8. 表达式求值(noip2015等价表达式)

    题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...

  9. 用Python3实现表达式求值

    一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...

随机推荐

  1. 《JavaScript 实战》:JavaScript 实现拖拽缩放效果

    拖拉缩放效果,实现通过鼠标拖动来调整层的面积(宽高)大小,例如选框效果.这里的拖拉缩放比一般的选框复杂一点,能设置八个方位(方向)的固定触发点,能设置最小范围,最大范围和比例缩放. 跟拖放效果一样,程 ...

  2. R1(下)—数据挖掘—关联规则理论介绍与R实现

    Apriori algorithm是关联规则里一项基本算法.是由Rakesh Agrawal和Ramakrishnan Srikant两位博士在1994年提出的关联规则挖掘算法.关联规则的目的就是在一 ...

  3. LintCode之二叉树的最大节点

    分治问题,可以把整棵树看做是由一颗颗只有三个节点组成的小树,一颗树的构成是根节点.左子树.右子树,这样只需要从左子树找出一个最大的节点,从右子树找出一个最大的节点,然后与根节点三个取个最大的,就是最终 ...

  4. 对Feign的请求url 重写

    需求:对当前请求的 url 重新构建 debug feign 的执行可知,重写 LoadBalancerFeignClient 类中的 execute 方法即可控制当前请求的url 代码分析 当引入  ...

  5. perl6正则 3: 行开头与结尾与多行开头,多行结尾

    ^ $ 匹配一行的开头或结尾, 可以用 ^ 或 $. > so 'abcde' ~~ /e$/ True > so 'abcdef' ~~ /e$/ False > so 'abcd ...

  6. onvif客户端

    前言 做开发有8年时间了,ffmpeg和onvif与我是特别有缘的了(说着玩的,我更认为是因为他们确实强大^_^). ffmpeg在毕业设计时就有用到,5年后做windows.linux播放库时又有用 ...

  7. Tslib触摸屏官网【转】

    转自:https://github.com/kergoth/tslib C library for filtering touchscreen events tslib consists of the ...

  8. PSQueue队列操作

    队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈(FILO,First In Last Out,先进后出)属于线性表一样,队 ...

  9. .pnts点云

    一种3d tiles格式 MIME格式: <configuration> <system.webServer> <staticContent> <remove ...

  10. POJ 2186 Popular cows(Kosaraju+强联通分量模板)

    题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...