爲什麼下面的代碼在做operand * 10
?爲什麼它不只是做operand = (expression[i] - '0');
而不是operand = (operand*10) + (expression[i] - '0');
爲什麼「操作數=(操作數* 10)+(表達式[i] - '0');」
else if(IsNumericDigit(expression[i]))
{
int operand = 0;
while(i<expression.length() && IsNumericDigit(expression[i]))
{
operand = (operand*10) + (expression[i] - '0');
// why is he doing operand *10?
// example : if have a string 2 3 * 3 4 +.
// It is pushing 23 on stack rather than 2 and 3
i++;
}
i--;
S.push(operand);
}
如果他做的操作數=(表達式[i] - '0')並且表達式爲「123」,那麼在while循環操作數爲3之後123. – user1438832
請編輯問題主體以包含實際問題。標題應該只是問題的簡短摘要,評論應該在代碼之外作爲主要問題。還請添加一些上下文,並詳細說明您不確定的內容。但首先請花一些時間[閱讀如何提出好問題](http://stackoverflow.com/help/how-to-ask)。 –
當然,這個想法是處理像42這樣的大數字。呃,它應該退出while循環,因爲''不是數字數字。你有沒有試過調試它? – Rene