【发布时间】:2021-07-10 08:46:48
【问题描述】:
我想做后缀功能。但是如果我执行这个程序,它就会变成无限加载。我想我有足够的案例分类。所以,我不知道添加更多案例的条件,请让我知道我错过了什么
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);
}
}
【问题讨论】:
-
编辑问题以提供minimal reproducible example,包括可编译和执行的完整程序、重现问题的示例输入、预期输出以及观察到的输出或行为。
-
试试debugging...你读过Shunting-yard algorithm吗?
-
在编辑代码时,我发现您遇到了其他问题。 en.wikipedia.org/wiki/Dangling_else 当你编写嵌套的 if 语句时,总是会正确缩进并且不会错过任何大括号!