鸿蒙应用简单计算器代码及演示

复制代码
@Entry
@Component
struct Index {
  @State result: string[] = ['0']

  setParams(pattern: string) {
    const operators: string = '+-*/.'
    if (this.result.length == 1 && this.result[0] == '0') {
      if ('*/'.includes(pattern)) return
      this.result = [pattern]
    } else {
      const len = this.result.length
      const last = this.result.lastIndexOf('.')
      const slice = this.result.slice(last + 1)
      if (last != -1 && pattern == '.' && /^\d+$/.test(slice.join(''))) return
      if(!(operators.includes(this.result[len-1]) && operators.includes(pattern)))
      {
        this.result.push(pattern)
      }
    }
  }

  calc() {
    let res = 0
    const dd = this.result.join('').match(/[\-\+]?\d+(\.\d+)?([\/\*]\d+(\.?\d+)?)*/g)
    dd.forEach(item => {
      if (/^[\+\-]?\d+(\.\d+)?$/.test(item)) {
        res += +item
      } else {
        let rr = 0
        let isj = false
        let operator: string = "*"
        let rrr = item.match(/[\/\*]|(\d+(\.\d+)?)/g)
        for (let index = 0; index < rrr.length; index++) {
          const element = rrr[index];
          if (element == '-') {
            isj = true
          } else if ('*/'.includes(element)) {
            operator = element
          } else {
            if (rr == 0) {
              rr = +element
              continue
            }
            if (operator == '*') {
              rr *= +element
            } else {
              rr /= +element
            }
          }
          if (isj) {
            rr = 0 - rr
          }
        }
        res += rr
      }

    })
    this.result = [res + '']
  }
  
  
  build() {
    Grid() {
      GridItem() {
        Text(this.result.join(''))
          .padding(10)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .textAlign(TextAlign.End)
          .fontSize(50)

      }
      .columnStart(1)
      .columnEnd(4)
      .backgroundColor('#FFC0CB')

      GridItem() {
        Button('CE')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            if (this.result.length == 1) {
              if (this.result[0] == '0') return
              this.result = ['0']
              return
            }
            this.result.pop()
          })
      }

      GridItem() {
        Button('C')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            this.result = ['0']
          })
      }

      GridItem() {
        Button('/')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            this.setParams('/')
          })
      }
      .backgroundColor('#FFC0CB')

      GridItem() {
        Button('*')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            this.setParams('*')
          })
      }

      ForEach(['7', '8', '9'], (item: string) => {
        GridItem() {
          Button(item)
            .type(ButtonType.Normal)
            .height('100%')
            .width('100%')
            .fontColor('#000000')
            .backgroundColor('#FFC0CB')
            .onClick(() => {
              this.setParams(item)
            })
        }
      }, item => item)


      GridItem() {
        Button('-')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            this.setParams('-')
          })
      }

      ForEach(['4', '5', '6'], (item: string) => {
        GridItem() {
          Button(item)
            .type(ButtonType.Normal)
            .height('100%')
            .width('100%')
            .fontColor('#000000')
            .backgroundColor('#FFC0CB')
            .onClick(() => {
              this.setParams(item)
            })
        }
      }, item => item)

      GridItem() {
        Button('+')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            this.setParams('+')
          })
      }

      ForEach(['1', '2', '3'], (item: string) => {
        GridItem() {
          Button(item)
            .type(ButtonType.Normal)
            .height('100%')
            .width('100%')
            .fontColor('#000000')
            .backgroundColor('#FFC0CB')
            .onClick(() => {
              this.setParams(item)
            })
        }
      }, item => item)

      GridItem() {
        Button('=')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            this.calc()
          })
      }
      .rowStart(5)
      .rowEnd(6)
      .backgroundColor('#FFC0CB')

      GridItem() {
        Button('0')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            this.setParams('0')
          })
      }
      .columnStart(1)
      .columnEnd(2)

      GridItem() {
        Button('.')
          .type(ButtonType.Normal)
          .height('100%')
          .width('100%')
          .fontColor('#000000')
          .backgroundColor('#FFC0CB')
          .onClick(() => {
            this.setParams('.')
          })
      }
    }
    .columnsGap(10)
    .rowsGap(10)
    .padding(10)
    .rowsTemplate('2fr 1fr 1fr 1fr 1fr 1fr')
    .columnsTemplate('1fr 1fr 1fr 1fr')
    .backgroundColor('#FFFFFF')
  }
}

效果图:

编辑器为Dev Eco

相关推荐
zhcong_6 分钟前
Nginx+Tomcat 负载均衡群集
服务器·负载均衡·lvs
王二蛋与他的张大花16 分钟前
HarmonyOS运动开发:如何用mpchart绘制运动配速图表
harmonyos
程序员小刘19 分钟前
【HarmonyOS 5】生活与服务开发实践详解以及服务卡片案例
华为·生活·harmonyos
wanhengidc34 分钟前
高防服务器能够抵御哪些网络攻击呢?
运维·服务器
明月看潮生1 小时前
青少年编程与数学 01-011 系统软件简介 02 UNIX操作系统
服务器·青少年编程·操作系统·unix·系统软件
凯勒姆2 小时前
6.linux文本内容显示cat,more,less
linux·运维·服务器
恒拓高科WorkPlus2 小时前
BeeWorks 协同办公能力:局域网内企业级协作的全场景重构
服务器·网络·重构
xiaomu_3473 小时前
基于Linux系统docker封装exe
linux·运维·服务器·docker
小草帽学编程4 小时前
鸿蒙Next开发真机调试签名申请流程
android·华为·harmonyos
陈奕昆4 小时前
5.2 HarmonyOS NEXT应用性能诊断与优化:工具链、启动速度与功耗管理实战
华为·harmonyos