首页 > 解决方案 > 我想做后缀功能,我要添加哪种情况?

问题描述

我想做后缀功能。但是如果我执行这个程序,它就会变成无限加载。我想我有足够的案例分类。所以,我不知道添加更多案例的条件,请让我知道我错过了什么

void toPostfix() {
    /* infixExp pointer which read one bye on character */
    char *exp = infixExp;

    /* variable for storing char temporarily */
    char x; 
    
    while (*exp != '\0') //iteration 
    {
        if (getPriority(*exp) == operand) // if it is operated
        {
            charCat(exp); //put into postfixExp.
        }
        else 
            if (getPriority(*exp) == rparen) { /* In the case of')', the operator is subtracted from the postfixstack until'(' appears and put into postfixExp*/
                while (getPriority(postfixStack[postfixStackTop]) != lparen) {
                    x = postfixPop(); // subtract operator from postfixstack
                    charCat(&x);//put in postfixExp
                }

                postfixPop(); //The'(' brackets are removed from postfixstack.
            } else { /*If the operator you are trying to put in postfixstack has a lower priority than postfixStack[postfixStackTop]. After subtracting postfixStack[postfixStackTop], put it in postfixExp and enter postfixstack*/
                while (getPriority(postfixStack[postfixStackTop]) >= getPriority(*exp)) {
                    x = postfixPop();// Subtract operator from postfixstack.
                    charCat(&x);// put it in postfixExp
                
                }

                postfixPush(*exp); // Put the operator in postfixstack.
            }      
            
            exp++; //exp has the address value of infixExp, so increase the address value by 1 byte.
    }

    while (postfixStack[postfixStackTop] != '\0') {
        x = postfixPop();
        charCat(&x);
    }
}

标签: calgorithm

解决方案


            while (getPriority(postfixStack[postfixStackTop]) >= getPriority(*exp))

一条线

          if (postfixStack[postfixStackTop] != '(')

缺少,因为如果堆栈顶部有括号运算符,则不允许从while循环中的后缀堆栈中弹出任何运算符,而与其优先级无关。


推荐阅读