Boolean Expressions POJ - 2106 (表达式求值)
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 (表达式求值)的更多相关文章
- 利用Z.Expressions.Eval表达式求值
Z.Expression.Eval是一个开源的(OpenSource),可扩展的(Extensible),超轻量级(Super lightweight)的公式化语言解析执行工具包. 使用方法:1.从n ...
- 利用栈实现算术表达式求值(Java语言描述)
利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...
- java实现算术表达式求值
需要根据配置的表达式(例如:5+12*(3+5)/7.0)计算出相应的结果,因此使用java中的栈利用后缀表达式的方式实现该工具类. 后缀表达式就是将操作符放在操作数的后面展示的方式,例如:3+2 后 ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- Aviator 表达式求值引擎开源框架
简介¶ Aviator是一个高性能.轻量级的java语言实现的表达式求值引擎,主要用于各种表达式的动态求值.现在已经有很多开源可用的java表达式求值引擎,为什么还需要Avaitor呢? Aviato ...
- 蓝桥杯算法训练 java算法 表达式求值
问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的值. 样例输入 1-2+3*(4-5) 样例输出 - ...
- leetcode算法学习----逆波兰表达式求值(后缀表达式)
下面题目是LeetCode算法:逆波兰表达式求值(java实现) 逆波兰表达式即后缀表达式. 题目: 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式.同 ...
- 表达式求值(noip2015等价表达式)
题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...
- 用Python3实现表达式求值
一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...
随机推荐
- 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法
[题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...
- js父页面和子页面相互取值
iframe子页面与父页面通信根据iframe中src属性是同域链接还是跨域链接,通信方式也不同. 一.同域下父子页面的通信 父页面parent.html <html> <head& ...
- NYOJ 129 树的判定 (并查集)
题目链接 描述 A tree is a well-known data structure that is either empty (null, void, nothing) or is a set ...
- python初步学习-面向对象之类(一)
python 面向对象 python 从设计之初就已经是一门面向对象的语言,正因为如此,在python中创建一个类和对象是很容易的. 对象对象奇数简介 类(Class): 用于描述具有相同的属性和方法 ...
- windows+python3.6下安装fasttext+fasttext在win上的使用+gensim(fasttext)
真是坑了好久,faxttext对win并不是很友好,所以遇到了很多坑,记录下来,以供大家少走弯路. 法1:刚开始直接用pip install fasttext,最后一直报下面这个错误 “error:M ...
- Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure y
这个应该是很多github新手经常出错的问题,这个就是没有在你github上添加一个公钥. 下面就直接说步骤: 1 可以用 ssh -T git@github.com去测试一下 图上可以明显看出缺少了 ...
- weblogic 开启注意问题
1.关闭防火墙 service iptables stop chkconfig iptables off 2.weblogic unable to get file lock问题 我的解决办法是ps ...
- [004] last_k_node
[Description] find the k-th node from the last node of single linked list. e.g. Linked-list: 1-2-3-4 ...
- Ubuntu 17.10 安装 “爱壁纸” 时,缺失了 python-support 依赖
Ubuntu 17.10 安装 "爱壁纸" 的 deb 包时,缺失了 python-support 依赖.使用 sudo apt-get -f install 也没修复.查了下官 ...
- python爬虫多线程编程
#使用了线程库 import threading from queue import Queue from bs4 import BeautifulSoup import json import re ...