zoukankan      html  css  js  c++  java
  • (c++实现)南洋理工 oj 267 郁闷的C小加(二)

    郁闷的C小加(二)

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:4
     
    描述

    聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很高兴。但C小加是个爱思考的人,他又想通过这种方法计算一个表达式的值。即先把表达式转换为后缀表达式,再求值。这时又要考虑操作数是小数和多位数的情况。

     
    输入
    第一行输入一个整数T,共有T组测试数据(T<10)。
    每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数并且小于1000000。
    数据保证除数不会为0。
    输出
    对于每组测试数据输出结果包括两行,先输出转换后的后缀表达式,再输出计算结果,结果保留两位小数。两组测试数据之间用一个空行隔开。
    样例输入
    2
    1+2=
    (19+21)*3-4/5=
    
    样例输出
    12+=
    3.00
    
    1921+3*45/-=
    119.20
    #include<bits/stdc++.h>
    #define maxn 1100
    using namespace std;
    char s[maxn];
    double answer;
    stack<char> a;
    stack<double> ans;
    double math(char c)
    {
        double y = ans.top();
        ans.pop();
        double x = ans.top();
        ans.pop();
        if(a.top() == '+') answer = x + y;
        if(a.top() == '-')answer = x - y;
        if(a.top() == '*')answer = x * y;
        if(a.top() == '/')answer = x / y;
        ans.push(answer);
    }
    int main()
    {
        int cases;
        cin>>cases;
        getchar();
        while(cases--)
        {
            while(!ans.empty())ans.pop();
            while(!a.empty())a.pop();
            gets(s);
            int l =strlen(s) - 1;
            for(int i = 0 ; i < l; ++i)
            {
                if(s[i] == '(')
                {
                    a.push(s[i]);
                }
                else if(s[i] == ')')
                {
                    while((!a.empty()) && a.top() != '(')
                    {
                        cout<<a.top();
                        math(a.top());
                        a.pop();
                    }
                    a.pop();
                }
                else if(isdigit(s[i]) || s[i] == '.')
                {
                    cout<<s[i];
                    string temp ;
                    temp += s[i];
                    while((isdigit(s[i+1]) || s[i+1] == '.') && i < l)
                    {
                        cout<<s[i+1];
                        temp += s[i+1];
                        i++;
                    }
                    double t = atof(temp.c_str());
                    ans.push(t);
                }
                else if(s[i] == '+' || s[i] == '-')
                {
                    while((!a.empty()) && (a.top() == '+' || a.top() == '*' || a.top() == '/' || a.top() == '-'))
                    {
                        cout<<a.top();
                        math(a.top());
                        a.pop();
                    }
                    a.push(s[i]);
                }
                else if(s[i] == '*' ||s[i] == '/')
                {
                    while((!a.empty()) && (a.top() == '*' || a.top() == '/'))
                    {
                        cout<<a.top();
                        math(a.top());
                        a.pop();
                    }
                    a.push(s[i]);
                }
            }
            while(!a.empty())
            {
                cout<<a.top();
                math(a.top());
                a.pop();
            }
            cout<<"="<<endl;
            printf("%.2lf
    ",ans.top());
            if(cases > 0)cout<<endl;
        }
    }
    

      

  • 相关阅读:
    给RArray和CArray加上自己的泛型算法(转)
    由电话号码得到姓名(转)
    在Eclipse中用TODO标签管理任务(转)
    Symbian_C++:Multipart/formdata(转)
    Symbian源代码还原之一,二,三,四,五(转)
    使用Carbide C++进行ODD(On Device Debugging)(转)
    S60_Platform_and_device_identification_codes(转)
    S60 3rd中怎么设置字体类型与字体大小!!!!!(转)
    诺基亚10.22变革影响的分析(转)
    MeeGo软件架构综述(转)
  • 原文地址:https://www.cnblogs.com/cwenliu/p/5924006.html
Copyright © 2011-2022 走看看