/*
* POJ_2106.cpp
*
* Created on: 2013年10月30日
* Author: Administrator
*/ #include <iostream>
#include <cstdio> using namespace std; const int maxn = 110; int op[maxn], otop;
int val[maxn],vtop; void insert(int b){//将操作数b压入操作数栈val
while(otop && op[otop - 1] == 3){//在压入之前对b进行!运算
b = !b;
--otop;
} val[vtop++] = b;
} void calc(){//进行双目运算
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(){
char c; int counter = 1;
while((c = getchar()) != EOF){//要理解并记住这种形式的输入输出处理
otop = 0;
vtop = 0; do{
if( c == '('){
op[otop++] = 0;
}else if(c == ')'){//处理)内的所有运算,结果压入val栈
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){//将op栈中所有优先级比&的运算符出栈进行运算
calc();
} op[otop++] = 2;//将&压入op栈
}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){//将op栈中的所有元素出栈进行运算
calc();
} printf("Expression %d: ",counter++);
printf(val[0]?"V":"F");
printf("\n");
} return 0;
}

(栈的应用5.2.2)POJ 2106 Boolean Expressions(表达式求值)的更多相关文章

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

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

  2. POJ 2106 Boolean Expressions

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

  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 课本代码

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

  5. 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值

    一.简介 迷宫求解:类似图的DFS.具体的算法思路可以参考书上的50.51页,不过书上只说了粗略的算法,实现起来还是有很多细节需要注意.大多数只是给了个抽象的名字,甚至参数类型,返回值也没说的很清楚, ...

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

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

  7. 数据结构--栈的应用(表达式求值 nyoj 35)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...

  8. 【NYOJ-35】表达式求值——简单栈练习

    表达式求值 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...

  9. 表达式求值 (栈) 用C++实现

    #include <cstdio> #include <cstdlib> #include <cmath> #include <stack> #incl ...

随机推荐

  1. php-001-win7 环境下 wamp 的至简部署

    此文主要记录学习 PHP 开发环境 wamp 的至简部署.敬请各位小主参阅,若有不足之处,敬请大神指正,不胜感激! 首先依据自身的操作系统选择,进入 wamp 的网站 :http://www.wamp ...

  2. Windows-006-映射网络驱动器图文详解

    此文主要讲述 Win7 中,如何映射网络驱动器,一般用于网络共享时.敬请亲们参阅,若有不足之处,敬请大神指正,不胜感激! 打开计算机,选择工具栏中的 映射网络驱动器,依据下图中的操作进行映射网络驱动器 ...

  3. Eclipse代码风格

    1.代码对齐风格:project...properties...Java Code Style...Formatter...Brance

  4. Linux系统产生随机数的3种方法

    Linux系统产生随机数的3种方法 方法一:生成8位随机数 [root@localhost ~]# echo "$RANDOM$(date +%N%t)" | md5sum | c ...

  5. 设置tomcat的编码为utf-8

    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" ...

  6. 微信公众平台自定义菜单及高级接口PHP SDK

    本文介绍介绍微信公众平台自定义菜单及高级接口的PHP SDK及使用方法. 作者 方倍工作室 修正记录: 2014.05.03 v1.0 方倍工作室 http://www.cnblogs.com/txw ...

  7. Linux 内核的文件 Cache 管理机制介绍

    Linux 内核的文件 Cache 管理机制介绍 http://www.ibm.com/developerworks/cn/linux/l-cache/ 1 前言 自从诞生以来,Linux 就被不断完 ...

  8. C# 键盘钩子类

    键盘钩子类代码如下 class globalKeyboardHook { #region Constant, Structure and Delegate Definitions /// <su ...

  9. Windows 7 / Windows 10 安装 IPX/SPX

    以我的系统为例: Windows 7/10 x64 首先下载 NWLINK IPX/SPX 驱动(这是 Microsoft 对 IPX/SPX 的实现.) http://pan.baidu.com/s ...

  10. APN 推送

    推送的各种状态 http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled ...