99精品伊人亚洲|最近国产中文炮友|九草在线视频支援|AV网站大全最新|美女黄片免费观看|国产精品资源视频|精彩无码视频一区|91大神在线后入|伊人终合在线播放|久草综合久久中文

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

開(kāi)發(fā)一個(gè)鴻蒙版仿蘋果計(jì)算器教程.附代碼

OpenHarmony技術(shù)社區(qū) ? 來(lái)源:鴻蒙技術(shù)社區(qū) ? 作者: 我曾是少年_ ? 2021-10-11 14:17 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

眾所周知鴻蒙 JS 框架是非常輕量級(jí)的 MVVM 模式。通過(guò)使用和 Vue2 相似的屬性劫持技術(shù)實(shí)現(xiàn)了響應(yīng)式系統(tǒng)。

學(xué)習(xí)鴻蒙很長(zhǎng)時(shí)間了,想寫一個(gè) demo 進(jìn)行練練手,就選擇開(kāi)發(fā)這個(gè)仿蘋果計(jì)算器程序。

先看效果圖:

23715caa-2a47-11ec-82a8-dac502259ad0.gif

2390ea70-2a47-11ec-82a8-dac502259ad0.gif

話不多說(shuō),上代碼

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:[],//記錄輸入內(nèi)容
keyArr:["AC","+/-","%","÷","7","8","9","×","4","5","6","-","1","2","3","+","0",".","="],
reOper:"",//記錄點(diǎn)擊的運(yùn)算符
reStr1:"",//記錄第一次輸入內(nèi)容
reStr2:"",//記錄點(diǎn)擊運(yùn)算符后的內(nèi)容
bool:false//防止第二次輸入內(nèi)容時(shí)內(nèi)容清空
},
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精準(zhǔn)除法函數(shù)
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精準(zhǔn)加法函數(shù)
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精準(zhǔn)減法函數(shù)
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));
//動(dòng)態(tài)控制精度長(zhǎng)度
n=r1>=r2?r1:r2;
return(arg1*m-arg2*m)/m;
}
//js精準(zhǔn)乘法函數(shù)
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();

為了解決浮點(diǎn)數(shù)計(jì)算失準(zhǔn)問(wèn)題,我使用一些解決計(jì)算失準(zhǔn)的函數(shù)可供大家參考。
編輯:jq
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 函數(shù)
    +關(guān)注

    關(guān)注

    3

    文章

    4379

    瀏覽量

    64830
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4900

    瀏覽量

    70682
  • CSS
    CSS
    +關(guān)注

    關(guān)注

    0

    文章

    110

    瀏覽量

    14855
  • 鴻蒙
    +關(guān)注

    關(guān)注

    60

    文章

    2617

    瀏覽量

    44033

原文標(biāo)題:開(kāi)發(fā)一個(gè)鴻蒙版仿蘋果計(jì)算器

