HDU 1237 简单计算器(栈的应用)

32 篇文章 0 订阅
5 篇文章 0 订阅

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237

简单计算器

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

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

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

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

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

Sample Output
3.00
13.36

Source
浙大计算机研究生复试上机考试-2006年

【思路分析】栈的典型应用,先把所有的乘除运算的结果计算出来,然后最后进行加减的模拟。

【AC代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;

char a[205];
int main()
{
    stack<double>num;
    stack<char>ch;
    while(gets(a))
    {
        while(!num.empty())
        {
            num.pop();
        }
        while(!ch.empty())
        {
            ch.pop();
        }
        if(strcmp(a,"0")==0)
        {
            break;
        }
        int len=strlen(a);
        for(int i=0; i<len; i++)
        {
            if(a[i]==' ')
            {
                continue;
            }
            else if(a[i]=='*'||a[i]=='/')//把其他运算的结果计算出来
            {
                double x=num.top();
                num.pop();
                double y=0;
                int j;
                for(j=i+2; j<len; j++)
                {
                    if(a[j]==' ')
                    {
                        break;
                    }
                    y=y*10+a[j]-'0';
                }
                if(a[i]=='*')
                {
                    num.push(x*y);
                }
                else
                {
                    num.push(x/y);
                   // printf("***\n");
                    //printf("%.2lf\n",x/y);
                }
                i=j;
            }
            else if(a[i]=='+'||a[i]=='-')
            {
                ch.push(a[i]);
            }
            else
            {
                int j;
                double x=0;
                for(j=i; j<len; j++)
                {
                    if(a[j]!=' ')
                    {
                        x=x*10+a[j]-'0';
                    }
                    else
                    {
                        break;
                    }
                }
                i=j;
                num.push(x);
            }
        }
        int sum=0;
        char ss[205];
        double c[208];
        int n=0;
        stack<double>nn;
        stack<char>cc;
        while(!ch.empty())
        {
            cc.push(ch.top());
            ch.pop();
        }
        while(!num.empty())
        {
            nn.push(num.top());
            num.pop();
        }
        while(!cc.empty()&&!nn.empty())
        {
            double x=nn.top();
            nn.pop();
            double y=nn.top();
            nn.pop();
            char str=cc.top();
            cc.pop();
            if(str=='+')
            {
                nn.push(x+y);
            }
            else
            {
                nn.push(x-y);
            }
        }
        printf("%.2lf\n",nn.top());
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值