/*
* 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. Selenium2学习-027-WebUI自动化实战实例-025-JavaScript 在 Selenium 自动化中的应用实例之三(页面滚屏,模拟鼠标拖动滚动条)

    日常的 Web UI 自动化测试过程中,get 或 navigate 到指定的页面后,若想截图的元素或者指定区域范围不在浏览器的显示区域内,则通过截屏则无法获取相应的信息,反而浪费了无畏的图片服务器资 ...

  2. JS-面向对象-封装

    --参考文献: --http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html --js面 ...

  3. js实例代码

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. POJ2262问题描述

    Goldbach's Conjecture   Description In 1742, Christian Goldbach, a German amateur mathematician, sen ...

  5. 求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m

    private static int fun(int m) { ; ; i <= m; i++) { == ) temp = temp + i; else temp = temp - i; } ...

  6. 一般处理程序使用Session的方法

    1 引用这个命名空间 using System.Web.SessionState; 2 实现这两个接口中的任何一个 IReadOnlySessionState   //此接口只能使用session,无 ...

  7. Android Framework框架_转

    选自<Android内核剖析> Framework定义了客户端组件和服务端组件功能及接口.包含3个主要部分:服务端,客户端和Linux驱动. (一)服务端 服务端主要包含两个重要类,分别是 ...

  8. [转]qt中文乱码问题

    http://blog.csdn.net/brave_heart_lxl/article/details/7186631#

  9. ASP.NET MVC Filter

    http://stackoverflow.com/questions/6940371/asp-net-mvc-request-filters http://afana.me/post/aspnet-m ...

  10. [Android Tips] 3. Launch CallLog Activity

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(android.provider.CallLog.Calls.CONTEN ...