C++栈计算器运行异常求助:垃圾值输出与栈未初始化问题
Hey there! Let's break down the issues in your code step by step—those garbage values and wrong outputs are coming from a few key bugs we can fix together.
1. Critical Bug in isDigit() Function
Your isDigit function has reversed logic right now. The condition c <= '0' && c <= '9' only returns true for characters with an ASCII value less than or equal to '0', which means it never recognizes actual digits (like '3', '4') correctly. This is the root cause of the garbage stack values you saw—your program never properly reads numbers into the stack!
Fix it by checking if the character falls between '0' and '9':
bool isDigit(char c) { return (c >= '0' && c <= '9'); // Swap the first <= to >= }
2. Unhandled Whitespace in Input
Your test inputs include spaces (e.g., "3 + 4 + 5"), but your code doesn't skip them. When the loop hits a space, it ignores it but leaves the val variable intact, leading to incorrect number parsing later. Add a check to skip whitespace:
while (pos < s.length()) { char spot = s[pos]; if (spot == ' ') { // Skip any whitespace characters pos++; continue; } // ... rest of your existing loop logic }
3. Wrong Operator Precedence for Parentheses
Your getPrecendence function assigns parentheses a higher priority (3) than multiplication/division (2), which breaks how expressions with parentheses are evaluated. Parentheses should be treated as a special case, not as a high-priority operator. Adjust the function:
int getPrecendence(char c) { switch (c) { case '+': case '-': return 1; case '*': case '/': return 2; case '(': // Left parenthesis has low priority for comparison return 0; case ')': // Right parenthesis doesn't need a precedence value here return -1; default: return -1; } }
4. Broken Closing Parenthesis Handling
The logic for processing ) has two major flaws:
- You push the final number inside the parentheses to the stack, then pop it immediately after calculating the result—this discards the value you just computed.
- The order of popping values and calculating was mixed up.
Fix the closing parenthesis branch:
else if (spot == ')') { vals.push(val); // Push the last number inside the parentheses while (ops.peek() != '(') { char op = ops.pop(); int val2 = vals.pop(); int val1 = vals.pop(); vals.push(operate(val1, val2, op)); } ops.pop(); // Remove the matching '(' from the operator stack val = vals.pop(); // Set val to the result of the parenthesized expression }
5. Flawed Final Expression Evaluation
The loop that processes remaining operators at the end of the string uses incorrect logic. You're mixing up the order of values popped from the stack, leading to wrong calculations. Replace that section with:
while (!ops.isEmpty()) { int val2 = vals.pop(); int val1 = vals.pop(); char op = ops.pop(); vals.push(operate(val1, val2, op)); } return vals.pop(); // Return the final computed result from the value stack
After Fixes
Once you apply all these changes, your test code will output the expected results:
12 17 23 35
内容的提问来源于stack exchange,提问作者Miguel Arturo Sanchez Cuello

