作者:张前霞
本文正在参加星光计划3.0–夏日挑战赛
前言
本计算器是仿造windows系统实现的,实现了基本的功能:四则运算,清除,退位,小数点,正负号功能。作为FA初学者,拿来练练手,重点是熟悉其中一些语法的使用,以及css属性。
效果展示

API参考
属性 |
类型 |
|
描述 |
border-radius |
<length> |
- |
border-radius属性设置元素的外边框圆角半径。设置border-radius时不能单独设置某一个方向的border-[left|top|right|bottom]-width,border-[left|top|right|bottom]-color ,border-[left|top|right|bottom]-style,如果要设置color、width和style,需要将四个方向一起设置(border-width、border-color、border-style)。说明顺序为左下、右下、左上和右上。 |
border-[top|bottom]-[left|right]-radius |
<length> |
- |
分别设置左上,右上,右下和左下四个角的圆角半径。 |
background |
<linear-gradient> |
- |
仅支持设置渐变样式,与background-color、background-image不兼容。 |
background-color |
<color> |
- |
设置背景颜色。 |
代码展示
1. html代码
2. css代码
3. js代码
export default {
data: {
iptValue:'',
havenum:false,
tool:'',
arr:[
{id:1, value:'7',tool:'num'},
{id:2, value:'8' ,tool:'num'},
{id:3, value:'9' ,tool:'num'},
{id:4, value:'del',tool:'del'},
{id:5, value:'C',tool:'clear'},
{id:6, value:'4',tool:'num'},
{id:7, value:'5' ,tool:'num'},
{id:8, value:'6' ,tool:'num'},
{id:9, value:'*',tool:'com'},
{id:10, value:'/',tool:'com'},
{id:11, value:'1',tool:'num'},
{id:12, value:'2' ,tool:'num'},
{id:13, value:'3' ,tool:'num'},
{id:14, value:'+',tool:'com'},
{id:15, value:'-',tool:'com'},
{id:16, value:'0',tool:'num'},
{id:17, value:'00' ,tool:'num'},
{id:18, value:'.' ,tool:'dot'},
{id:19, value:'%',tool:'com'},
{id:20, value:'=',tool:'equal'},
],
},
numFn(val){
this.iptValue = this.iptValue === '0'? '' :this.iptValue;
this.iptValue += this.tool ? this.tool + val : val;
this.tool = "";
this.havenum = true;
},
comFn(val){
this.commonFunc(val)
},
commonFunc(val) {
let arr = [];
let flag = false;
for(let i = 0; i < this.iptValue.length; i++) {
if (isNaN(parseInt(this.iptValue[i]))) {
if (this.iptValue.length - 1 !== i) {
flag = true;
this.tool = this.iptValue[i];
}
}
}
if (flag || !this.iptValue.length) {
arr = this.iptValue.split(this.tool);
switch(this.tool) {
case "*" : arr.length && arr[1] !== undefined ? this.iptValue = (Number(arr[0]) * Number(arr[1])).toString() : arr[0];
break;
case "+" : arr.length && arr[1] !== undefined ? this.iptValue = (Number(arr[0]) + Number(arr[1])).toString() : arr[0];
break;
case "-" : arr.length && arr[1] !== undefined ? this.iptValue = (Number(arr[0]) - Number(arr[1])).toString() : arr[0];
break;
case "/" : arr.length && arr[1] !== undefined ? this.iptValue = (Number(arr[0]) / Number(arr[1])).toString() : arr[0];
break;
case "%" : arr.length && arr[1] !== undefined ? this.iptValue = (Number(arr[0]) % Number(arr[1])).toString() : arr[0];
break;
}
this.tool = val;
} else {
if (isNaN(parseInt(this.iptValue[this.iptValue.length - 1]))) {
this.iptValue = this.iptValue.slice(0, this.iptValue.length - 1)
this.tool = val
} else {
this.iptValue += val;
this.tool = ""
}
}
},
deleteFn(){
this.iptValue = this.iptValue.slice(0,this.iptValue.length-1);
},
clearFn(){
this.iptValue = '';
this.tool = ""
},
dot(val){
if(this.havenum){
this.iptValue += val;
this.havenum = false;
}
},
equal(){
this.commonFunc();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
注意事项
-
+*-/.五种符号不能开头输入,而且不能出现连续的符号 ,除此之外 ,不能连续输入两次及以上的符号。
-
一个0开头后面不能出现数字,出现NaN(例如0/0)后的逻辑处理或者是计算结果出现别的string字符需要立即自动消失。
-
运算表达式不能直接使用eval方法求值,在计算逻辑中只做两个数的单次运算,那么再FA中是否有可以直接返回复合运算结果的方法,有待继续学习发现。
-
运算表达式字符串转数组,以运算符号拆分,拿到两个数进行单次运算得到结果,这个拆分也可以用两个变量赋值运算。字符串的拆分截取中,replace()方法,原replace()方法中的第一个参数正则表达式在 FA中没生效,所以在这里替换使用slice()方法截取。
-
if else 语句就是判断选择的是什么种类的运算符号,从而进行对应的运 算, 其中的parseInt的作用是解析一个字符串,并返回一个整数,文本框双向绑定赋结果。
总结
在写代码过程中发现,鸿蒙有些属性的写法不支持,类似常用的圆角属性不支持百分比,背景background不支持复合属性写法,以及字符串方法,数组方法中的参数,方法不合用等等,后续开发中还需慢慢总结积累。
入门到精通、技巧到案例,系统化分享HarmonyOS开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
很有趣
小工具get
不错不错
除了夸赞,再无他词,此时此刻我只想用一个番文来表达我的赞意, six!
用过,很实用!链接麻烦发一下。
厉害了