在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

開發一個鴻蒙版仿蘋果計算器教程.附代碼

OpenHarmony技術社區 ? 來源:鴻蒙技術社區 ? 作者: 我曾是少年_ ? 2021-10-11 14:17 ? 次閱讀

眾所周知鴻蒙 JS 框架是非常輕量級的 MVVM 模式。通過使用和 Vue2 相似的屬性劫持技術實現了響應式系統。

學習鴻蒙很長時間了,想寫一個 demo 進行練練手,就選擇開發這個仿蘋果計算器程序。

先看效果圖:

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

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

話不多說,上代碼

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();

為了解決浮點數計算失準問題,我使用一些解決計算失準的函數可供大家參考。
編輯:jq
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 函數
    +關注

    關注

    3

    文章

    4372

    瀏覽量

    64292
  • 代碼
    +關注

    關注

    30

    文章

    4891

    瀏覽量

    70306
  • CSS
    CSS
    +關注

    關注

    0

    文章

    110

    瀏覽量

    14753
  • 鴻蒙
    +關注

    關注

    59

    文章

    2534

    瀏覽量

    43805

原文標題:開發一個鴻蒙版仿蘋果計算器

文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術社區】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏

    評論

    相關推薦
    熱點推薦

    鴻蒙5開發寶藏案例分享---瀑布流優化實戰分享

    以下是根據鴻蒙官方瀑布流優化案例整理的非官方技術分享,結合開發實戰經驗重新解讀,加入更多場景分析和代碼示例: ?** 鴻蒙瀑布流性能優化實戰:告別卡頓的寶藏指南!** 大家好!最近在
    發表于 06-12 17:41

    VirtualLab Fusion應用:相干時間和相干長度計算器

    摘要 在本用例中,我們介紹了計算器,它可以根據給定光源的波譜信息快速估計其時間相干特性。然后,可以將該計算器的結果自動復制到通用探測中,以便在考慮時間相干性時應用近似方法,而無
    發表于 04-08 08:48

    VirtualLab:衍射角計算器

    介質的折射率、結構的周期和入射角。這種相關性在數學上被編碼在光柵方程中。在這個用例中,我們介紹了VirtualLab Fusion的衍射角計算器,這是用于計算光柵方程的方便工具。
    發表于 04-08 08:46

    鴻蒙海報編輯APP,分享端云一體化開發的經驗!

    前言 在我工作的日常中,經常會用些畫圖編輯,簡單設計些頁面原型。而在去年低代碼很火的時候,我在公司就開發
    的頭像 發表于 03-16 16:09 ?317次閱讀
    <b class='flag-5'>鴻蒙</b>海報編輯<b class='flag-5'>器</b>APP,分享端云<b class='flag-5'>一體化開發</b>的經驗!

    鴻蒙原生開發手記:04-完整元服務案例

    影院熱映 分享完整的元服務案例,這個案例高仿了豆瓣的小程序。 簡介 整個元服務分為 4-5 頁面,首頁為列表頁,展示了當前影院熱門的電影,點開是
    發表于 12-27 10:35

    VirtualLab Fusion應用:相干時間和相干長度計算器

    摘要 在本用例中,我們介紹了計算器,它可以根據給定光源的波譜信息快速估計其時間相干特性。然后,可以將該計算器的結果自動復制到通用探測中,以便在考慮時間相干性時應用近似方法,而無需
    發表于 12-27 08:48

    Debye-Wolf積分計算器的用法

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

    LP光纖模式計算器

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

    使用DRV421進行設計:系統參數計算器

    電子發燒友網站提供《使用DRV421進行設計:系統參數計算器.pdf》資料免費下載
    發表于 10-26 09:52 ?1次下載
    使用DRV421進行設計:系統參數<b class='flag-5'>計算器</b>

    基于FPGA的計算器設計

    本文通過FPGA實現8位十進制數的加、減、乘、除運算,通過矩陣鍵盤輸入數據和運算符,矩陣鍵盤的布局圖如下所示。該計算器可以進行連續運算,當按下等號后,可以直接按數字進行下次運算,或者按運算符,把上次運算結果作為本次運算的第一個操作數。
    的頭像 發表于 10-24 14:28 ?1157次閱讀
    基于FPGA的<b class='flag-5'>計算器</b>設計

    鴻蒙Flutter實戰:08-如何調試代碼

    # 鴻蒙Flutter實戰:如何調試代碼 ## 1.環境搭建 參考文章[鴻蒙Flutter實戰:01-搭建開發環境](https://gitee.com/zacks
    發表于 10-23 16:29

    鴻蒙Flutter實戰:07混合開發

    。 其優點是主項目開發者可以不關注Flutter實現,不需要安裝配置Flutter開發環境,缺點是無法及時修改Flutter代碼,也不存在熱重載。 ## 2.基于源碼 通過源碼依賴的當時,在原生
    發表于 10-23 16:00

    CAN位時序參數計算器

    電子發燒友網站提供《CAN位時序參數計算器.pdf》資料免費下載
    發表于 10-11 09:55 ?1次下載
    CAN位時序參數<b class='flag-5'>計算器</b>

    色環電阻計算器的研究與應用

    理想的色環電阻計算器的界面應該包含顏色選擇,讓用戶能夠通過點擊或下拉菜單選擇各個顏色環
    的頭像 發表于 09-18 13:45 ?790次閱讀

    【免費分享】OpenHarmony鴻蒙物聯網開發板資料包網打盡,教程/視頻/項目/源碼...

    ?想要深入學習鴻蒙設備開發鴻蒙物聯網開發嗎?現在機會來了!我們為初學者們準備了份全面的資料包,包括原理圖、教程、視頻、項目、源碼等,所有
    的頭像 發表于 09-14 14:09 ?823次閱讀
    【免費分享】OpenHarmony<b class='flag-5'>鴻蒙</b>物聯網<b class='flag-5'>開發</b>板資料包<b class='flag-5'>一</b>網打盡,<b class='flag-5'>附</b>教程/視頻/項目/源碼...
    主站蜘蛛池模板: 精品一区二区三区自拍图片区 | 日本免费不卡视频 | 色婷婷婷丁香亚洲综合不卡 | 亚洲午夜日韩高清一区 | 日韩欧美高清一区 | 国产高清精品自在久久 | 伊人五月婷婷 | 天堂网在线资源www种子 | 亚洲一卡二卡三卡 | 伊人久久成人 | 男人j进入女人j视频大全 | 久久99久久精品国产99热 | 国产片无遮挡在线看床戏 | 国产成人av在线 | 天天狠天天天天透在线 | 久久亚洲免费视频 | 高清影院在线欧美人色 | 久久综合社区 | 日韩在线天堂免费观看 | 人人爱人人艹 | 欧美黄色片一级 | 天天摸天天看 | 69ww免费视频播放器 | 看视频免费 | 天天爱天天插 | 1024手机看片国产 | 精品国产影院 | 年下系列高h文 | 精品国产免费观看一区高清 | 亚洲午夜在线观看 | brazzersvideosexhd欧美高清 | 国产在线97色永久免费视频 | 中文字幕成人乱码在线电影 | 亚洲最新在线 | 99精品久久久久久久婷婷 | 成人三级电影在线观看 | 亚洲成网777777国产精品 | 激情综合激情五月 | 手机看片精品国产福利盒子 | 国产自在自线午夜精品视频在 | 好男人社区www在线观看 |