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. JAVA多线程提高四:多个线程之间共享数据的方式

    多个线程访问共享对象和数据的方式 如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 如果每个线程执行的代码不同,这 ...

  2. 【不能继续浪啦】BZ做题记录[7.01~7.06]

    距离上次提交..><居然已经过去一个半月了... 然后再去看看人家RXDoi.. 差距越来越大啦... 最后更新时间:7.06 19:06 [07.03 21:02]夏令营自修课逃逃真爽. ...

  3. Windows Live Writer博客草稿迁移的一种解决方案

    作为一个苦逼的码农,喜欢写博客做总结是很正常的事,写博客写的久的人都接触过各种客户端工具,最流行的就是Windows Live Writer了. 作为一个苦逼的码农,换电脑也是很经常的事,经常会出现一 ...

  4. let块级作用域

    let是es6中新加的作用域,即块级作用域. var申明的变量要么全局,要么函数级,而let允许把变量的作用域限制在块级域中,这里的块级可以是()内,或{}内. 示例: code_1: "u ...

  5. Django之前端插件定制之表头

    什么是插件? 插件只是辅助,是开发过程中的一个阶段.一般项目一期会用各种插件,迅速将功能.界面搭出来,二期时就改成自己的代码了.大点的公司都有自己的js库,自己开发类似jquery的库. 那接下来就写 ...

  6. 给vim安装YouCompleteMe

    要安装YouCompleteMe ,vim须支持python.看是否支持,可以在vim中:version 查看, 如果python前有+号,就是支持,减号就是不支持. 如果不支持,需要以编译安装方式重 ...

  7. Xcode 9安装

    Xcode 9 Xcode 9 Compatibility Xcode 9 requires a Mac running macOS 10.13.2 or later. Xcode 9 include ...

  8. linux内核sysfs详解【转】

    转自:http://blog.csdn.net/skyflying2012/article/details/11783847 "sysfs is a ram-based filesystem ...

  9. linux系统定时任务设置

    .使用at命令设置一次性定时任务 2.使用crontab设置周期性定时任务 1)cd /home 目录下,使用vi test.py创建文件,内容如下: #!/usr/bin/python#coding ...

  10. curl 发送请求的时候报错

    AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see ...