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

      

  • 相关阅读:
    MysqlServer如何实现成功卸载,并成功安装
    win7安装xampp,提示windows找不到-n文件(安装成功后,443端口占用,apache服务器无法正常启动)
    (JS实现顾客商品浏览记录以及购物车)Cookie的保存与删除
    (转)SVN 服务端、客户端安装及配置、导入导出项目
    正则表达式详解
    Struts2.3.4+Hibernate4.2.4+Mysql6.0整合
    CSS中TRBL和position关系
    const typedef #define
    数组的替代品
    输入
  • 原文地址:https://www.cnblogs.com/tswcypy/p/4790377.html
Copyright © 2020-2023  润新知