poj 2106 Boolean Expressions 课本代码

#include<cstdio>
const int maxn=100 +10;

int val[maxn],vtop;
int op[maxn],otop;

void insert(int b)
{
	while(otop &&op[otop-1]==3)
	{
		b=!b;
		--otop;
	}
	val[vtop++]=b;
}

void calc(void)
{
	int b=val[--vtop];
	int a=val[--vtop];
	int opr=op[--otop];

	int c=(a&b);
	if(opr==1)
		c=(a|b);
	insert(c);
}

int main(void)
{
	int loop=0;

	char c;
	while((c=getchar())!=EOF)
	{
		vtop=otop=0;

		do
		{
			if(c==‘(‘)
			{
				op[otop++]=0;
			}
			else if(c==‘)‘)
			{
				while(otop&&op[otop-1]!=0)
					calc();
				--otop;
				insert(val[--vtop]);//  当表达式是!()时,这一句是计算完 () 后,计算 前面的 ! 的
			}
			else if(c==‘!‘)
			{
				op[otop++]=3;
			}
			else if(c==‘&‘)
			{
				while(otop&&op[otop-1]>=2)
					calc();
				op[otop++]=2;
			}
			else if(c==‘|‘)
			{
				while(otop&&op[otop-1]>=1)
					calc();
				op[otop++]=1;
			}
			else if(c==‘V‘||c==‘F‘)
			{
				insert(c==‘V‘?1:0);
			}
		}
		while((c=getchar())!=‘\n‘&&c!=EOF);
		while(otop)
			calc();

		printf("Expression %d: %c\n",++loop,(val[0]?‘V‘:‘F‘));
	}
	return 0;

}

poj 2106 Boolean Expressions 课本代码

时间: 2024-07-31 10:54:33

poj 2106 Boolean Expressions 课本代码的相关文章

[poj 2106] Boolean Expressions 递归

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 ope

POJ 2106 Boolean Expressions

Boolean Expressions Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3665   Accepted: 1104 Description The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next: Expression: ( V | V ) &

POJ 2106 Boolean Expression 表达式求值

题意:给出布尔表达式求值? 插入数字时,若有!则更新.遇到右括号弹出知道左括号,左括号前有'!'则更新, 其余和中缀表达式一样,遇到下一个运算符时 若操作栈中运算符优先级大,则先算. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <s

Boolean Expressions

总时间限制: 1000ms  内存限制: 65536kB 描述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 includ

005:Boolean Expressions

描述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: !

poj 2106

原题链接:http://poj.org/problem?id=2106 题意:或.与. 非的多元表达式的求值: 思路:中缀表达式变为后缀表达式: 代码: 1 #include<cstdio> 2 #include<iostream> 3 #include<stack> 4 #include<string> 5 6 using namespace std; 7 8 struct Pri{ 9 char op; 10 int pri; 11 }lpri[5]={

POJ 1258 Agri-Net(Prim算法)

题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> #include<cstring> using namespace std; int n; int tot; int v[150][150]; int dist[150];//存 节点到树 的最小距离 bool use[150];//标记节点是否存在 int main(){ while(sca

POJ 2421 Constructing Roads(Kruskal算法)

题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostream> using namespace std; int fa[120]; int get_father(int x){ return fa[x]=fa[x]==x?x:get_father(fa[x]);//判断两个节点是否属于一颗子树(并查集) } int main(){ int n; int p[

【重构.改善既有代码的设计】9、简化条件表达式

简化条件表达式 Decompose Conditional(分解条件式) 你有一个复杂的条件(if-then-else)语句. 从if.then.else 三个段落中分别提炼出独立函数. 分解为多个独立函数,根据每个小块代码的用 途,为分解而得的新函数命名,并将原函数中对应的代码替换成「对新建函数的调用」,从而更清楚地表达自己的意图. 对于条件逻辑,[将每个分支条件分解,形成新函数」还可以给你带来更多好处:可以突出条件逻辑,更清楚地表明每个分支的作用,并且突出每个分支的原因. Consolida