• HDU-1237-简单计算器


    简单计算器

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 18561    Accepted Submission(s): 6543


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

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

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

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

    Sample Output
    3.00 13.36
     
    解题思路:
    栈的基本操作,+号直接压栈,-号变号压栈,*号和/号取出栈顶元素运算过后重新压栈
    全部输入完毕,重新读出栈里面的内容,进行加和运算,得到最终的结果



    源代码:
    <span style="font-size:18px;">#include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<deque>
    #include<map>
    #include<set>
    #include<algorithm>
    #include<string>
    #include<iomanip>
    #include<cstdlib>
    #include<cmath>
    #include<sstream>
    #include<ctime>
    using namespace std;
    
    typedef long long ll;
    
    int main()
    {
        while(1)
        {
            stack<double> q;
            double num;
            scanf("%lf",&num);
            q.push(num);
            char ch;
            scanf("%c",&ch);
            while(ch==' ')
            {
                scanf("%c",&ch);
            }
            while(ch!='
    ')
            {
                scanf("%lf",&num);
                if(ch=='+')
                {
                    q.push(num);
                }
                else if(ch=='-')
                {
                    q.push(0-num);
                }
                else if(ch=='*')
                {
                    num*=q.top();
                    q.pop();
                    q.push(num);
                }
                else if(ch=='/')
                {
                    num=q.top()/num;
                    q.pop();
                    q.push(num);
                }
                scanf("%c",&ch);
                while(ch==' ')
                {
                    scanf("%c",&ch);
                }
            }
            double res=0;
            int cnt=0;
            while(!q.empty())
            {
                res+=q.top();
                q.pop();
                cnt++;
            }
            if(cnt==1&&res==0)
            {
                break;
            }
            else
                printf("%.2lf
    ",res);
        }
    	return 0;
    }</span>



  • 相关阅读:
    通用测试用例(转载)
    微信小程序开发-使用阿里巴巴矢量图标
    flask框架启服务+json格式入参+postman获取上个接口的token作为下个接口的入参+关联接口【多测师_王sir】
    this.$set的正确使用
    vue中异步函数async和await的用法
    Tornado 异步协程coroutine原理
    nvm安装配置
    python基础
    数组for循环方法总结
    react 之props传值
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7776056.html
Copyright © 2020-2023  润新知