鴻蒙JS實(shí)戰(zhàn)之計(jì)算器功能開(kāi)發(fā)實(shí)例
場(chǎng)景:
通過(guò)動(dòng)態(tài)設(shè)置按鈕組件button實(shí)現(xiàn)計(jì)算器的鍵盤,通過(guò)文本text顯示計(jì)算的表達(dá)書(shū),可以計(jì)算+,-,*,/,可以一個(gè)一個(gè)移除,可以重置 等。
下面我們開(kāi)始今天的文章,還是老規(guī)矩,通過(guò)如下幾點(diǎn)來(lái)說(shuō):
1,實(shí)現(xiàn)思路
2,代碼解析
3,實(shí)現(xiàn)效果
3,總結(jié)
一,實(shí)現(xiàn)思路
計(jì)算器的鍵盤,本來(lái)是想使用grid的 但是有一些默認(rèn)屬性不好控制,等后續(xù)組件完善了在做優(yōu)化,目前grid適合一些均衡布局,通過(guò)監(jiān)聽(tīng)計(jì)算符號(hào)添加判斷邏輯,計(jì)算結(jié)果也是通過(guò)添加的計(jì)算類型進(jìn)行計(jì)算,目前支持一級(jí)計(jì)算,后面做連續(xù)計(jì)算。
二,代碼解析
子組件:
1,hml文件
實(shí)用了四個(gè)for循環(huán)實(shí)現(xiàn)了鍵盤效果,后面想了一下其實(shí)一個(gè)就能搞定,動(dòng)態(tài)換行就行,時(shí)間有限后續(xù)優(yōu)化(總感覺(jué)計(jì)算器挺簡(jiǎn)單,其實(shí)做起來(lái)還需要點(diǎn)時(shí)間)
<div class="container">
<text class="input-content">{{inputcontent}}</text>
<div class="menu-content">
<div class="caluater" for="{{ caluater }}" >
<button class="content" onclick="calculatorClick({{ $item.id }})">{{ $item.id }}</button>
</div>
</div>
<div class="menu-content">
<div class="caluater" for="{{ caluater1 }}">
<button class="content2" on:click="calculatorClick({{ $item.id }})">{{ $item.id }}</button>
</div>
</div>
<div class="menu-content">
<div class="caluater" for="{{ caluater2 }}">
<button class="content2" on:click="calculatorClick({{ $item.id }})">{{ $item.id }}</button>
</div>
</div>
<div class="list2-content">
<div class="list3-content">
<div class="list2-content">
<div class="caluater" for="{{ caluater3 }}">
<button class="content2" on:click="calculatorClick({{ $item.id }})">{{ $item.id }}</button>
</div>
</div>
<div class="list2-content">
<div class="caluater" for="{{ caluater4 }}">
<button class="content2" on:click="calculatorClick({{ $item.id }})">{{ $item.id }}</button>
</div>
</div>
</div>
<button class="equals" onclick="calculatorResult">=</button>
</div>
</div>
2,css文件
樣式比較簡(jiǎn)單,主要控制鍵盤和表達(dá)式文本的顏色和大小
.container {
flex-direction: column;
justify-content: flex-end;
align-items: flex-end;
width: 100%;
}
.input-content{
background-color: #00ffffff;
text-align: right;
font-size: 25px;
padding: 10px;
font-weight: bold;
}
.menu-content{
width: 100%;
background-color: brown;
allow-scale: true;
}
.caluater {
flex-direction: row;
width: 100%;
height: 70px;
background-color: #00ffffff;
margin-left: 5px;
margin-right: 5px;
margin-top: 10px;
}
.content{
font-size: 30px;
font-weight: bold;
radius: 10px;
width: 100%;
height: 100%;
text-color: #007EFF;
background-color: #A8CCFB;
}
.content2{
font-size: 30px;
font-weight: bold;
radius: 10px;
width: 100%;
height: 100%;
text-color: #000000;
background-color: #dddcdc;
}
.list2-content{
flex-direction: row;
justify-content: center;
align-items: center;
background-color: brown;
}
.list3-content{
flex-direction: column;
margin-bottom: 10px;
}
.equals{
width: 30%;
height: 150px;
font-size: 30px;
font-weight: bold;
radius: 10px;
margin-left: 5px;
margin-right: 5px;
}
3,js文件
js中主要實(shí)現(xiàn)邏輯,請(qǐng)看具體計(jì)算的判斷。
import prompt from '@system.prompt';
export default {
data: {
title: "",
inputcontent: "",
number1: "",
number2: "",
type: "",
caluater: [
{
'id': "C",
},
{
'id': "÷",
},
{
'id': "×",
},
{
'id': "←",
}
],
caluater1:[
{
'id': "7",
},
{
'id': "8",
},
{
'id': "9",
},
{
'id': "+",
}
],
caluater2:[
{
'id': "4",
},
{
'id': "5",
},
{
'id': "6",
},
{
'id': "-",
}
],
caluater3:[
{
'id': "1",
},
{
'id': "2",
},
{
'id': "3",
}
],
caluater4:[
{
'id': "%",
},
{
'id': "0",
},
{
'id': ".",
}
]
},
onInit() {
},
calculatorClick(result){
this.inputcontent = this.inputcontent+"";
let str = this.inputcontent
.substring(this.inputcontent.length-1, this.inputcontent.length);
switch(result) {
case "←":
if(this.inputcontent.length > 0) {
let str = this.inputcontent
.substring(0, this.inputcontent.length - 1);
this.inputcontent = str;
}
break;
case "C":
this.inputcontent = "";
break;
case "+":
this.calcula(str,result,"+");
break;
case "-":
this.calcula(str,result,"-");
break;
case "×":
this.calcula(str,result,"×");
break;
case "÷":
this.calcula(str,result,"÷");
break;
case ".":
if(this.inputcontent.length > 0 && str != ".") {
this.inputcontent += result;
}
break;
default:
this.inputcontent += result;
break;
}
},
calcula(str,result,cla){
if(this.inputcontent.length > 0 && str != "+" && str != "-" && str != "×" && str != "÷") {
this.type = cla;
this.inputcontent += result;
} else {
this.calculatorResult();
}
},
calculatorResult(){// 計(jì)算結(jié)果
var temp = this.inputcontent.split(this.type);
console.log("this.inputcontent = "+this.inputcontent)
let str = this.inputcontent
.substring(this.inputcontent.length-1, this.inputcontent.length);
console.log("this.type = "+this.type)
if (this.type == "+") { // 加法計(jì)算
if(temp.length > 1) {
console.log("temp = "+temp)
var num1 = parseFloat(temp[0]);
var num2 = parseFloat(temp[1]);
console.log("num1 = "+num1)
console.log("num2 = "+num2)
console.log("str = "+str)
if(str != "+") {
this.inputcontent = num1 + num2;
this.type = "";
}
}
} else if(this.type == "-") { // 減法計(jì)算
if(temp.length > 1) {
var num1 = parseFloat(temp[0]);
var num2 = parseFloat(temp[1]);
if(str != "-") {
this.inputcontent = num1 - num2;
this.type = "";
}
}
} else if(this.type == "×") { // 乘法計(jì)算
if(temp.length > 1) {
var num1 = parseFloat(temp[0]);
var num2 = parseFloat(temp[1]);
if(str != "×") {
this.inputcontent = num1 * num2;
this.type = "";
}
}
} else if(this.type == "÷") { // 除法計(jì)算
if(temp.length > 1) {
var num1 = parseFloat(temp[0]);
var num2 = parseFloat(temp[1]);
if(str != "÷") {
this.inputcontent = num1 / num2;
this.type = "";
}
}
}
}
}
為了目前的單一計(jì)算,現(xiàn)在做了不少的判斷,后續(xù)做連續(xù)計(jì)算的時(shí)候會(huì)有改動(dòng),但是目前正常計(jì)算沒(méi)有問(wèn)題,期待后續(xù)更新。
三,實(shí)現(xiàn)效果

