uva727运算符 中缀到后缀的转换

    技术2022-06-12  50

    卡了好久好久    一直没敢在动   今天一遍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;}


    最新回复(0)