zoukankan      html  css  js  c++  java
  • C——简单计算器(HDU1237)

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

    Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。 
    Output对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。 
    Sample Input

    1 + 2
    4 + 2 * 5 - 7 / 11
    0

    Sample Output

    3.00
    13.36

    解题思路:
    虽然用数组也可以解决这一类题,但在这里我使用栈来解答这一题。
    显然这是一个模拟题。首先把数字和符号分别放入栈中;
    计算部分首先计算乘除法,并把计算的结果放入栈内,如果遇到加减号则把数字和符号分别放入另一个栈中
    此时另一个栈中就只有加减法,再计算加减法,得到计算结果。(注意计算顺序)
     1 #include <iostream>
     2 #include<stack>
     3 #include<queue>
     4 #include<cstring>
     5 #include<cstdio>
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     int i,j,h,g=0,d,b,q;
    11     double ans,x1,x2,k;
    12     char a[202];
    13     while (1)
    14     {
    15         memset(a,0,sizeof(a));
    16         k=0;h=0;ans=0;d=0,b=0;g=0;
    17         gets(a);
    18         if (strlen(a)==1&&a[0]=='0')break;
    19         stack<char>f,f2;
    20         stack<double>s,s2;
    21         queue<char>f1;
    22         queue<double>s1;
    23         for (i=0;i<strlen(a);i++)
    24         {
    25         if (a[i]==' '){h++;continue;}  //分离数字和符号
    26         else
    27            {
    28         if (h%2!=0){f.push(a[i]);s.push(k);k=0;d++;continue;}
    29         if (h%2==0){k=k*10+(a[i]-'0');}
    30            }
    31         }
    32         s2.push(k);
    33         q=d;
    34         while (q--)        //导入另一个栈内使计算顺序正确(栈是后进先出)
    35         {
    36             s2.push(s.top());s.pop();
    37             f2.push(f.top());f.pop();
    38         }
    39        while (d--)
    40        {
    41            g=1;
    42            if (f2.top()=='+'||f2.top()=='-')  //把加减法放入另一个队列
    43            {
    44                f1.push(f2.top());
    45                s1.push(s2.top());
    46                f2.pop();
    47                s2.pop();
    48                b++;continue;
    49            }
    50            if (f2.top()=='*')      //计算乘除法
    51            {
    52                x1=s2.top();s2.pop();
    53                x2=s2.top();s2.pop();
    54                s2.push(x1*x2);
    55                f2.pop();continue;
    56            }
    57            if (f2.top()=='/')
    58            {
    59                x1=s2.top();s2.pop();
    60                x2=s2.top();s2.pop();
    61                s2.push(x1/x2);
    62                f2.pop();continue;
    63            }
    64        }
    65        s1.push(s2.top());
    66        ans+=s1.front();s1.pop();
    67        while (b--)
    68        {
    69            if (f1.front()=='+')     //计算加减法
    70            {
    71                ans+=s1.front();
    72                s1.pop();
    73                f1.pop();continue;
    74            }
    75            if (f1.front()=='-')
    76            {
    77                ans-=s1.front();
    78                s1.pop();
    79                f1.pop();continue;
    80            }
    81        }
    82         if (g==1)printf("%0.2lf
    ",ans);   //g==1用来控制格式(可以去掉)
    83     }
    84     return 0;
    85 }


  • 相关阅读:
    WinRAR v5.90中文正式32&64位整合安装版
    WIN10永久激活工具 HWIDGEN
    Adobe2020套装激活工具
    keka 1.1.26-压缩工具
    python文件和目录操作方法大全(含实例)
    如何使公网能够访问本地web服务器?
    网线那根正极那根负极
    HPUX 大文件系统扩容
    LESS详解之函数(四)
    Win7 安装Apache 2.2.4报错:<OS 5>拒绝访问. :Failed to open the WinNT service manager
  • 原文地址:https://www.cnblogs.com/shendeng23/p/7221480.html
Copyright © 2011-2022 走看看