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;
    }
  • 相关阅读:
    C++拷贝构造函数(深拷贝,浅拷贝)
    c++ string assign =
    undefined与null的区别---js
    Cocos2d-x内存自动释放机制--透彻篇
    cocos2d-x 坐标系
    cocos-html5 JS 写法基础 语言核心
    selenium--更改标签的属性值
    selenium--高亮显示正在操作的元素
    selenium--页面元素是否可见和可操作
    selenium--拖拽页面元素
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5500062.html
Copyright © 2011-2022 走看看