眾所周知鴻蒙 JS 框架是非常輕量級的 MVVM 模式。通過使用和 Vue2 相似的屬性劫持技術實現了響應式系統。
學習鴻蒙很長時間了,想寫一個 demo 進行練練手,就選擇開發這個仿蘋果計算器程序。
先看效果圖:


話不多說,上代碼
hml:
<divclass="container">
<divclass="header">
<textclass="{{outputClassName}}">{{output}}text>
div>
<divclass="keyboard">
<blockfor="{{keyArr}}">
<divif="{{$item=='0'}}"class="zeroKeys"onclick="onclickNubmer({{$item}})">
<text>
{{$item}}
text>
div>
<divelif="{{$item=='AC'||$item=='+/-'||$item=='%'}}"class="operatorKeys-top"onclick="onclickOper({{$item}})">
<text>
{{$item}}
text>
div>
<divelif="{{$item=='÷'||$item=='×'||$item=='-'||$item=='+'||$item=='='}}"class="operatorKeys-right"onclick="onclickOper({{$item}})">
<text>
{{$item}}
text>
div>
<divelseclass="keyskeys-nubmer"onclick="onclickNubmer({{$item}})">
<text>
{{$item}}
text>
div>
block>
div>
div>
css:
.container{
flex-direction:column;
background-color:#010101;
height:100%;
width:100%;
}
.header{
height:36%;
width:100%;
align-items:flex-end;
padding:2px20px2px10px;
}
.keyboard{
height:64%;
width:100%;
padding:2px10px;
flex-wrap:wrap;
}
.outputText,.outputTextSmall{
width:100%;
height:100px;
color:#FFFFFF;
text-align:end;
}
.outputText{
font-size:80px;
}
.outputTextSmall{
font-size:58px;
}
.keys,.zeroKeys,.operatorKeys-top,.operatorKeys-right{
width:74px;
height:74px;
justify-content:center;
align-items:center;
border-radius:74px;
margin:10px5px;
}
.keys-nubmer,.zeroKeys{
background-color:#333333;
}
.zeroKeys{
width:158px;
}
.operatorKeys-top{
background-color:#a4a4a4;
}
.operatorKeys-right{
background-color:#f79f31;
}
.keys:active,.zeroKeys:active{
background-color:#737373;
}
.keystext,.zeroKeystext,.operatorKeys-righttext{
font-size:42px;
color:#FFFFFF;
}
.operatorKeys-toptext{
font-size:36px;
color:#010101;
}
.operatorKeys-top:active{
background-color:#d9d9d9;
}
.operatorKeys-right:active{
background-color:#f5c891;
}
js:
import{math}from"../../common/js/utils.js";
exportdefault{
data:{
output:"0",
outputClassName:"outputText",
cache:[],//記錄輸入內容
keyArr:["AC","+/-","%","÷","7","8","9","×","4","5","6","-","1","2","3","+","0",".","="],
reOper:"",//記錄點擊的運算符
reStr1:"",//記錄第一次輸入內容
reStr2:"",//記錄點擊運算符后的內容
bool:false//防止第二次輸入內容時內容清空
},
onInit(){
this.$watch("output","watchOutPut")
},
onclickOper(item){
if(item=="AC"){
this.clearComput();
}elseif(item=="+"||item=="-"||item=="×"||item=="÷"){
this.reOper=item;
this.reStr1=this.output;
if(this.cache.length>0){
this.startCompute();
}
this.cache.push(this.reStr1);
}elseif(item=="+/-"){
this.output="-"+this.output;
}elseif(item=="%"){
this.output=math.accDiv(this.output,100);
}elseif(item=="="){
this.reStr2=this.output;
this.cache.push(this.reStr2);
this.startCompute();
}
},
onclickNubmer(item){
if(this.cache.length>0&&!this.bool){
this.output="0";
this.bool=true;
}
if(this.output=="0"&&item!="."){
this.output=item;
}elseif(item=="."){
if(this.output.indexOf(".")==-1){
if(this.output=="0"){
this.output="0."
}else{
this.output+=item;
}
}
}else{
if(this.output.length10){
this.output+=item;
}
}
},
watchOutPut(nVal){
if(nVal.length>7&&nVal.length10){
this.outputClassName="outputTextSmall";
}else{
this.outputClassName="outputText";
}
},
startCompute(){
switch(this.reOper){
case"+":
this.output=math.accAdd(this.reStr1,this.reStr2);
this.reStr1=this.output;
break;
case"-":
this.output=math.accSub(this.reStr1,this.reStr2);
this.reStr1=this.output;
break;
case"×":
this.output=math.accMul(this.reStr1,this.reStr2);
this.reStr1=this.output;
break;
case"÷":
this.output=math.accDiv(this.reStr1,this.reStr2);
this.reStr1=this.output;
break;
default:
break;
}
},
clearComput(){
this.output="0";
this.reOper="";
this.reStr1="";
this.reStr2="";
this.cache=[];
this.bool=false;
}
}
utils.js:
classMathCalss{
//js精準除法函數
accDiv(arg1,arg2){
lett1=0,
t2=0,
r1,
r2;
try{
t1=arg1.toString().split('.')[1].length;
}catch(e){}
try{
t2=arg2.toString().split('.')[1].length;
}catch(e){}
r1=Number(arg1.toString().replace('.',''));
r2=Number(arg2.toString().replace('.',''));
return(r1/r2)*Math.pow(10,t2-t1);
}
//js精準加法函數
accAdd(arg1,arg2){
varr1,r2,m,c;
try{
r1=arg1.toString().split(".")[1].length;
}
catch(e){
r1=0;
}
try{
r2=arg2.toString().split(".")[1].length;
}
catch(e){
r2=0;
}
c=Math.abs(r1-r2);
m=Math.pow(10,Math.max(r1,r2));
if(c>0){
varcm=Math.pow(10,c);
if(r1>r2){
arg1=Number(arg1.toString().replace(".",""));
arg2=Number(arg2.toString().replace(".",""))*cm;
}else{
arg1=Number(arg1.toString().replace(".",""))*cm;
arg2=Number(arg2.toString().replace(".",""));
}
}else{
arg1=Number(arg1.toString().replace(".",""));
arg2=Number(arg2.toString().replace(".",""));
}
return(arg1+arg2)/m;
}
//js精準減法函數
accSub(arg1,arg2){
letr1,r2,m,n;
try{
r1=arg1.toString().split('.')[1].length;
}catch(e){
r1=0;
}
try{
r2=arg2.toString().split('.')[1].length;
}catch(e){
r2=0;
}
m=Math.pow(10,Math.max(r1,r2));
//動態控制精度長度
n=r1>=r2?r1:r2;
return(arg1*m-arg2*m)/m;
}
//js精準乘法函數
accMul(arg1,arg2){
varm=0,s1=arg1.toString(),s2=arg2.toString();
try{
m+=s1.split(".")[1].length;
}
catch(e){
}
try{
m+=s2.split(".")[1].length;
}
catch(e){
}
returnNumber(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}
}
exportvarmath=newMathCalss();
為了解決浮點數計算失準問題,我使用一些解決計算失準的函數可供大家參考。
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
函數
+關注
關注
3文章
4417瀏覽量
67509 -
代碼
+關注
關注
30文章
4968瀏覽量
73966 -
CSS
+關注
關注
0文章
110瀏覽量
15501 -
鴻蒙
+關注
關注
60文章
2963瀏覽量
45898
原文標題:開發一個鴻蒙版仿蘋果計算器
文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術社區】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
熱點推薦
Quartz Frequency 實戰:熱力圖+計算器(6 篇)
AT-cut
面向對象:嵌入式/硬件/射頻/物聯網工程師
內容亮點:可視化圖示 + 在線計算器 + 設計邊界與案例
大家好!整理了一套石英定時(Quartz Timing)高原創度文章與工具
發表于 10-09 15:42
鴻蒙5開發寶藏案例分享---一多開發實例(音樂)
各位開發者小伙伴們好呀!今天咱們來點硬核干貨!最近在鴻蒙文檔中心挖到一座“金礦”——官方竟然暗藏了100+實戰案例,從分布式架構到交互動效優化應有盡有!這些案例不僅藏著華為工程師的私房技巧,還直接
Qorvo全新設計計算器:晶振選型、能耗預算計算器和鏈路預算與覆蓋范圍計算器
款功能強大的PC端計算工具 。這些工具—— 晶振采購工具 、 能耗預算計算器 和 鏈路預算與覆蓋范圍計算器 ——讓優化晶振選型、預測電池續航時間以及評估RF鏈路性能變得前所未有地簡單。 接下來,讓我們深入了解每
VirtualLab:衍射角計算器
介質的折射率、結構的周期和入射角。這種相關性在數學上被編碼在光柵方程中。在這個用例中,我們介紹了VirtualLab Fusion的衍射角計算器,這是一個用于計算光柵方程的方便工具。
發表于 06-16 08:48
鴻蒙5開發寶藏案例分享---優化應用包體積大小問題
?** 鴻蒙包體積優化實戰:藏在官方文檔里的寶藏技巧!**
大家好呀~我是你們的鴻蒙開發小伙伴!今天在翻官方文檔時,發現了一個超實用的「包體
發表于 06-13 10:09
鴻蒙5開發寶藏案例分享---Grid性能優化案例
發現鴻蒙寶藏:優化Grid組件性能的實戰技巧!
大家好呀!最近在鴻蒙開發者社區挖到一個超實用的性能優化案例—— 解決Grid組件加載慢、滾動
發表于 06-12 17:47
鴻蒙5開發寶藏案例分享---瀑布流優化實戰分享
鴻蒙瀑布流性能優化實戰:告別卡頓的寶藏指南!
大家好!最近在鴻蒙文檔里挖到一個 性能優化寶藏庫 ,原來官方早就準備好了各種場景的最佳實踐!今天重點分享「瀑布流加載慢丟幀」的解決方案,
發表于 06-12 17:41
鴻蒙5開發寶藏案例分享---一多開發實例(旅行訂票)
把多端適配的黑科技玩出了花!趕緊帶大家來挖寶,手把手解析如何用一套代碼征服手機/折疊屏/平板/PC四大終端!
?** 一多開發核心揭秘**
鴻蒙的\"
發表于 06-03 16:16
DevEco Studio AI輔助開發工具兩大升級功能 鴻蒙應用開發效率再提升
者的喜愛。
應廣大開發者的需求建議,我們最近又對CodeGenie進行了一次升級,不僅針對DeepSeek-R1新增支持鴻蒙知識RAG能力,還上線了代碼解釋功能,持續拓展AI在輔助
發表于 04-18 14:43
VirtualLab Fusion應用:相干時間和相干長度計算器
摘要
在本用例中,我們介紹了一種計算器,它可以根據給定光源的波譜信息快速估計其時間相干特性。然后,可以將該計算器的結果自動復制到通用探測器中,以便在考慮時間相干性時應用近似方法,而無
發表于 04-08 08:48
VirtualLab:衍射角計算器
介質的折射率、結構的周期和入射角。這種相關性在數學上被編碼在光柵方程中。在這個用例中,我們介紹了VirtualLab Fusion的衍射角計算器,這是一個用于計算光柵方程的方便工具。
發表于 04-08 08:46
開發一個鴻蒙版仿蘋果計算器教程.附代碼
評論