文章出處:【微信號(hào):gh_834c4b3d87fe,微信公眾號(hào):OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    Qorvo全新設(shè)計(jì)計(jì)算器:晶振選型、能耗預(yù)算計(jì)算器和鏈路預(yù)算與覆蓋范圍計(jì)算器

    款功能強(qiáng)大的PC端計(jì)算工具 。這些工具—— 晶振采購(gòu)工具 、 能耗預(yù)算計(jì)算器 和 鏈路預(yù)算與覆蓋范圍計(jì)算器 ——讓優(yōu)化晶振選型、預(yù)測(cè)電池續(xù)航時(shí)間以及評(píng)估RF鏈路性能變得前所未有地簡(jiǎn)單。 接下來(lái),讓我們深入了解每
    的頭像 發(fā)表于 06-24 17:51 ?904次閱讀
    Qorvo全新設(shè)計(jì)<b class='flag-5'>計(jì)算器</b>:晶振選型、能耗預(yù)算<b class='flag-5'>計(jì)算器</b>和鏈路預(yù)算與覆蓋范圍<b class='flag-5'>計(jì)算器</b>

    VirtualLab:衍射角計(jì)算器

    介質(zhì)的折射率、結(jié)構(gòu)的周期和入射角。這種相關(guān)性在數(shù)學(xué)上被編碼在光柵方程中。在這個(gè)用例中,我們介紹了VirtualLab Fusion的衍射角計(jì)算器,這是個(gè)用于計(jì)算光柵方程的方便工具。
    發(fā)表于 06-16 08:48

    鴻蒙5開(kāi)發(fā)寶藏案例分享---瀑布流優(yōu)化實(shí)戰(zhàn)分享

    鴻蒙瀑布流性能優(yōu)化實(shí)戰(zhàn):告別卡頓的寶藏指南! 大家好!最近在鴻蒙文檔里挖到個(gè) 性能優(yōu)化寶藏庫(kù) ,原來(lái)官方早就準(zhǔn)備好了各種場(chǎng)景的最佳實(shí)踐!今天重點(diǎn)分享「瀑布流加載慢丟幀」的解決方案,
    發(fā)表于 06-12 17:41

    VirtualLab Fusion應(yīng)用:相干時(shí)間和相干長(zhǎng)度計(jì)算器

    摘要 在本用例中,我們介紹了計(jì)算器,它可以根據(jù)給定光源的波譜信息快速估計(jì)其時(shí)間相干特性。然后,可以將該計(jì)算器的結(jié)果自動(dòng)復(fù)制到通用探測(cè)中,以便在考慮時(shí)間相干性時(shí)應(yīng)用近似方法,而無(wú)
    發(fā)表于 04-08 08:48

    VirtualLab:衍射角計(jì)算器

    介質(zhì)的折射率、結(jié)構(gòu)的周期和入射角。這種相關(guān)性在數(shù)學(xué)上被編碼在光柵方程中。在這個(gè)用例中,我們介紹了VirtualLab Fusion的衍射角計(jì)算器,這是個(gè)用于計(jì)算光柵方程的方便工具。
    發(fā)表于 04-08 08:46

    鴻蒙原生開(kāi)發(fā)手記:04-個(gè)完整元服務(wù)案例

    影院熱映 分享個(gè)完整的元服務(wù)案例,這個(gè)案例高仿了豆瓣的小程序。 簡(jiǎn)介 整個(gè)元服務(wù)分為 4-5 個(gè)頁(yè)面,首頁(yè)為列表頁(yè),展示了當(dāng)前影院熱門的電影,點(diǎn)開(kāi)是
    發(fā)表于 12-27 10:35

    VirtualLab Fusion應(yīng)用:相干時(shí)間和相干長(zhǎng)度計(jì)算器

    摘要 在本用例中,我們介紹了計(jì)算器,它可以根據(jù)給定光源的波譜信息快速估計(jì)其時(shí)間相干特性。然后,可以將該計(jì)算器的結(jié)果自動(dòng)復(fù)制到通用探測(cè)中,以便在考慮時(shí)間相干性時(shí)應(yīng)用近似方法,而無(wú)需
    發(fā)表于 12-27 08:48

    Debye-Wolf積分計(jì)算器的用法

    即可進(jìn)行計(jì)算。 該案例將說(shuō)明如何在VirtualLab中使用Debye-Wolf積分計(jì)算器。 **建模任務(wù) ** 開(kāi)啟Debye-Wolf積分計(jì)算器 ?我們直接單擊計(jì)算器,然后選擇D
    發(fā)表于 12-26 08:59

    LP光纖模式計(jì)算器

    :漸變折射率 (GRIN) 光纖 光纖模式計(jì)算器允許定義線性偏振貝塞爾模式和線性偏振拉蓋爾模式。 對(duì)于 GRIN 光纖,定義了梯度常數(shù)。 然后通過(guò)下式計(jì)算折射率 與前種情況樣,
    發(fā)表于 12-18 13:36

    使用DRV421進(jìn)行設(shè)計(jì):系統(tǒng)參數(shù)計(jì)算器

    電子發(fā)燒友網(wǎng)站提供《使用DRV421進(jìn)行設(shè)計(jì):系統(tǒng)參數(shù)計(jì)算器.pdf》資料免費(fèi)下載
    發(fā)表于 10-26 09:52 ?1次下載
    使用DRV421進(jìn)行設(shè)計(jì):系統(tǒng)參數(shù)<b class='flag-5'>計(jì)算器</b>

    基于FPGA的計(jì)算器設(shè)計(jì)

    本文通過(guò)FPGA實(shí)現(xiàn)8位十進(jìn)制數(shù)的加、減、乘、除運(yùn)算,通過(guò)矩陣鍵盤輸入數(shù)據(jù)和運(yùn)算符,矩陣鍵盤的布局圖如下所示。該計(jì)算器可以進(jìn)行連續(xù)運(yùn)算,當(dāng)按下等號(hào)后,可以直接按數(shù)字進(jìn)行下次運(yùn)算,或者按運(yùn)算符,把上次運(yùn)算結(jié)果作為本次運(yùn)算的第一個(gè)操作數(shù)。
    的頭像 發(fā)表于 10-24 14:28 ?1267次閱讀
    基于FPGA的<b class='flag-5'>計(jì)算器</b>設(shè)計(jì)

    鴻蒙Flutter實(shí)戰(zhàn):08-如何調(diào)試代碼

    # 鴻蒙Flutter實(shí)戰(zhàn):如何調(diào)試代碼 ## 1.環(huán)境搭建 參考文章[鴻蒙Flutter實(shí)戰(zhàn):01-搭建開(kāi)發(fā)環(huán)境](https://gitee.com/zacks
    發(fā)表于 10-23 16:29

    CAN位時(shí)序參數(shù)計(jì)算器

    電子發(fā)燒友網(wǎng)站提供《CAN位時(shí)序參數(shù)計(jì)算器.pdf》資料免費(fèi)下載
    發(fā)表于 10-11 09:55 ?1次下載
    CAN位時(shí)序參數(shù)<b class='flag-5'>計(jì)算器</b>

    色環(huán)電阻計(jì)算器的研究與應(yīng)用

    個(gè)理想的色環(huán)電阻計(jì)算器的界面應(yīng)該包含個(gè)顏色選擇,讓用戶能夠通過(guò)點(diǎn)擊或下拉菜單選擇各個(gè)顏色環(huán)
    的頭像 發(fā)表于 09-18 13:45 ?875次閱讀

    【免費(fèi)分享】OpenHarmony鴻蒙物聯(lián)網(wǎng)開(kāi)發(fā)板資料包網(wǎng)打盡,教程/視頻/項(xiàng)目/源碼...

    ?想要深入學(xué)習(xí)鴻蒙設(shè)備開(kāi)發(fā)鴻蒙物聯(lián)網(wǎng)開(kāi)發(fā)嗎?現(xiàn)在機(jī)會(huì)來(lái)了!我們?yōu)槌鯇W(xué)者們準(zhǔn)備了份全面的資料包,包括原理圖、教程、視頻、項(xiàng)目、源碼等,所有
    的頭像 發(fā)表于 09-14 14:09 ?937次閱讀
    【免費(fèi)分享】OpenHarmony<b class='flag-5'>鴻蒙</b>物聯(lián)網(wǎng)<b class='flag-5'>開(kāi)發(fā)</b>板資料包<b class='flag-5'>一</b>網(wǎng)打盡,<b class='flag-5'>附</b>教程/視頻/項(xiàng)目/源碼...