uva 727 运算符 中缀到后缀的转换

来源:互联网 发布:java如何设置全局变量 编辑:程序博客网 时间:2024/06/02 06:06

卡了好久好久    一直没敢在动   今天一遍AC    

 

#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;
}

原创粉丝点击