727运算符前缀变后缀

来源:互联网 发布:编程员招聘 编辑:程序博客网 时间:2024/06/13 12:03

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stack>

using namespace std;

stack<char>operat;

int main()
{
    //freopen("in.in","r",stdin);
    int test_case;
    cin>>test_case;
    cin.ignore(1);
    char c;
    int flage = 0;
    while(test_case--)
    {
        while((c = getchar()) != EOF)
        {
            if(c == '\n')
            {
                flage++;
                if(flage == 2)  break;
                continue;
            }
            flage = 0;
            if(isdigit(c))
                cout<<c;
            else{
                        if(c == '+' || c == '-'){
                        while(!operat.empty())
                        {
                            if(operat.top() != '(' )
                            {
                                cout<<operat.top();
                                operat.pop();
                            }
                            else break;
                        }
                        operat.push(c);
                        }
                        else if(c == '*' || c == '/')
                        {
                            while(!operat.empty())
                            {
                                if(operat.top() == '*' || operat.top() == '/')
                                {
                                    cout<<operat.top();
                                    operat.pop();
                                }
                                else break;
                            }
                            operat.push(c);
                        }
                        else if(c == '(')
                        operat.push(c);
                        else if(c == ')')
                        {
                            while(!operat.empty())
                            {
                                if(operat.top() != '(')
                                {
                                    cout<<operat.top();
                                    operat.pop();
                                }
                                else
                                {
                                    operat.pop();
                                    break;
                                }
                            }
                        }
                }
        }
        while(!operat.empty())
        {
                cout<<operat.top();
                operat.pop();
        }

        if(test_case != 0) cout<<endl;
        cout<<endl;
    }


    return 0;
}

 

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#define M 500

using namespace std;

long long deal(char *str,char a,char b)
{
    stack<char>operat;
    stack<long long>num;

    long long len = strlen(str);
    long long flage = 0;
    long long n;
    char ope;
    for(long long i = 0;i < len;i++)
    {
        if(!flage){
            sscanf(&str[i],"%lld",&n);
            num.push(n);
            if(n >= 10) i++;
            flage = 1;
        }
        else{
            ope = str[i];
            flage = 0;

            if(!operat.empty())
            {
char cal = operat.top();
                long long x,y;
if(ope == b || cal == a){
                   operat.pop();
                   x = num.top();num.pop();
                   y = num.top();num.pop();
                   long long r;
                   if(cal == '*')
                     r = x * y;
                   else r = x + y;

                   num.push(r);
                }

                operat.push(ope);
            }
            else operat.push(ope);
        }
    }

    while(!operat.empty())
    {
        char cal = operat.top();
        operat.pop();
        long long x,y;
        x = num.top(); num.pop();
        y = num.top(); num.pop();
        long long r;
        if(cal == '*')
            r = x * y;
        else r = x + y;

        num.push(r);
    }

    return num.top();
}

int main()
{
    //freopen("in.in","r",stdin);

    long long T;
    long long minv,maxv;
    char str[M];
    scanf("%lld",&T); getchar();
    while(T--)
    {
        gets(str);
        minv = deal(str,'*','+');
        maxv = deal(str,'+','*');

    printf("The maximum and minimum are %lld and %lld.\n",maxv,minv);
    }
    return 0;
}