• 表达式求值_栈


    问题 C: 表达式求值

    时间限制: 3 Sec  内存限制: 128 MB
    提交: 1  解决: 1
    [提交][状态][讨论版]

    题目描述

    ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧。
    比如输入:“1+2/4=”,程序就输出1.50(结果保留两位小数)

    输入

    第一行输入一个整数n,共有n组测试数据(n<10)。
    每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。
    数据保证除数不会为0

    输出

    每组都输出该组运算式的运算结果,输出结果保留两位小数。

    样例输入

    2
    1.000+2/4=
    ((1+2)*5+1)/4=

    样例输出

    1.50
    4.00

    解题思路:
    今晚上就做了这一个题,感觉有时候做出一个题来好不容易,但当做出来的时候感觉是相当爽。
      这是数据结构里面的问题,要利用栈。
      (建立三个栈,分别是数字栈,运算符栈,括号栈)
      首先要考虑各个字符的优先级,比如碰到*/,优先级是挺高的,就得先算完,比如3+2*4,碰到4时看它前面这个运算符,是*,就得先把2*4算出来,然后入数字栈。得到①:见到*/就算出结果来,然后入数字栈。
      ②碰到(,没什么可以做的,只能入括号栈。
      ③碰到+-,入运算符栈。
      ④碰到),这个优先级也是很高的,碰到他就得把之前的运算全部算出来,然后入数字栈。
      ⑤碰到=,就应该考虑输出结果了,但输出之前得看看运算符栈里面还有没有运算符(这时候里面应该只剩下+-两种了),有的话计算完再输出。没有的话直接输出。
      
    ps:  55555...后来验证发现程序有严重bug,并不适用所有情况。。。
    代码:
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <stack>
      5 
      6 using namespace std;
      7 
      8 int main()
      9 {
     10     char tt;
     11     int n;
     12     double d;
     13     char c;
     14     scanf("%d",&n);
     15     for(int i=0;i<n;i++){
     16         stack<double> num;
     17         stack<char> oper;
     18         stack<char> paren;
     19         char ch1[1111];
     20         getchar();
     21         while(1){
     22         c=getchar();
     23         if(c=='='){
     24             //scanf("%c",&tt);
     25             while(!oper.empty()){
     26                 //while(!paren.empty()){
     27                     char c2=oper.top();
     28                     oper.pop();
     29                     if(c2=='+'){
     30                         double t1;
     31                         t1=num.top();
     32                         num.pop();
     33                         double t2=num.top();
     34                         num.pop();
     35                         t1=t1+t2;
     36                         num.push(t1);
     37                     }
     38                     if(c2=='-'){
     39                         double t1=num.top();
     40                         num.pop();
     41                         double t2=num.top();
     42                         num.pop();
     43                         t1=t2-t1;
     44                         num.push(t1);
     45                     }
     46                 //}
     47                 //paren.pop();
     48             }
     49 
     50             printf("%.2lf
    ",num.top());
     51             num.pop();
     52             break;
     53         }else{
     54             if(c==')'){
     55                 while(!oper.empty()){
     56                     char c3=oper.top();
     57                     oper.pop();
     58                     if(c3=='+'){
     59                         double t1=num.top();
     60                         num.pop();
     61                         double t2=num.top();
     62                         num.pop();
     63                         t1=t1+t2;
     64                         num.push(t1);
     65                     }
     66                     if(c3=='-'){
     67                         double t1=num.top();
     68                         double t2=num.top();
     69                         t1=t2-t1;
     70                         num.push(t1);
     71                     }
     72                 }
     73                 paren.pop();
     74             }
     75             if(c=='+'||c=='-'||c=='*'||c=='/'){
     76                 oper.push(c);
     77             }
     78 
     79             if(c=='('){
     80                 paren.push(c);
     81             }
     82             if(c>='0'&&c<='9'){
     83                 ungetc(c,stdin);
     84                 double t3=d;
     85                 scanf("%lf",&t3);
     86                 if(!oper.empty()){
     87                     char c1=oper.top();
     88                     oper.pop();
     89                     if(c1=='*'){
     90                         double t1=num.top();
     91                         num.pop();
     92                         t1=t1*t3;
     93                         num.push(t1);
     94                     }
     95                     if(c1=='/'){
     96                         double t1=num.top();
     97                         num.pop();
     98                         t1=t1/t3;
     99                         num.push(t1);
    100                     }
    101                     if(c1=='+'||c1=='-'){
    102                         num.push(t3);
    103                         oper.push(c1);
    104                     }
    105                 }else{
    106                     num.push(t3);
    107                 }
    108             }
    109         }
    110     }
    111     }
    112     return 0;
    113 }
     
  • 相关阅读:
    《高校后勤管理信息系统设计与实现》论文笔记五
    《高校后勤管理系统的设计与实现》论文笔记三
    《高校后勤管理系统的设计与实现》论文笔记二
    如何利用React.js开发出强大Web应用
    关于啤酒和尿布故事的真相
    以生活例子说明单线程与多线程
    未来哪些领域WiFi将成为刚需?
    CSS开发中的10个不要
    10年后编程还有意义吗?
    JavaEE中遗漏的10个最重要的安全控制
  • 原文地址:https://www.cnblogs.com/TWS-YIFEI/p/5778283.html
Copyright © 2020-2023  润新知