@Entry
@Component
struct Jsq {
@State display: string = '0'
@State firstOperand: string = ''
@State operator: string = ''
@State waitingForSecondOperand: boolean = false
appendDigit(digit: string) {
if (this.waitingForSecondOperand) {
this.display = digit
this.waitingForSecondOperand = false
} else {
this.display = this.display === '0' ? digit : this.display + digit
}
}
appendDot() {
if (this.waitingForSecondOperand) {
this.display = '0.'
this.waitingForSecondOperand = false
} else if (!this.display.includes('.')) {
this.display = this.display + '.'
}
}
setOperator(nextOperator: string) {
const inputValue = parseFloat(this.display)
if (this.operator && this.waitingForSecondOperand) {
this.operator = nextOperator
return
}
if (this.firstOperand === '') {
this.firstOperand = this.display
} else if (this.operator) {
const result = this.calculate(parseFloat(this.firstOperand), inputValue, this.operator)
this.display = String(result)
this.firstOperand = String(result)
}
this.waitingForSecondOperand = true
this.operator = nextOperator
}
calculate(first: number, second: number, op: string): number {
switch (op) {
case '+': return first + second
case '-': return first - second
case '×': return first * second
case '÷': return second !== 0 ? first / second : 0
default: return second
}
}
performEqual() {
if (!this.operator || this.waitingForSecondOperand) return
const secondOperand = parseFloat(this.display)
let result = this.calculate(parseFloat(this.firstOperand), secondOperand, this.operator)
if (result === 0 && this.operator === '÷') {
this.display = 'Error'
} else {
this.display = String(result)
if (!Number.isInteger(result)) {
this.display = parseFloat(this.display).toFixed(8).replace(/\.?0+$/, '')
}
}
this.firstOperand = ''
this.operator = ''
this.waitingForSecondOperand = false
}
clear() {
this.display = '0'
this.firstOperand = ''
this.operator = ''
this.waitingForSecondOperand = false
}
build() {
Column({ space: 15 }) {
Column({ space: 10 }) {
Text(this.display)
.fontSize(48)
.fontWeight(FontWeight.Bold)
.fontColor('#333')
.width('92%')
.textAlign(TextAlign.End)
.maxLines(1)
}
.width('100%')
.height('30%')
.justifyContent(FlexAlign.End)
.alignItems(HorizontalAlign.Center)
.padding({ right: 20, bottom: 20 })
Column({ space: 10 }) {
Row({ space: 10 }) {
Button('C')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#fff')
.backgroundColor('#ff6b6b')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.clear())
Button('÷')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#fff')
.backgroundColor('#ffa502')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.setOperator('÷'))
Button('×')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#fff')
.backgroundColor('#ffa502')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.setOperator('×'))
Button('⌫')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#fff')
.backgroundColor('#ffa502')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => {
if (this.display.length > 1) {
this.display = this.display.slice(0, -1)
} else {
this.display = '0'
}
})
}
.width('100%')
.height('15%')
.justifyContent(FlexAlign.SpaceEvenly)
Row({ space: 10 }) {
Button('7')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('7'))
Button('8')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('8'))
Button('9')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('9'))
Button('-')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#fff')
.backgroundColor('#ffa502')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.setOperator('-'))
}
.width('100%')
.height('15%')
.justifyContent(FlexAlign.SpaceEvenly)
Row({ space: 10 }) {
Button('4')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('4'))
Button('5')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('5'))
Button('6')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('6'))
Button('+')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#fff')
.backgroundColor('#ffa502')
.width('23%')
.height('100%')
.borderRadius(12)
.onClick(() => this.setOperator('+'))
}
.width('100%')
.height('15%')
.justifyContent(FlexAlign.SpaceEvenly)
Row({ space: 10 }) {
Column({ space: 10 }) {
Row({ space: 10 }) {
Button('1')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('32%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('1'))
Button('2')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('32%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('2'))
Button('3')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('32%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('3'))
}
.width('100%')
.height('15%')
.justifyContent(FlexAlign.SpaceEvenly)
Row({ space: 10 }) {
Button('0')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('32%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDigit('0'))
Button('.')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#333')
.backgroundColor('#fff')
.width('32%')
.height('100%')
.borderRadius(12)
.onClick(() => this.appendDot())
Button('=')
.fontSize(28)
.fontWeight(FontWeight.Medium)
.fontColor('#fff')
.backgroundColor('#1FA485')
.width('32%')
.height('100%')
.borderRadius(12)
.onClick(() => this.performEqual())
}
.width('100%')
.height('15%')
.justifyContent(FlexAlign.SpaceEvenly)
}
.width('73%')
}
}
.width('100%')
.height('70%')
.padding({ left: 10, right: 10 })
}
.width('100%')
.height('100%')
.backgroundColor('#f5f5f5')
}
}