我有一個stdin輸入,如「33 44 55 + 66 * + =」(即Reverse Polish notation,RPN),我使用如下代碼來解析它。但是scanf(「%d」)讀取'+'並丟棄它,如何取消操作並使其被scanf(「%c」)讀取?解決問題的最佳方法是什麼?謝謝。如何使用while循環掃描數字和運算符系列?
while ((reta = scanf("%d", &operand)) == 1 || (retb = scanf(" %c ", &operator)) == 1) {
if (reta == 1) push(exprStack, operand);
else if (retb == 1) {
operand = pop(exprStack);
/* function pmtd executes some basic calculation, i.e., plus, minus, times and divide */
push(exprStack, pmtd(operator, pop(exprStack), operand));
}
}
你可能想看看'strtok(3)'。 – 2011-12-24 18:09:24
你必須使用'scanf()'?交互式輸入通常不合適。使用'fgets()'並自己解析輸入字符串。 – 2011-12-24 18:10:16
@Carl:strtok可能不會很好,deltrs將被strtok(3)丟棄。 – 2011-12-24 19:36:39