• Problem D: STL——表达式求值(用的vector没用stack)


    Description

    给出一个表达式,求出其值。表达式中只存在 +、-、*、三种运算,我们假设表达式是正确的,
          且不存在除数为零的情况。

    Input

    第一行输入一个正整数 n(1<=n<=30) ,表示有表达式 n 个数(每个数均小于100),表达式中只有数值(都是大于零的数)
          和运算符(包括+、-、*、=四种运算符,其中 = 只在表达式最后,表示一个表达式输出结束,且整个表达式不存在空格)

    Output

    表达式的值(表达式的值不会超出 double 的范围并保留两位小数)

    Sample Input

    5 1*2*3*4*5= 5 5-1-2+3+4=

    Sample Output

    120.00 9.00

    HINT

    使用STL的stack容易实现。


    Append Code

    #include<iostream>
    #include<stack>//remember top
    #include<algorithm>
    #include<iterator>
    #include<iomanip>
    using namespace std;
     
    int main()
    {
        int n;
        while(cin>>n)
        {
            double a;
        char b;
        stack<double> d_sta;
        stack<char> c_sta;
        while(n--)
        {
            cin>>a>>b;
            if(!d_sta.empty())//d,c all can
            {
                if(c_sta.top()=='-')
                    a=-a;
                else if(c_sta.top()=='*')
                {
                    a*=d_sta.top();
                    d_sta.pop();//dessert
                }
            }
                d_sta.push(a);
                c_sta.push(b);
        }
        a=0;
        while(!d_sta.empty())//as if have not iterator
        {
            a+=d_sta.top();
            d_sta.pop();
        }
        cout<<fixed<<setprecision(2)<<a<<endl;
        }
        return 0;
    }
  • 相关阅读:
    3dsmax不同版本 pyside qt UI 设置max窗口为父窗口的方法
    oracle中的数据库和实例
    oracle中的表空间(tablespace)、方案(schema)、段(segment)、区(extent)、块(block)
    什么是WSE
    Server.Transfer,Response.Redirect的区别
    Oracle 中的几个数据类型介绍
    oracle中的连接字符串
    Oracle中的 单引号 和 双引号
    接口是否可以继承接口?抽象类是否可以实现接口?抽象类是否可以继承实体类?
    聚簇索引
  • 原文地址:https://www.cnblogs.com/TogetherLaugh/p/6653709.html
Copyright © 2020-2023  润新知