zoukankan      html  css  js  c++  java
  • 中缀转前缀

    
    # infix [] -> 中缀表达式 ; s2[] -> 栈的顺序表 ; top2 -> 栈顶指针 ; len表达式长度
    void infixToPostFix(char infix[ ], char s2[ ], int &top2, int len)
    {
        char s1[maxSize];       #s1辅助栈
        int top1 = -1;
        int i = len-1;
        while(i >= 0)
        {
            if('0' <= infix[i] && infix[i] <= '9')
            {
                s2[++top2] = infix[i];
                --i;
            }
            else if(infix[i] == ')' )
            {
                s1[++top1] = ')';
                 --i; 
            }
            else if (infix[i] == '+' || infix[i] == '-' ||
                        infix[i] == '*' || infix[i] == '/')
            {
                if(top1 == -1 || s1[top1] == ')' ||      #当栈为空
                    getPriority(infix[i]) >= getPriority(s1[top1]))     #当前优先级 >= 栈顶优先级
                {
                    s1[++top1] = infix[i];
                    --i;
                }
                else
                    s2[++top2] = s1[top1--];      #将运算符入栈到s2中
            }
            else if(infix[i] == '(')      #当遇到左括号
            {
                while(s1[top1] != ')')
                    s2[++top2] = s1[top1--];
                --top1;
                --i;
            }
        }
        while(top1 != -1)
            s2[++top2] = s1[top--];
    }
    
    
  • 相关阅读:
    yii之behaviors
    查看windows系统信息
    idm chrome扩展被阻止解决办法
    音乐乐理基础
    bootstrap4
    七牛上传整合CI
    提升上传速度
    卡漫绘图
    指针的操作
    定语从句八个易混淆
  • 原文地址:https://www.cnblogs.com/dsbz/p/14458318.html
Copyright © 2011-2022 走看看