东师附中 1185 栈 中缀转后缀

来源:互联网 发布:淘宝网的营销策略 编辑:程序博客网 时间:2024/04/29 15:38

1185: 栈 中缀转后缀

时间限制: 1 Sec  内存限制: 128 MB
提交: 9  解决: 5
[提交][状态][讨论版]

题目描述

请编写程序将一个中缀表达式转换为后缀表达式。

输入

仅一行,是一个中缀表达式。输入的符号中只有这些基本符号“0123456789+-*/()”,并且不会出现形如2*-3的格式,所有数字都是个位数,“/”表示整除运算。

输出

仅一行,是转换后的后缀表达式。数字之间、运算符之间、数字和运算符之间都用一个空格隔开

样例输入

8-(3+2*6)/5+4

样例输出

8 3 2 6 * + 5 / - 4 +

提示

表达式长度<=200

来源

[提交][状态][讨论版]

基本的中缀表达式转后缀表达式,代码如下:


#include<stdio.h>#include<iostream>#include<string.h>#include<queue>#include<stack>using namespace std;stack<char > op;queue<char > num;queue<char > last;char ch[210];bool is_op(char a){    if(a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')')        return true;    else        return false;}int fun(char a){    switch(a)    {        case '+':        case '-':return 1;break;        case '*':        case '/':return 2;break;        case '(':return 3;break;        default:return 0;break;    }}int main(){    int i,len;    char c;    scanf("%s",ch);    len = strlen(ch);    for(i = 0;i < len;i++)    {        if(!is_op(ch[i]))//if a number            num.push(ch[i]);        else//if a symbol        {            if(op.empty())                op.push(ch[i]);            else            {                if(ch[i] != ')')//the new one is not a ')'                {                    if(op.top() == '('||fun(op.top()) < fun(ch[i]))                        op.push(ch[i]);                    else                    {                        while(!num.empty())                        {                            last.push(num.front());                            num.pop();                        }                        if(!op.empty())                        {                            last.push(op.top());                            op.pop();                        }                        --i;                    }                }                else//the new one is a ')'                {                    while(!num.empty())                    {                        last.push(num.front());                        num.pop();                    }                    while(!op.empty())                    {                        if(op.top() != '(')                        {                            c = op.top();                            last.push(op.top());                            op.pop();                        }                        else                        {                            op.pop();                            if(!op.empty())                            {                                if(op.top() != '('&&fun(op.top()) > fun(c))                                {                                    last.push(op.top());                                    op.pop();                                    break;                                }                                else                                    break;                            }                        }                    }                }            }        }    }    while(!num.empty())    {        last.push(num.front());        num.pop();    }    while(!op.empty())    {        last.push(op.top());        op.pop();    }    while(last.size()>=2)    {        cout<<last.front()<<" ";        last.pop();    }    cout<<last.front()<<endl;    last.pop();    return 0;}



0 0