C++實現中綴表達式轉前、後綴

#include<iostream>
#include<string>
#include<stack>
using namespace std;
bool isInt(char ch)
{
    if(ch>='0'&&ch<='9')
        return true;
    return false;
}
bool isOperator(char ch)
{
    if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
        return true;
    return false;
}
int opLevel(char ch)
{
    int level;
    switch(ch)
    {
    case'+':
    case'-':
        level=1;
        break;
    case'*':
    case'/':level=2;
        break;
    default:
        level=0;
        break;
    }
    return level;
}
/*中綴-->前綴  算法
1)求輸入串的逆序。
2)檢查輸入的下一元素。
3)假如是操做數,把它添加到輸出串中。
4)假如是閉括號,將它壓棧。
5)假如是運算符,則
    i)假如棧空,此運算符入棧。
    ii)假如棧頂是閉括號,此運算符入棧。
    iii)假如它的優先級高於或等於棧頂運算符,此運算符入棧。
    iv)不然,棧頂運算符出棧並添加到輸出串中,重複步驟5。
6)假如是開括號,棧中運算符逐個出棧並輸出,直到遇到閉括號。閉括號出棧並丟棄。
7)假如輸入還未完畢,跳轉到步驟2。
8)假如輸入完畢,棧中剩餘的全部操做符出棧並加到輸出串中。
9)求輸出串的逆序。
*/
string priOrder(string myStr)
{
    stack<char> opStack;
    string result;
    for(int i=myStr.length()-1; i>=0; i--)
    {
        char ch=myStr[i];
        if(isInt(ch))
        {
            result.push_back(ch);
        }
        else if(')'==ch)
        {
            opStack.push(ch);
        }
        else if(isOperator(ch))//操做符
        {
            while(true)
            {
                if(opStack.empty()||opStack.top()==')'||(opLevel(ch)>=opLevel(opStack.top())))
                {
                    opStack.push(ch);
                    break;
                }
                else
                {
                    result.push_back(opStack.top());
                    opStack.pop();
                }
            }
        }
        else if('('==ch)
        {
            while(opStack.top()!=')')
            {
                result.push_back(opStack.top());
                opStack.pop();
            }
            opStack.pop();
        }

    }
    while(!opStack.empty())
    {

        result.push_back(opStack.top());
        opStack.pop();

    }
    return result;

}
/*中綴-->後綴 算法
*/
string postOrder(string myStr)
{
    string result;
    stack<char> opStack;
    for(int i=0; i<myStr.length(); i++)
    {
        char ch=myStr[i];
        if(isInt(ch))
        {
            result.push_back(ch);
        }
        else if('('==ch)
        {
            opStack.push(ch);
        }
        else if(isOperator(ch))
        {
            while(true)
            {
                if(opStack.empty()||opStack.top()=='('||opLevel(ch)>=opLevel(opStack.top()))
                {
                    opStack.push(ch);
                    break;
                }
                else
                {
                    result.push_back(opStack.top());
                    opStack.pop();
                }
            }
        }
        else if(')'==ch)
        {
            while(opStack.top()!='(')
            {
                result.push_back(opStack.top());
                opStack.pop();
            }
            opStack.pop();
        }

    }
    while(!opStack.empty())
    {
        result.push_back(opStack.top());
        opStack.pop();
    }
    return result;
}
int main()
{
    string myStr;
    cin>>myStr;
    string result;
    result=priOrder(myStr);
    for(int i=result.length()-1; i>=0; i--)
    {
        cout<<result[i];
    }
    cout<<endl;
    result=postOrder(myStr);
    for(int i=0; i<=result.length(); i++)
    {
        cout<<result[i];
    }
    return 0;
}
相關文章
相關標籤/搜索