STL栈的应用之表达式求值
#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack> using namespace std; /*************
*计算
*************/ int operate(int a, char op, int b ){
if(op == '+') return a+b;
else if(op == '-') return a-b;
else if(op == '*') return a*b;
else if(op == '/') return a/b;
} /**************
*比較优先级
**************/ char precede(char a, char b){
if(a=='+' || a=='-'){
if(b=='*' || b=='/' || b=='(') return '<';
else if(b==')' || b=='+'
|| b=='-' || b=='#') return '>';
} else if(a=='*' || a=='/'){
if(b=='(') return '<';
else if(b=='+' || b=='-' || b=='*'
|| b=='/' || b==')' || b=='#') return '>';
} else if(a=='('){
if(b=='+' || b=='-' || b=='*'
|| b=='/' || b=='(') return '<';
else if(b==')') return '=';
} else if(a==')'){
if(b=='+' || b=='-' || b=='*'
|| b=='/' || b=='#' || b==')') return '>';
} else if(a=='#'){
if(b=='+' || b=='-' || b=='*' ||
b=='/' || b=='#' || b=='(') return '<';
}
} /**********************
*推断是否为数字字符
**********************/ bool judge(char a){
if(a>='0' && a<='9')return true;
else return false;
} /***************
*Main函数
***************/ int main()
{
stack<int>op_n;
stack<char>op_s;
char str, ans;
int num = 0;
bool flag = false;
op_s.push('#');
cin >> str; while(str!='#' || op_s.top()!='#'){
if(judge(str)){
flag = true;
num = num*10 + (str-'0'); /*处理多位数数据*/
cin >> str;
}else{
if(flag){
flag = false;
op_n.push(num);
num = 0;
}
ans = precede(op_s.top(), str);
if(ans == '<'){
op_s.push(str);
cin>>str;
} if(ans == '='){
op_s.pop();
cin >> str;
} if(ans == '>'){
int a;int b;char c;
b = op_n.top();op_n.pop();
a = op_n.top();op_n.pop();
c = op_s.top();op_s.pop();
cout << a << ' ' << c
<< ' ' << b << endl;
op_n.push( operate(a, c, b) );
}
}
}
cout << op_n.top() << endl;
return 0;
}
STL栈的应用之表达式求值的更多相关文章
- 第四章 栈与队列(c4)栈应用:中缀表达式求值
- C++练习 | 基于栈的中缀算术表达式求值(double类型
#include<iostream> #include<stack> #include<cmath> using namespace std; char ch; b ...
- 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值
一.简介 迷宫求解:类似图的DFS.具体的算法思路可以参考书上的50.51页,不过书上只说了粗略的算法,实现起来还是有很多细节需要注意.大多数只是给了个抽象的名字,甚至参数类型,返回值也没说的很清楚, ...
- 利用栈实现算术表达式求值(Java语言描述)
利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...
- 数据结构--栈的应用(表达式求值 nyoj 35)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...
- 【NYOJ-35】表达式求值——简单栈练习
表达式求值 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...
- 表达式求值 (栈) 用C++实现
#include <cstdio> #include <cstdlib> #include <cmath> #include <stack> #incl ...
- 表达式求值(栈方法/C++语言描述)(二)
上篇中完成了对表达式求值的整体过程,接下来看看如何处理不同类型的token. 对运算数的处理比较简单,它直接调用函数strtod(),将字符串中的运算数转换为浮点类型并将它压入运算数栈中: void ...
- 第四届河南省ACM 表达式求值 栈
表达式求值 时间限制: 1 Sec 内存限制: 128 MB 提交: 14 解决: 7 [提交][状态][讨论版] 题目描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简 ...
随机推荐
- 由字符串反转(使用递归)引申出来一道Java面试题
如何面试一个从事编程工作的开发人员既困难又乏味,幸好还有很多值得参考的指南,比如:<Joel Guerilla Guide to interviewing>,但最后雇佣与否,还得由你自己决 ...
- 【LeetCode two_pointer】11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 「Codechef April Lunchtime 2015」Palindromeness
「Codechef April Lunchtime 2015」Palindromeness 解题思路 : 考虑对于回文子串 \(s\) 贡献的定义: \[ value_s = [\ s[1,\lflo ...
- [BZOJ4890][TJOI2017]城市(DP)
题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达,但是通过一条高速公路需要收 ...
- MODI出现ORC RUNNING ERROR的解决方法
stackflow都没个靠谱的说法,最后还是csdn上看到的.转一个备用.http://bbs.csdn.net/topics/390135443 由于直接执行Mircosoft Office Doc ...
- [CSAcademy]Squared Ends
[CSAcademy]Squared Ends 题目大意: 给你一个长度为\(n(n\le10^4)\)的数列\(\{A_i\}(A_i\le10^6)\).定义区间\(A_{[l,r]}\)的代价为 ...
- ZOJ 3624 Count Path Pair 排列组合
思路:在没有限制条件时,很容易知道结果为C(m+n,n)*C(m+q-p,q). 然后再把相交的情况去除就可以了.而如果想到了就是水题了…… 求A->D,B->C相交的情况可以转化为求A- ...
- lightoj 1306 - Solutions to an Equation 扩展的欧几里得
思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...
- [转]Android网格视图(GridView)
GridView的一些属性: 1.android:numColumns=”auto_fit” //GridView的列数设置为自动,也可以设置成2.3.4…… 2.android:columnWi ...
- UESTCACM 每周算法讲堂 延迟标记+bfs dfs搜索入门
http://www.bilibili.com/video/av4163472/ 地址在上面~