HarmonyOS使用Grid网格实现计算器功能实现

ops/2025/1/18 22:37:39/

使用Grid网格处理,实现了计算器的加减乘除功能

@Entry
@Component
struct GridPage {@State str: string = ""; //暂存区@State num: string = "0"; //输入区@State flagNum: boolean = false; //标识build() {Column() {Grid() {GridItem() {Text(this.str) //默认为空}.columnStart(1) //colum表示列.columnEnd(4) //1-4列.backgroundColor(Color.Pink)GridItem() {Text(this.num.toString())// 默认为0.fontSize(40).fontWeight(FontWeight.Normal)}.columnStart(1) //colum表示列.columnEnd(4) //1-4列.backgroundColor(Color.White)GridItem() {Button('CE', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {const lastChar = this.str[this.str.length - 1]; // 获取最后一个字符if (lastChar === '=') { //清空操作this.str = "";}this.num = "0"console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('C', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {this.num = '0' //重置this.str = '' //重置console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('/', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {const flag = this.str[this.str.length - 1] === '='if (flag) {this.str = this.num + "/"; //如果结果已经计算,就重置为/} else {if (!this.flagNum) { //防止多打印出/线标识const flag = this.str[this.str.length - 1] === '/' //判断最后一个字符是不是/if (!flag) {this.str = this.num + "/"; //如果不是则添加一个} else { //如果是则进行计算const result = parseAndChu(this.str, this.num);this.num = result.toString();this.str = result.toString() + "/";}} else {//如果不是则默认为初始第一个const st = removeLastIfOperator(this.str) + "/";this.str = st;}}this.flagNum = true;console.info(`str组件内容是:${this.str}`)})}.backgroundColor(Color.Red)GridItem() {Button('*', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {const flag = this.str[this.str.length - 1] === '='if (flag) {this.str = this.num + "*";} else {if (!this.flagNum) {const flag = this.str[this.str.length - 1] === '*'if (!flag) {this.str = this.num + "*";} else {const result = parseAndCheng(this.str, this.num);this.num = result.toString();this.str = result.toString() + "*";}} else {const st = removeLastIfOperator(this.str) + "*";this.str = st;}}this.flagNum = true;console.info(`str组件内容是:${this.str}`)})}.backgroundColor(Color.Red)GridItem() {Button('7', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "7"} else {this.num = this.num == '0' ? "7" : this.num + "7"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('8', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "8"} else {this.num = this.num == '0' ? "8" : this.num + "8"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('9', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "9"} else {this.num = this.num == '0' ? "9" : this.num + "9"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('-', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {const flag = this.str[this.str.length - 1] === '='if (flag) {this.str = this.num + "-";} else {if (!this.flagNum) {const flag = this.str[this.str.length - 1] === '-'if (!flag) {this.str = this.num + "-";} else {const result = parseAndJian(this.str, this.num);this.num = result.toString();this.str = result.toString() + "-";}} else {const st = removeLastIfOperator(this.str) + "-";this.str = st;}}this.flagNum = true;console.info(`str组件内容是:${this.str}`)})}.backgroundColor(Color.Red)GridItem() {Button('4', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "4"} else {this.num = this.num == '0' ? "4" : this.num + "4"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('5', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "5"} else {this.num = this.num == '0' ? "5" : this.num + "5"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('6', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "6"} else {this.num = this.num == '0' ? "6" : this.num + "6"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('+', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {const flag = this.str[this.str.length - 1] === '='if (flag) {this.str = this.num + "+";} else {if (!this.flagNum) {const flag = this.str[this.str.length - 1] === '+'if (!flag) {this.str = this.num + "+";} else {const result = parseAndAdd(this.str, this.num);this.num = result.toString();this.str = result.toString() + "+";}} else {const st = removeLastIfOperator(this.str) + "+";this.str = st;}}this.flagNum = true;console.info(`str组件内容是:${this.str}`)})}.backgroundColor(Color.Red)GridItem() {Button('1', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "1"} else {this.num = this.num == '0' ? "1" : this.num + "1"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('2', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "2"} else {this.num = this.num == '0' ? "2" : this.num + "2"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('3', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = "3"} else {this.num = this.num == '0' ? "3" : this.num + "3"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)GridItem() {Button('=', { type: ButtonType.Normal, stateEffect: true}).width(80).height(160).onClick(() => {if (!this.flagNum) {const flag = this.str[this.str.length - 1] === '='if (!flag) {const lastChar = this.str[this.str.length - 1]; // 获取最后一个字符if (lastChar === '+') {const result = parseAndAdd(this.str, this.num);this.str = this.str + this.num + "=";this.num = result.toString();} else if (lastChar === '-') {const result = parseAndJian(this.str, this.num);this.str = this.str + this.num + "=";this.num = result.toString();} else if (lastChar === '*') {const result = parseAndCheng(this.str, this.num);this.str = this.str + this.num + "=";this.num = result.toString();} else if (lastChar === '/') {const result = parseAndChu(this.str, this.num);this.str = this.str + this.num + "=";this.num = result.toString();}}} else {const flag = this.str[this.str.length - 1] === '='if (flag) {if (this.str === '') {this.str = this.num + "="}return;}if (this.str === '') {this.str = this.num + "="return;}const st = removeLastIfOperator(this.str) + "=";this.str = st;}this.flagNum = true;console.info(`str组件内容是:${this.str}`)})}.rowStart(5) //开始第5行.rowEnd(6) //结束第6行.backgroundColor(Color.Red)GridItem() {Button('0', { type: ButtonType.Normal, stateEffect: true}).width(160).height(80).onClick(() => {if (this.flagNum) {this.num = "0"} else {this.num = this.num == '0' ? "0" : this.num + "0"}this.flagNum = false;console.info(`text组件内容是:${this.num}`)})}.columnStart(1) //第一列.columnEnd(2) //第二列.backgroundColor(Color.Grey)GridItem() {Button('.', { type: ButtonType.Normal, stateEffect: true}).width(80).height(80).onClick(() => {if (this.flagNum) {this.num = this.num+"."} else {if(this.str === ''){this.num = this.num+"."}else{this.num = this.num == '0' ? "0." : this.num + "."}}this.flagNum = false;console.info(`text组件内容是:${this.num}`)})}.backgroundColor(Color.Red)}.rowsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr') //行.columnsTemplate('1fr 1fr 1fr 1fr') //列.height('80%').width('90%')}.width('100%').height('100%').backgroundColor(Color.Yellow)}
}function parseAndAdd(str1: string, str2: string): number {// 尝试从第一个字符串中提取数字部分let num1 = parseFloat(removeLastIfOperator(str1)); // 移除所有非数字字符let num2 = parseFloat(str2); // 直接转换第二个字符串为数字// 现在我们可以相加这两个数字return num1 + num2;
}function parseAndJian(str1: string, str2: string): number {// 尝试从第一个字符串中提取数字部分let num1 = parseFloat(removeLastIfOperator(str1)); // 移除所有非数字字符let num2 = parseFloat(str2); // 直接转换第二个字符串为数字// 现在我们可以相加这两个数字return num1 - num2;
}function parseAndCheng(str1: string, str2: string): number {// 尝试从第一个字符串中提取数字部分let num1 = parseFloat(removeLastIfOperator(str1)); // 移除所有非数字字符let num2 = parseFloat(str2); // 直接转换第二个字符串为数字// 现在我们可以相加这两个数字return num1 * num2;
}function parseAndChu(str1: string, str2: string): number {// 尝试从第一个字符串中提取数字部分let num1 = parseFloat(removeLastIfOperator(str1)); // 移除所有非数字字符let num2 = parseFloat(str2); // 直接转换第二个字符串为数字// 现在我们可以相加这两个数字return num1 / num2;
}function removeLastIfOperator(str: string): string {const lastChar = str[str.length - 1]; // 获取最后一个字符const isOperator = ['+', '-', '*', '/'].includes(lastChar); // 判断是否是运算符if (isOperator) {return str.slice(0, -1); // 如果是运算符,则去除最后一个字符} else {return str; // 如果不是运算符,则返回原字符串}
}


