127 lines
3.1 KiB
TypeScript
127 lines
3.1 KiB
TypeScript
// pages/calculator/calculator.ts
|
|
Page({
|
|
data: {
|
|
displayValue: '0',
|
|
history: '',
|
|
operator: null as string | null,
|
|
firstOperand: null as number | null,
|
|
waitingForSecondOperand: false,
|
|
},
|
|
|
|
onLoad() {
|
|
},
|
|
|
|
onDigit(e: any) {
|
|
const digit = e.currentTarget.dataset.digit;
|
|
const { displayValue, waitingForSecondOperand } = this.data;
|
|
|
|
if (waitingForSecondOperand) {
|
|
this.setData({
|
|
displayValue: digit,
|
|
waitingForSecondOperand: false
|
|
});
|
|
} else {
|
|
this.setData({
|
|
displayValue: displayValue === '0' ? digit : displayValue + digit
|
|
});
|
|
}
|
|
},
|
|
|
|
onDot() {
|
|
const { displayValue, waitingForSecondOperand } = this.data;
|
|
if (waitingForSecondOperand) {
|
|
this.setData({
|
|
displayValue: '0.',
|
|
waitingForSecondOperand: false
|
|
});
|
|
} else if (displayValue.indexOf('.') === -1) {
|
|
this.setData({
|
|
displayValue: displayValue + '.'
|
|
});
|
|
}
|
|
},
|
|
|
|
onClear() {
|
|
this.setData({
|
|
displayValue: '0',
|
|
history: '',
|
|
operator: null,
|
|
firstOperand: null,
|
|
waitingForSecondOperand: false
|
|
});
|
|
},
|
|
|
|
onDelete() {
|
|
const { displayValue } = this.data;
|
|
this.setData({
|
|
displayValue: displayValue.length > 1 ? displayValue.slice(0, -1) : '0'
|
|
});
|
|
},
|
|
|
|
onOperator(e: any) {
|
|
const nextOperator = e.currentTarget.dataset.op;
|
|
const { displayValue, operator, firstOperand } = this.data;
|
|
const inputValue = parseFloat(displayValue);
|
|
|
|
if (operator && this.data.waitingForSecondOperand) {
|
|
this.setData({
|
|
operator: nextOperator,
|
|
history: `${firstOperand} ${nextOperator}`
|
|
});
|
|
return;
|
|
}
|
|
|
|
let newFirstOperand = firstOperand;
|
|
|
|
if (firstOperand == null) {
|
|
newFirstOperand = inputValue;
|
|
} else if (operator) {
|
|
const result = this.performCalculation(operator, firstOperand, inputValue);
|
|
newFirstOperand = result;
|
|
this.setData({
|
|
displayValue: String(result),
|
|
});
|
|
}
|
|
|
|
this.setData({
|
|
firstOperand: newFirstOperand,
|
|
waitingForSecondOperand: true,
|
|
operator: nextOperator,
|
|
history: `${newFirstOperand} ${nextOperator}`
|
|
});
|
|
},
|
|
|
|
onEqual() {
|
|
const { displayValue, operator, firstOperand } = this.data;
|
|
const inputValue = parseFloat(displayValue);
|
|
|
|
if (operator && firstOperand != null) {
|
|
const result = this.performCalculation(operator, firstOperand, inputValue);
|
|
this.setData({
|
|
displayValue: String(result),
|
|
firstOperand: null,
|
|
operator: null,
|
|
waitingForSecondOperand: true,
|
|
history: ''
|
|
});
|
|
}
|
|
},
|
|
|
|
performCalculation(operator: string, firstOperand: number, secondOperand: number) {
|
|
switch (operator) {
|
|
case '+':
|
|
return firstOperand + secondOperand;
|
|
case '-':
|
|
return firstOperand - secondOperand;
|
|
case '*':
|
|
return firstOperand * secondOperand;
|
|
case '/':
|
|
return firstOperand / secondOperand;
|
|
case '%':
|
|
return firstOperand % secondOperand;
|
|
default:
|
|
return secondOperand;
|
|
}
|
|
}
|
|
});
|