1.  
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<stack>
  6.  
  7. using namespace std;
  8.  
  9. /*************
  10. *计算
  11. *************/
  12.  
  13. int operate(int a, char op, int b ){
  14. if(op == '+') return a+b;
  15. else if(op == '-') return a-b;
  16. else if(op == '*') return a*b;
  17. else if(op == '/') return a/b;
  18. }
  19.  
  20. /**************
  21. *比較优先级
  22. **************/
  23.  
  24. char precede(char a, char b){
  25. if(a=='+' || a=='-'){
  26. if(b=='*' || b=='/' || b=='(') return '<';
  27. else if(b==')' || b=='+'
  28. || b=='-' || b=='#') return '>';
  29. }
  30.  
  31. else if(a=='*' || a=='/'){
  32. if(b=='(') return '<';
  33. else if(b=='+' || b=='-' || b=='*'
  34. || b=='/' || b==')' || b=='#') return '>';
  35. }
  36.  
  37. else if(a=='('){
  38. if(b=='+' || b=='-' || b=='*'
  39. || b=='/' || b=='(') return '<';
  40. else if(b==')') return '=';
  41. }
  42.  
  43. else if(a==')'){
  44. if(b=='+' || b=='-' || b=='*'
  45. || b=='/' || b=='#' || b==')') return '>';
  46. }
  47.  
  48. else if(a=='#'){
  49. if(b=='+' || b=='-' || b=='*' ||
  50. b=='/' || b=='#' || b=='(') return '<';
  51. }
  52. }
  53.  
  54. /**********************
  55. *推断是否为数字字符
  56. **********************/
  57.  
  58. bool judge(char a){
  59. if(a>='0' && a<='9')return true;
  60. else return false;
  61. }
  62.  
  63. /***************
  64. *Main函数
  65. ***************/
  66.  
  67. int main()
  68. {
  69. stack<int>op_n;
  70. stack<char>op_s;
  71. char str, ans;
  72. int num = 0;
  73. bool flag = false;
  74. op_s.push('#');
  75. cin >> str;
  76.  
  77. while(str!='#' || op_s.top()!='#'){
  78. if(judge(str)){
  79. flag = true;
  80. num = num*10 + (str-'0'); /*处理多位数数据*/
  81. cin >> str;
  82. }else{
  83. if(flag){
  84. flag = false;
  85. op_n.push(num);
  86. num = 0;
  87. }
  88. ans = precede(op_s.top(), str);
  89. if(ans == '<'){
  90. op_s.push(str);
  91. cin>>str;
  92. }
  93.  
  94. if(ans == '='){
  95. op_s.pop();
  96. cin >> str;
  97. }
  98.  
  99. if(ans == '>'){
  100. int a;int b;char c;
  101. b = op_n.top();op_n.pop();
  102. a = op_n.top();op_n.pop();
  103. c = op_s.top();op_s.pop();
  104. cout << a << ' ' << c
  105. << ' ' << b << endl;
  106. op_n.push( operate(a, c, b) );
  107. }
  108. }
  109. }
  110. cout << op_n.top() << endl;
  111. return 0;
  112. }

STL栈的应用之表达式求值的更多相关文章

  1. 第四章 栈与队列(c4)栈应用:中缀表达式求值

  2. C++练习 | 基于栈的中缀算术表达式求值(double类型

    #include<iostream> #include<stack> #include<cmath> using namespace std; char ch; b ...

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

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

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

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

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

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

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

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

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

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

  8. 表达式求值(栈方法/C++语言描述)(二)

    上篇中完成了对表达式求值的整体过程,接下来看看如何处理不同类型的token. 对运算数的处理比较简单,它直接调用函数strtod(),将字符串中的运算数转换为浮点类型并将它压入运算数栈中: void ...

  9. 第四届河南省ACM 表达式求值 栈

    表达式求值 时间限制: 1 Sec  内存限制: 128 MB 提交: 14  解决: 7 [提交][状态][讨论版] 题目描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简 ...

随机推荐

  1. 小程序登陆遇到 ERR_REQUEST_PARAM

    小程序测试环境突然登陆不上,返回的错误信息是{}"code":-1,"error":"ERR_REQUEST_PARAM"}. 小程序登陆代 ...

  2. Visual Studio 2017为Android APK包签名

    Visual Studio 2017为Android APK包签名   为Android APK包签名,可以保证后期的App顺利升级.在Visual Studio 2015中,IDE会自动生成两个AP ...

  3. AtcoderGrandContest 005 F. Many Easy Problems

    $ >AtcoderGrandContest \space 005 F.  Many Easy Problems<$ 题目大意 : 有一棵大小为 \(n\) 的树,对于每一个 \(k \i ...

  4. 【tarjan】BZOJ2140-稳定婚姻

    又名NTR的故事 [题目大意] n对夫妻Bi和Gi.若某男Bi与某女Gj曾经交往过,他们有私奔的可能性.不妨设Bi和Gj旧情复燃,进而Bj会联系上了他的初恋情人Gk,以此递推.若在Bi和Gi离婚的前提 ...

  5. 升级到php7和安装拓展(mac centos)

    Mac升级到php7 使用homebrew安装php7 brew update #更新源 brew search php #查找源中的php,发现有php7.1版本,安装最新的php7.1 brew ...

  6. Google图片和NASA 网站图片的爬虫

    1.根据关键字爬取NASA网站上的图片 首先针对需要爬取的网站进行分析,输入关键字查找需要的内容 通过关键字请求,网页每次会加载20张的缩略图,分析网页源码能够很容易的找到缩略图的url: 然后再点开 ...

  7. python开发_常用的python模块及安装方法

    adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctype ...

  8. High-current supply uses standard three-terminal regulator

    Voltage-regulator design for high output currents can be a critical and difficult task. Although vol ...

  9. FreeRTOS API

    Task Creation xTaskCreate vTaskDelete Task Control vTaskDelay vTaskDelayUntil uxTaskPriorityGet vTas ...

  10. 【java失业择业中】失业第四天:准备面试

    1.jQuery基础 学好jquery的一个基础条件是学好css层叠样式,因为很多时候这2个是一块配合使用的. 页面中很多需要jquery实现的效果只是通过jquery的选择器,选中要操作的元素,添加 ...