http://www.ppmy.cn/ops/151203.html

相关文章

【数据结构】—— 顺序表的实现与优化:空间管理与增容策略

文章目录 顺序表的基本概念与结构顺序表的分类静态顺序表动态顺序表 顺序表问题与思考插入与删除的时间复杂度增容的开销如何解决空间浪费问题? 顺序表作为一种常见的线性数据结构,广泛应用于各种编程任务中。它通过连续的物理内存存储数据元素&#xff…

K8S中Pod调度之污点和容忍

污点和容忍 在 Kubernetes 中,污点(Taints)和容忍(Tolerations)是调度系统中的两个重要概念,它们允许管理员对节点(Node)进行标记,以此来影响 Pod 的调度行为。 前面的调…

如何将原来使用cmakelist编译的qt工程转换为可使用Visual Studio编译的项目

将原来使用CMakeLists.txt编译的Qt工程转换为可使用Visual Studio编译的项目,可以通过以下步骤实现: 一、准备阶段 安装必要的软件: 确保已安装Visual Studio,并选择了C开发相关的组件。安装CMake,并确保其版本与Qt和…

WildFly与tomcat的异同

WildFly 和 Tomcat 都是开源的 Java 应用服务器,用于运行和管理 Java Web 应用程序,但它们有很多的异同。下面是二者在功能、架构、用途等方面的详细对比: 1. 功能定位和支持的技术 WildFly: 全面的 Java EE (Jakarta EE) 支持:…

2025年01月15日Github流行趋势

1. 项目名称:tabby - 项目地址url:https://github.com/TabbyML/tabby - 项目语言:Rust - 历史star数:25764 - 今日star数:1032 - 项目维护者:wsxiaoys, apps/autofix-ci, icycodes, liangfung, boxbeam - 项…

IDEA编译器集成Maven环境以及项目的创建(2)

选择:“File” ---> "Othoer Setting" --> "Settings for New Projects..." --->搜索“Maven” 新建项目 利用maven命令去编译这个项目 利用maven去打包

通用查询类接口开发的另类思路

文章目录 一、需求概述二、开发方式1、传统开发方式2、将接口视为资源文件1.)springmvc工程2.)springboot工程3.)nginx代理 三、接口数据如何更新1、原始数据文件生成接口数据1.)定义启动类2.)启动监听3.)文…

Rnote:Star 8.6k,github上的宝藏项目,手绘与手写画图笔记,用它画图做笔记超丝滑,值得尝试!

嗨,大家好,我是小华同学,关注我们获得“最新、最全、最优质”开源项目和高效工作学习方法 Rnote是一款开源的基于矢量的绘图应用,专为学生、教师以及绘图板用户设计。它支持草图绘制、手写笔记以及对文档和图片进行注释。Rnote提供…