如何在JavaScript计算器的栈中存储完整数值元素
Fixing Stack to Store Full Numbers Instead of Individual Digits
Got it, let's sort this out so your stack holds complete numbers like ["23", "+", "55"] instead of splitting digits into separate entries. The core problem with your current code is that every button press—whether it's a digit or operator—gets pushed to the stack right away, which breaks multi-digit numbers apart. Here's how to adjust your code to fix this:
Key Changes Needed
- Add a variable to track the current number being typed (so we can build multi-digit numbers before pushing them to the stack)
- Only push the complete number to the stack when an operator or equals sign is clicked
- Reset the current number tracker after pushing it to the stack or when starting a new calculation post-equals
Modified Working Code
<script> var stack = new Array(); const buttons = document.querySelectorAll("input[type=button]"); const length = buttons.length; for (let i = 0; i < length; i++) { buttons[i].addEventListener("click", handle); } var prevop = ''; let currentNumber = ''; // Track the current multi-digit number being input function handle(event) { const value = event.target.value; // If we're starting a new calculation after equals, reset everything if (prevop === '=') { document.getElementById("textfield").value = ""; stack = []; currentNumber = ''; } switch (value) { case "+": case "-": case "*": case "/": // Push the completed current number to stack if it exists if (currentNumber) { stack.push(currentNumber); currentNumber = ''; // Reset for next number } stack.push(value); // Push the operator to stack document.getElementById("textfield").value += value; console.log(`${value} was clicked, stack now:`, stack); break; case "=": // Push the final number to stack before calculating if (currentNumber) { stack.push(currentNumber); currentNumber = ''; } console.log("= was clicked, stack contents:", stack); // Calculate using the stack (or use eval on the input field if preferred) const result = eval(document.getElementById("textfield").value); document.getElementById("textfield").value += value + result; break; default: // It's a digit currentNumber += value; // Build the multi-digit number document.getElementById("textfield").value += value; console.log(`${value} was clicked, current number:`, currentNumber); break; } prevop = value; } </script>
How This Works
currentNumbervariable: This stores digits as you click them, so "2" followed by "3" becomes"23"instead of two separate stack entries.- Operator clicks: When you hit
+,-,*, or/, we first push the fully builtcurrentNumberto the stack, then push the operator, and resetcurrentNumberto start fresh for the next number. - Equals click: We push the last
currentNumberto the stack (so the stack has the full sequence like["23", "+", "55"]), then calculate the result. - Post-equals reset: If you click a digit after hitting
=, we clear the stack,currentNumber, and input field to start a new calculation cleanly.
You can test this by clicking "2" → "3" → "+" → "5" → "5" → "="—your stack will log ["23", "+", "55"] exactly as you wanted!
内容的提问来源于stack exchange,提问作者user6889331
相关产品推荐
相关产品推荐

