zoukankan      html  css  js  c++  java
  • 南阳理工-35

    http://acm.nyist.net/JudgeOnline/problem.php?pid=35

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<algorithm>
    
    using namespace std;
    const int maxn=10007;
    char a[maxn], b[maxn];
    stack<char> S;
    stack<double> S1;
    
    int Priority(char ch)
    {
        int i;
        switch(ch)
        {
        case '+':
        case '-':
            i=1;
            break;
        case '*':
        case '/':
            i=2;
            break;
        default :
            i=-1;
            break;
        }
        return i;
    }
    ///中缀表达式转后缀表达式
    void ToPoland()
    {
        int i=0, j=0;
        int len=strlen(a);
        S.push('@');
        while(i < len-1)
        {
            if(a[i]=='(')
            {
                S.push(a[i]);
                i++;
            }
            else if(a[i]==')')
            {
                while(S.top()!='(')
                {
                    b[j++]=S.top();
                    b[j++]=' ';
                    S.pop();
                }
                S.pop();
                i++;
            }
            else if(a[i]=='+' || a[i]=='-' || a[i]=='*' || a[i]=='/')
            {
                while(Priority(a[i]) <= Priority(S.top()))
                {
                    b[j++]=S.top();
                    b[j++]=' ';
                    S.pop();
                }
                S.push(a[i]);
                i++;
            }
            else
            {
                while((a[i]>='0'&&a[i]<='9') || a[i]=='.')
                {
                    b[j++]=a[i++];
                }
                b[j++]=' ';
            }
        }
        while(S.top()!='@')
        {
            b[j++]=S.top();
            b[j++]=' ';
            S.pop();
        }
        S.pop();
    }
    ///求逆波兰表达式的值
    double Count_Poland()
    {
        double p, q, u, v;
        for(int i=0; b[i]; i++)
        {
            p=q=0;
            if((b[i]>='0'&&b[i]<='9') || b[i]=='.')
            {
                while((b[i]>='0'&&b[i]<='9') || b[i]=='.')
                {
                    if(b[i]=='.')
                    {
                        i++;
                        double x=1;
                        while(b[i]>='0'&&b[i]<='9')
                        {
                            x*=0.1;
                            q=q+(b[i]-'0')*x;
                            i++;
                        }
                    }
                    else
                    {
                        p=p*10+(b[i]-'0');
                        i++;
                    }
                }
                S1.push(p+q);
            }
            else
            {
                if(b[i]=='+')
                {
                    u=S1.top();
                    S1.pop();
                    v=S1.top();
                    S1.pop();
                    S1.push(v+u);
                }
                else if(b[i]=='-')
                {
                    u=S1.top();
                    S1.pop();
                    v=S1.top();
                    S1.pop();
                    S1.push(v-u);
                }
                else if(b[i]=='*')
                {
                    u=S1.top();
                    S1.pop();
                    v=S1.top();
                    S1.pop();
                    S1.push(v*u);
                }
                else if(b[i]=='/')
                {
                    u=S1.top();
                    S1.pop();
                    v=S1.top();
                    S1.pop();
                    S1.push(v/u);
                }
            }
        }
        return S1.top();
    }
    int main()
    {
        int n;
        scanf("%d", &n);
    
        while(n--)
        {
            scanf("%s", a);
            ToPoland();
            printf("%.2f
    ", Count_Poland());
        }
        return 0;
    }
  • 相关阅读:
    linux系统——机制与策略(三)
    linux系统——机制与策略(二)
    Linux系统——机制策略(一)
    RTSP会话基本流程
    linux编程学习
    编码风格——linux内核开发的coding style
    编程风格——整洁代码的4个提示
    编程风格——五种应该避免的代码注释
    十条不错的编程观点
    代码优化概要
  • 原文地址:https://www.cnblogs.com/w-y-1/p/6652512.html
Copyright © 2011-2022 走看看