zoukankan      html  css  js  c++  java
  • 简单计算器(栈和队列的应用)

    简单计算器 codeup1918 

    918: 简单计算器

    时间限制: 1 Sec  内存限制: 32 MB
    提交: 2063  解决: 846
    [提交][状态][讨论版][命题人:外部导入]

    题目描述

    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

    输入

    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

    输出

    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

    样例输入

    30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
    0

    样例输出

    12178.21

    思路:
    题目给出的计算式是中缀表达式,求解的步骤是,第一步将中缀表达式转换为后缀表达式,第二步计算后缀表达式的结果
    利用栈存储操作符,队列存储后缀表达式

      1 #include <iostream>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <stack>
      5 #include <queue>
      6 #include <map>
      7 
      8 using namespace std ;
      9 
     10 struct node{
     11     double num ;
     12     char op ;
     13     bool flag ;//op or num
     14 };
     15 
     16 stack<node> stk ;
     17 queue<node> que ;
     18 map<char,int> mp ;
     19 string str ;
     20 
     21 //30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92 
     22 
     23 void transform(){//将中缀表达式转换为后缀表达式 
     24     int la = str.size() ;
     25     for(int i=0;i<la;i++){
     26         if(str[i]>='0' && str[i]<='9'){
     27             node tmp ;
     28             tmp.num = str[i++] - '0' ;
     29             tmp.flag = true ;
     30             while(str[i]>='0' && str[i] <= '9' && i < la){
     31                 tmp.num = tmp.num * 10 + str[i] - '0' ;
     32                 i++ ;
     33             }
     34             i--;
     35             que.push(tmp) ;
     36         }else{
     37             node tmp ;
     38             tmp.op = str[i] ;
     39             tmp.flag = false ;
     40             while(!stk.empty() && mp[stk.top().op] >= mp[tmp.op]){
     41                 auto ele = stk.top() ;
     42                 stk.pop() ;
     43                 que.push(ele) ;
     44             }
     45             stk.push(tmp) ;
     46         }
     47     }
     48     while(!stk.empty()){
     49         auto ele = stk.top() ;
     50         stk.pop() ;
     51         que.push(ele) ;
     52     }
     53 }
     54 
     55 double calc(){//计算结果 
     56     while(!que.empty()){
     57         auto ele = que.front() ;
     58         que.pop() ;
     59         if(ele.flag){
     60             stk.push(ele) ;
     61         }else{
     62             node tmp ;
     63             tmp.flag = true ;
     64             double num1 = stk.top().num ;
     65             stk.pop() ;
     66             double num2 = stk.top().num ;
     67             stk.pop() ;
     68             if(ele.op == '+'){
     69                 tmp.num = num2 + num1 ;
     70             }else if(ele.op == '-'){
     71                 tmp.num = num2 - num1 ;
     72             }else if(ele.op == '*'){
     73                 tmp.num = num2 * num1 ;
     74             }else{
     75                 tmp.num = num2 / num1 ;
     76             }
     77             stk.push(tmp) ;
     78         } 
     79     }
     80     return stk.top().num ;
     81 }
     82 
     83 int main(){
     84     mp['+'] = mp['-'] = 1 ;
     85     mp['*'] = mp['/'] = 2 ;
     86     while(getline(cin,str),str != "0"){
     87         for(string::iterator it=str.begin();it!=str.end();it++){
     88             if(*it == ' '){
     89                 str.erase(it) ;
     90             }
     91         }
     92         transform() ;
     93         printf("%.2lf
    ",calc()) ;
     94         while(!stk.empty()){
     95             stk.pop() ;
     96         }
     97     }
     98     
     99     return 0 ;
    100 }

    ...

  • 相关阅读:
    BZOJ1051 [HAOI2006]受欢迎的牛 强连通分量缩点
    This blog has been cancelled for a long time
    欧拉定理、费马小定理及其拓展应用
    同余基础
    [LeetCode] 73. Set Matrix Zeroes
    [LeetCode] 42. Trapping Rain Water
    [LeetCode] 41. First Missing Positive
    [LeetCode] 71. Simplify Path
    [LeetCode] 148. Sort List
    [LeetCode] 239. Sliding Window Maximum
  • 原文地址:https://www.cnblogs.com/gulangyuzzz/p/12063403.html
Copyright © 2011-2022 走看看