关于长沙大学的前段计算
/**\n * 计算器\n */\nEntry\nComponent\n\nstruct Calculator{\n /*创建数组用来设置计算器按钮上显示的内容*/\n private buttonTexts [\n AC , Del , /- , / ,\n 7 , 8 , 9 , * ,\n 4 , 5 , 6 , - ,\n 1 , 2 , 3 , ,\n % , 0 , . , \n ]\n State displayText: string 0\n State firstOperand: number | null null\n State operator: string | null null\n State waitingForSecondOperand: boolean false\n /*构件界面*/\n build() {\n /*添加列*/\n Column(){\n /*--------------------------添加第一列 上面空白的部分*/\n Column(){\n }/*设置列的样式*/\n .width(100%)\n .height(10%)\n /*-------------------------------添加第二列 输入框*/\n Column(){\n /*添加文本框用来输入计算*/\n TextInput()\n .fontSize(70)\n .height(80%)/*设置文本框的高度*/\n .textAlign(TextAlign.End)/*设置文本框的对齐方式 输入显示在右边*/\n .backgroundColor(Color.White)\n .fontColor(Color.Black)\n .padding({right:20})\n }/*设置列的样式*/\n .width(100%)\n .height(35%)\n .backgroundColor(Color.White)\n\n /*------------------------------------添加第三列 按钮框*/\n Column(){\n /*添加网格组件*/\n Grid(){\n /*循环创建计算器的按钮 同时设置按钮上显示的内容*/\n ForEach(this.buttonTexts, (item: string , index) {\n /*添加网格项*/\n GridItem(){\n /*添加按钮*/\n Button(item)\n .width(100%)/*设置按钮的宽度*/\n .height(100%)/*设置按钮的高度*/\n .fontSize(30)/*设置按钮的字体大小*/\n .borderRadius(50)/*设置按钮的圆角*/\n .backgroundColor(this.setButtonBackgroundColor(item))/*设置按钮的背景颜色*/\n .fontColor(this.setButtonFontColor( item))/*设置按钮的字体颜色*/\n .onClick((){\n //this.handleButonClick(item)\n })\n }\n })\n }/*设置网格的样式*/\n .columnsTemplate(1fr 1fr 1fr 1fr)/*设置4列的模板*/\n .rowsTemplate(1fr 1fr 1fr 1fr 1fr)/*设置5行的模板*/\n .columnsGap(10)\n .rowsGap(10)\n }/*设置列的样式*/\n .width(95%)\n .height(55%)\n .margin(10)/*设置列的边距*/\n }/*设置列的样式*/\n .width(100%)\n .height(100%)\n }\n /*创建函数用来处理按钮点击事件*//*\n handleButonClick(item: string) {\n switch (item) {\n case AC:\n this.displayText 0\n this.firstOperand null\n this.Operand null\n this.waitingForSecondOperand false\n break\n case Del:\n if (this.displayText.length 1 ) {\n this.displayText this.displayText.slice(0, -1)\n } else {\n this.displayText 0;\n }\n break\n case /-:\n const currentValue parseFloat(this.displayText);\n this.displayText String(currentValue * -1)\n break\n case %:\n const currentValue parseFloat(this.displayText);\n this.displayText String(percentValue);\n break\n case .:\n if (!this.displayText.includes(.)) {\n this.displayText .\n }\n break\n case :\n if (this.operator this.firstOperand ! null) {\n const secondOperand parseFloat(this.displayText)\n let result 0\n\n switch (this.operator) {\n case :\n result this.firstOperand secondOperand\n break\n case -:\n result this.firstOperand - secondOperand\n break\n case *:\n result this.firstOperand * secondOperand\n break\n case /:\n result secondOperand ! 0 ? this.firstOperand / secondOperand : 0\n break\n }\n\n this.displayText String(result)\n this.firstOperand result\n this.operator null\n this.waitingForSecondOperand false\n }\n break\n\n default:\n if (this.waitingForSecondOperand) {\n this.displayText item\n this.waitingForSecondOperand false\n } else {\n this.displayText this.displayText 0 ? item : this.displayText item\n }\n break\n }\n }*/\n\n /***\n * 设置按钮背景颜色\n */\n setButtonBackgroundColor(item: string) {\n /*分支判断*/\n switch (item) {\n case /:\n case *:\n case -:\n case :\n return #ffef8282;\n case :\n return #ffdd4637;\n default:\n return #ededed;\n }\n }\n /**\n * 创建函数用来设置按钮上显示的内容的颜色\n */\n setButtonFontColor(item: string){\n switch( item){\n case AC:\n case Del:\n case /-:\n case /:\n case *:\n case -:\n case :\n return #ffef3c3c;\n case :\n return #ffffffff;\n default:\n return #ff000000;\n }\n }\n}\n\n\n\nWWechat\nimport { window } from kit.ArkUI\nimport { MyPage } from ../component/Mypage\nimport { WechatPage } from ../component/WechatPage\n\n/**\n * Description: 微信主页面\n * Author: khc\n * Date: 2026/3/30 9:15\n * Version: 1.0\n */\nEntry\nComponent\nstruct Home{\n /*创建变量用来获取点击选项时选项的索引值*/\n State selectedIndex: number 0\n\n aboutToAppear(): void {\n /*设置全铺整个屏幕*/\n window.getLastWindow(getContext(),(error,win){\n win.setWindowLayoutFullScreen(true)\n })\n }\n /*构造界面*/\n build() {\n /*添加列*/\n Column(){\n /*添加选项卡组件 1: barPosition: BarPosition.End在底部显示 index:指定默认选中的选项 指定选项的索引*/\n Tabs({barPosition: BarPosition.End,index: this.selectedIndex}){\n /*-----------------------------------微信*/\n TabContent(){\n /*调用微信页面*/\n //WechatPage()\n /*添加列*/\n Column(){\n\n }/*设置列的样式*/\n .width(100%)\n .height(100%)\n .backgroundColor(Color.Red)\n }/*设置选项的标题*/\n //.tabBar(微信)\n .tabBar(this.buildTabBar(0,微信,$r(app.media.wechat1),$r(app.media.wechat2)))\n /*------------------------------------通讯录*/\n TabContent(){\n /*添加列*/\n Column(){\n\n }/*设置列的样式*/\n .width(100%)\n .height(100%)\n .backgroundColor(Color.Green)\n\n }/*设置选项的标题*/\n //.tabBar(通讯录)\n .tabBar(this.buildTabBar(1,通讯录,$r(app.media.contacts1),$r(app.media.contacts2)))\n /*------------------------------------------发现*/\n TabContent(){\n /*添加列*/\n Column(){\n\n }/*设置列的样式*/\n .width(100%)\n .height(100%)\n .backgroundColor(Color.Blue)\n\n\n }/*设置选项的标题*/\n //.tabBar(发现)\n .tabBar(this.buildTabBar(2,发现,$r(app.media.find1),$r(app.media.find2)))\n\n /*------------------------------------------我*/\n TabContent(){\n /*调用MyPage主页面*/\n //MyPage()\n /*添加列*/\n Column(){\n\n }/*设置列的样式*/\n .width(100%)\n .height(100%)\n .backgroundColor(Color.White)\n }/*设置选项的标题*/\n //.tabBar(我)\n .tabBar(this.buildTabBar(3,我,$r(app.media.me1),$r(app.media.me2)))\n }/*设置Tabs的样式*/\n .onChange((index: number){/*index就是选中的选项的索引*/\n /*把选中的索引保存到selectedIndex中*/\n this.selectedIndex index\n })\n }/*设置列的样式*/\n .width(100%)/*设置列的宽度*/\n .height(100%)/*设置列的高度*/\n }\n /**\n * 创建选项中的内容\n * index 选项的索引\n * title 标题\n * icon 默认的图标\n * selection 选中时的图标\n * */\n Builder\n buildTabBar(index: number,title: string,icon: Resource,selection: Resource ){\n /*添加列*/\n Column(){\n /*添加图标*/\n Image(this.selectedIndex index ? selection:icon)\n .width(30)\n .height(30)\n /*添加标题*/\n Text(title)\n .fontColor(this.selectedIndex index ? Color.Green:)\n\n }/*设置列的样式*/\n .width(100%)\n .height(65%)\n }\n}