四,總結(jié)
開(kāi)發(fā)計(jì)算器最主要的是連續(xù)計(jì)算,連續(xù)計(jì)算需要添加計(jì)算優(yōu)先級(jí)邏輯,后續(xù)考慮通過(guò)遍歷來(lái)判斷里面的計(jì)算。
計(jì)算器界面開(kāi)發(fā)通過(guò)常用組件就能實(shí)現(xiàn),實(shí)現(xiàn)方式可以自己定。
在開(kāi)發(fā)中驗(yàn)證了NaN,這個(gè)空的判斷很多方式無(wú)效的,他是針對(duì)Number做的判斷。
到此這篇關(guān)于鴻蒙Js實(shí)戰(zhàn)之計(jì)算器功能開(kāi)發(fā)的文章就介紹到這了,更多相關(guān)鴻蒙JS計(jì)算器功能開(kāi)發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript常用內(nèi)置對(duì)象用法分析
這篇文章主要介紹了JavaScript常用內(nèi)置對(duì)象用法,簡(jiǎn)單總結(jié)分析了javascript String對(duì)象、Date對(duì)象、Math類、數(shù)組對(duì)象等常見(jiàn)對(duì)象的相關(guān)功能、方法與使用注意事項(xiàng),需要的朋友可以參考下2019-07-07
layui中l(wèi)ayer前端組件實(shí)現(xiàn)圖片顯示功能的方法分析
這篇文章主要介紹了layui中l(wèi)ayer前端組件實(shí)現(xiàn)圖片顯示功能的方法,結(jié)合實(shí)例形式分析了layui中l(wèi)ayer組件調(diào)用圖片顯示功能的操作方法與相關(guān)注意事項(xiàng),并提供了layer與layui源碼下載,需要的朋友可以參考下2017-10-10
js開(kāi)發(fā)插件實(shí)現(xiàn)tab選項(xiàng)卡效果
這篇文章主要為大家詳細(xì)介紹了js開(kāi)發(fā)插件實(shí)現(xiàn)tab選項(xiàng)卡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
引入autocomplete組件時(shí)JS報(bào)未結(jié)束字符串常量錯(cuò)誤
在引入jQuery的autocomplete組件時(shí),遇到j(luò)s報(bào)未結(jié)束字符串常量錯(cuò)誤,原因及解決方法如下,大家可以參考下2014-03-03
JavaScript定時(shí)器實(shí)現(xiàn)無(wú)縫滾動(dòng)圖片
這篇文章主要為大家詳細(xì)介紹了JavaScript定時(shí)器實(shí)現(xiàn)無(wú)縫滾動(dòng)圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
微信小程序基于slider組件動(dòng)態(tài)修改標(biāo)簽透明度的方法示例
這篇文章主要介紹了微信小程序基于slider組件動(dòng)態(tài)修改標(biāo)簽透明度的方法,可通過(guò)slider組件拖動(dòng)實(shí)現(xiàn)圖片透明度的改變功能,涉及微信小程序事件綁定、base64格式圖片載入及slider組件使用技巧,需要的朋友可以參考下2017-12-12
JS小知識(shí)之如何將CSV轉(zhuǎn)換為JSON字符串
CSV文件一般是以逗號(hào)為分隔值的文件(Comma-Separated?Values,CSV,有時(shí)也稱為字符分隔值,因?yàn)榉指糇址部梢圆皇嵌禾?hào)),其文件以純文本形式存儲(chǔ)表格數(shù)據(jù)(數(shù)字和文本),下面這篇文章主要給大家介紹了JS小知識(shí)之如何將CSV轉(zhuǎn)換為JSON字符串的相關(guān)資料,需要的朋友可以參考下2023-06-06

