zoukankan      html  css  js  c++  java
  • 2006浙大:简单计算器

    题目描述:
        读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
    输入:
        测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
    输出:
        对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
    样例输入:
    1 + 2
    4 + 2 * 5 - 7 / 11
    0
    样例输出:
    3.00
    13.36

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    using namespace std;
    const int MAXN=1005;
    char s[MAXN];
    bool isOp(char op)
    {
        if(op=='+'||op=='-'||op=='*'||op=='/')
            return true;
        else
            return false;
    }
    int Priority(char op)
    {
        switch(op)
        {
            case '+':return 1;
            case '-':return 1;
            case '*':return 2;
            case '/':return 2;
        }
    }
    double cal(double x,double y,char op)
    {
        switch(op)
        {
            case '+':return x+y;
            case '-':return x-y;
            case '*':return x*y;
            case '/':return x/y;
        }
    }
    char ss[MAXN];
    int top;
    int main()
    {
        while(gets(s)&&strcmp(s,"0")!=0)
        {
            top=0;
            stack<char> op;
            stack<double> it;
            int len=strlen(s);
            for(int i=0;i<len;i++)
            {
                if(s[i]==' ')   continue;
                ss[top++]=s[i];         
            }
            int e=0;
            for(int i=0;i<top;i++)
            {
                if(isOp(ss[i]))
                {
                    it.push(double(e));
                    e=0;
                    if(op.empty())
                    {
                        op.push(ss[i]);
                    }
                    else
                    {
                        int pri1=Priority(ss[i]);
                        int pri2=Priority(op.top());
                        if(pri1>pri2)
                        {
                            op.push(ss[i]);
                        }
                        else
                        {
                            do{
                                double x=it.top();it.pop();
                                double y=it.top();it.pop();
                                double res=cal(y,x,op.top());
                                op.pop();
                                it.push(res);
                            }while(!op.empty()&&pri1<=Priority(op.top()));
                            op.push(ss[i]);
                        }
                    }
                }
                else
                {
                    e*=10;
                    e=e+ss[i]-'0';
                }
            }
            it.push(double(e));
            while(it.size()>1)
            {
                double x=it.top();it.pop();
                double y=it.top();it.pop();
                double res=cal(y,x,op.top());op.pop();
                it.push(res);
            }
             
            double res=it.top();
            printf("%.2lf
    ",res);
        }
         
        return 0;
    }
  • 相关阅读:
    Libcurl
    Inno Setup教程
    APICloud平台的融云2.0集成
    关于mysql建立索引 复合索引 索引类型
    linux恢复误删除文件-extundelete
    OpenStack QA
    Android之应用程序怎样调用支付宝接口
    NYOJ 22 素数求和问题
    Mycat(5):聊天消息表数据库按月分表实践,平滑扩展
    opencv对图像进行边缘及角点检測
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5500062.html
Copyright © 2011-2022 走看看