数学运算
Math operations
我是初学者,这是我第一次在这里写作。所以我的目标是制作一个强大的计算器,现在我发现自己在计算过程中遇到更改操作的问题 - 例如:我得到 1+1 但当我处于 1+ 并想将其更改为-,/ 或 * 我不能。试图找到解决方案,但其他人以自己的方式进行计算,所以我在这里问。起草你的问题是建议显示最少的代码,所以我 post 我认为下面是相关的。如果有人需要更多代码来帮助我解决这个问题,我可以发送代码笔 link 但建议不要发送到这里。
class Calculator {
constructor(previousOperandTextElement, currentOperandTextElement){
this.previousOperandTextElement = previousOperandTextElement;
this.currentOperandTextElement = currentOperandTextElement;
this.readyToReset = false;
this.clear();
}
clear() {
this.currentOperand = "";
this.previousOperand = "";
this.operation = undefined;
}
delete() {
this.currentOperand = this.currentOperand.toString().slice(0, -1);
}
appendNumber(number) {
if (number === "." && this.currentOperand.includes(".")) return
this.currentOperand = this.currentOperand.toString() + number.toString();
}
chooseOperation(operation) {
if (this.currentOperand === "") return
if (this.currentOperand !== "" && this.previousOperand !== "") this.compute();
this.operation = operation;
this.previousOperand = this.currentOperand;
this.currentOperand = "";
}
compute() {
let computation
const prev = parseFloat(this.previousOperand);
const current = parseFloat(this.currentOperand);
if (isNaN(prev) || isNaN(current)) return;
switch (this.operation) {
case "+":
computation = prev + current;
break;
case "-":
computation = prev - current;
break;
case "*":
computation = prev * current;
break;
case "÷":
computation = prev / current;
break;
default:
return;
}
this.readyToReset = true;
this.currentOperand = computation;
this.operation = undefined;
this.previousOperand = "";
}
updateDisplay() {
this.currentOperandTextElement.innerText = this.currentOperand;
if (this.operation != null) {
this.previousOperandTextElement.innerText =
`${this.previousOperand} ${this.operation}`
} else {
this.previousOperandTextElement.innerText = "";
}
}
}
const numberButtons = document.querySelectorAll("[data-number]");
const operationButtons = document.querySelectorAll("[data-operation]");
const equalsButton = document.querySelector("[data-equals]");
const deleteButton = document.querySelector("[data-delete]");
const allClearButton = document.querySelector("[data-clear]");
const previousOperandTextElement = document.querySelector("[data-previous-operand]");
const currentOperandTextElement = document.querySelector("[data-current-operand]");
const calculator = new Calculator(previousOperandTextElement,
currentOperandTextElement);
allClearButton.addEventListener("click", () => {
calculator.clear();
calculator.updateDisplay();
})
numberButtons.forEach(button => {
button.addEventListener("click", () => {
if(calculator.previousOperand === "" &&
calculator.currentOperand !== "" &&
calculator.readyToReset) {
calculator.currentOperand = "";
calculator.readyToReset = false;
}
calculator.appendNumber(button.innerText)
calculator.updateDisplay();
})
})
operationButtons.forEach(button => {
button.addEventListener("click", () => {
calculator.chooseOperation(button.innerText)
calculator.updateDisplay();
})
})
equalsButton.addEventListener("click", button => {
calculator.compute();
calculator.updateDisplay();
})
deleteButton.addEventListener("click", button => {
calculator.delete();
calculator.updateDisplay();
})
.calculator-grid{
display: grid;
justify-content: center;
align-items: center;
min-height: 100;
grid-template-columns: repeat(4, 80px);
}
.calculator-grid > button {
cursor: pointer;
font-size: 2em;
border: 1px solid rgb(0, 0, 0);
outline: none;
background-color: rgb(233, 129, 129);
margin: 1px;
border-radius: 10px;
}
.output {
grid-column: 1 / -1;
background-color: rgb(251, 251, 251);
display: flex;
align-items: flex-end;
justify-content: space-between;
flex-direction: column;
padding: 10px;
height: 100px;
}
#kalkulacka{
width: 360px;
height: 520px;
background-color: rgb(0, 255, 221);
margin: auto;
top: 20px;
position: center;
border-radius: 10px;
}
.span-two{
grid-column: span 2;
}
<div id="kalkulacka" class="calculator-grid">
<div class="output">
<div data-history class="history">22</div>
<div data-previous-operand class="previous-operand">22</div>
<div data-current-operand class="current-operand">22</div>
</div>
<button data-clear>AC</button>
<button data-delete>DEL</button>
<button data-converse>+/-</button>
<button data-operation>÷</button>
<button data-number>7</button>
<button data-number>8</button>
<button data-number>9</button>
<button data-operation>*</button>
<button data-number>4</button>
<button data-number>5</button>
<button data-number>6</button>
<button data-operation>-</button>
<button data-number>1</button>
<button data-number>2</button>
<button data-number>3</button>
<button data-operation>+</button>
<button data-number>.</button>
<button data-number>0</button>
<button data-equals class="span-two">=</button>
</div>
我想这就是你想要的。
在您的构造函数中,添加:
this.lastEntryWasOperation = false;
在appendNumber()
的开头,加上:
this.lastEntryWasOperation = false;
然后将chooseOperation()
改为:
if (this.lastEntryWasOperation)
this.operation = operation;
else{
if (this.currentOperand === "") return
if (this.currentOperand !== "" && this.previousOperand !== "") this.compute();
this.operation = operation;
this.previousOperand = this.currentOperand;
this.currentOperand = "";
}
this.lastEntryWasOperation = true;
因此我们跟踪最后按下的键是否是操作符,如果是操作符则将标志设置为 true,否则设置为 false。
然后,如果第二个运算符紧跟在另一个运算符之后,我们只需覆盖它而不是移动操作数。
我是初学者,这是我第一次在这里写作。所以我的目标是制作一个强大的计算器,现在我发现自己在计算过程中遇到更改操作的问题 - 例如:我得到 1+1 但当我处于 1+ 并想将其更改为-,/ 或 * 我不能。试图找到解决方案,但其他人以自己的方式进行计算,所以我在这里问。起草你的问题是建议显示最少的代码,所以我 post 我认为下面是相关的。如果有人需要更多代码来帮助我解决这个问题,我可以发送代码笔 link 但建议不要发送到这里。
class Calculator {
constructor(previousOperandTextElement, currentOperandTextElement){
this.previousOperandTextElement = previousOperandTextElement;
this.currentOperandTextElement = currentOperandTextElement;
this.readyToReset = false;
this.clear();
}
clear() {
this.currentOperand = "";
this.previousOperand = "";
this.operation = undefined;
}
delete() {
this.currentOperand = this.currentOperand.toString().slice(0, -1);
}
appendNumber(number) {
if (number === "." && this.currentOperand.includes(".")) return
this.currentOperand = this.currentOperand.toString() + number.toString();
}
chooseOperation(operation) {
if (this.currentOperand === "") return
if (this.currentOperand !== "" && this.previousOperand !== "") this.compute();
this.operation = operation;
this.previousOperand = this.currentOperand;
this.currentOperand = "";
}
compute() {
let computation
const prev = parseFloat(this.previousOperand);
const current = parseFloat(this.currentOperand);
if (isNaN(prev) || isNaN(current)) return;
switch (this.operation) {
case "+":
computation = prev + current;
break;
case "-":
computation = prev - current;
break;
case "*":
computation = prev * current;
break;
case "÷":
computation = prev / current;
break;
default:
return;
}
this.readyToReset = true;
this.currentOperand = computation;
this.operation = undefined;
this.previousOperand = "";
}
updateDisplay() {
this.currentOperandTextElement.innerText = this.currentOperand;
if (this.operation != null) {
this.previousOperandTextElement.innerText =
`${this.previousOperand} ${this.operation}`
} else {
this.previousOperandTextElement.innerText = "";
}
}
}
const numberButtons = document.querySelectorAll("[data-number]");
const operationButtons = document.querySelectorAll("[data-operation]");
const equalsButton = document.querySelector("[data-equals]");
const deleteButton = document.querySelector("[data-delete]");
const allClearButton = document.querySelector("[data-clear]");
const previousOperandTextElement = document.querySelector("[data-previous-operand]");
const currentOperandTextElement = document.querySelector("[data-current-operand]");
const calculator = new Calculator(previousOperandTextElement,
currentOperandTextElement);
allClearButton.addEventListener("click", () => {
calculator.clear();
calculator.updateDisplay();
})
numberButtons.forEach(button => {
button.addEventListener("click", () => {
if(calculator.previousOperand === "" &&
calculator.currentOperand !== "" &&
calculator.readyToReset) {
calculator.currentOperand = "";
calculator.readyToReset = false;
}
calculator.appendNumber(button.innerText)
calculator.updateDisplay();
})
})
operationButtons.forEach(button => {
button.addEventListener("click", () => {
calculator.chooseOperation(button.innerText)
calculator.updateDisplay();
})
})
equalsButton.addEventListener("click", button => {
calculator.compute();
calculator.updateDisplay();
})
deleteButton.addEventListener("click", button => {
calculator.delete();
calculator.updateDisplay();
})
.calculator-grid{
display: grid;
justify-content: center;
align-items: center;
min-height: 100;
grid-template-columns: repeat(4, 80px);
}
.calculator-grid > button {
cursor: pointer;
font-size: 2em;
border: 1px solid rgb(0, 0, 0);
outline: none;
background-color: rgb(233, 129, 129);
margin: 1px;
border-radius: 10px;
}
.output {
grid-column: 1 / -1;
background-color: rgb(251, 251, 251);
display: flex;
align-items: flex-end;
justify-content: space-between;
flex-direction: column;
padding: 10px;
height: 100px;
}
#kalkulacka{
width: 360px;
height: 520px;
background-color: rgb(0, 255, 221);
margin: auto;
top: 20px;
position: center;
border-radius: 10px;
}
.span-two{
grid-column: span 2;
}
<div id="kalkulacka" class="calculator-grid">
<div class="output">
<div data-history class="history">22</div>
<div data-previous-operand class="previous-operand">22</div>
<div data-current-operand class="current-operand">22</div>
</div>
<button data-clear>AC</button>
<button data-delete>DEL</button>
<button data-converse>+/-</button>
<button data-operation>÷</button>
<button data-number>7</button>
<button data-number>8</button>
<button data-number>9</button>
<button data-operation>*</button>
<button data-number>4</button>
<button data-number>5</button>
<button data-number>6</button>
<button data-operation>-</button>
<button data-number>1</button>
<button data-number>2</button>
<button data-number>3</button>
<button data-operation>+</button>
<button data-number>.</button>
<button data-number>0</button>
<button data-equals class="span-two">=</button>
</div>
我想这就是你想要的。
在您的构造函数中,添加:
this.lastEntryWasOperation = false;
在appendNumber()
的开头,加上:
this.lastEntryWasOperation = false;
然后将chooseOperation()
改为:
if (this.lastEntryWasOperation)
this.operation = operation;
else{
if (this.currentOperand === "") return
if (this.currentOperand !== "" && this.previousOperand !== "") this.compute();
this.operation = operation;
this.previousOperand = this.currentOperand;
this.currentOperand = "";
}
this.lastEntryWasOperation = true;
因此我们跟踪最后按下的键是否是操作符,如果是操作符则将标志设置为 true,否则设置为 false。
然后,如果第二个运算符紧跟在另一个运算符之后,我们只需覆盖它而不是移动操作数。