增加小程序,修改游戏前端
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "计算器"
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
<!--pages/calculator/calculator.wxml-->
|
||||
<view class="calculator">
|
||||
<view class="screen">
|
||||
<view class="history">{{history}}</view>
|
||||
<view class="result">{{displayValue}}</view>
|
||||
</view>
|
||||
<view class="keypad">
|
||||
<view class="btn operator" bindtap="onClear">C</view>
|
||||
<view class="btn operator" bindtap="onDelete">DEL</view>
|
||||
<view class="btn operator" bindtap="onOperator" data-op="%">%</view>
|
||||
<view class="btn operator" bindtap="onOperator" data-op="/">÷</view>
|
||||
|
||||
<view class="btn" bindtap="onDigit" data-digit="7">7</view>
|
||||
<view class="btn" bindtap="onDigit" data-digit="8">8</view>
|
||||
<view class="btn" bindtap="onDigit" data-digit="9">9</view>
|
||||
<view class="btn operator" bindtap="onOperator" data-op="*">×</view>
|
||||
|
||||
<view class="btn" bindtap="onDigit" data-digit="4">4</view>
|
||||
<view class="btn" bindtap="onDigit" data-digit="5">5</view>
|
||||
<view class="btn" bindtap="onDigit" data-digit="6">6</view>
|
||||
<view class="btn operator" bindtap="onOperator" data-op="-">-</view>
|
||||
|
||||
<view class="btn" bindtap="onDigit" data-digit="1">1</view>
|
||||
<view class="btn" bindtap="onDigit" data-digit="2">2</view>
|
||||
<view class="btn" bindtap="onDigit" data-digit="3">3</view>
|
||||
<view class="btn operator" bindtap="onOperator" data-op="+">+</view>
|
||||
|
||||
<view class="btn zero" bindtap="onDigit" data-digit="0">0</view>
|
||||
<view class="btn" bindtap="onDot">.</view>
|
||||
<view class="btn equal" bindtap="onEqual">=</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,74 @@
|
||||
/* pages/calculator/calculator.wxss */
|
||||
page {
|
||||
height: 100%;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
.calculator {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.screen {
|
||||
flex: 1;
|
||||
background-color: #333;
|
||||
color: white;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: flex-end;
|
||||
align-items: flex-end;
|
||||
padding: 20rpx;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.history {
|
||||
font-size: 40rpx;
|
||||
color: #aaa;
|
||||
margin-bottom: 10rpx;
|
||||
}
|
||||
|
||||
.result {
|
||||
font-size: 80rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.keypad {
|
||||
background-color: #fff;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
grid-gap: 1px;
|
||||
background-color: #ccc; /* Gap color */
|
||||
}
|
||||
|
||||
.btn {
|
||||
background-color: #fff;
|
||||
height: 150rpx;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
font-size: 40rpx;
|
||||
active-color: #eee;
|
||||
}
|
||||
|
||||
.btn:active {
|
||||
background-color: #eee;
|
||||
}
|
||||
|
||||
.operator {
|
||||
color: #ff9500;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.equal {
|
||||
background-color: #ff9500;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.equal:active {
|
||||
background-color: #e08900;
|
||||
}
|
||||
|
||||
.zero {
|
||||
grid-column: span 2;
|
||||
}
|
||||
Reference in New Issue
Block a user