计算器实现(JS,VUE) 计算功能已完善

前端实现计算器。。应该不难。

第一步:先建个项目:

项目结构如下:

注意:这个vue.js懂的都懂,官网下载或者一些编译器都自带的,这个不放代码了。
不多说,其他直接上代码。

第二步:html代码:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<link rel="stylesheet" href="calculator.css">
	</head>
	<body>
		<div id="app">
			<table id="cal">
				<tr>
					<td id="output" colspan="4">{{ output }}</td>
				</tr>
				<tr v-for="row in keys">
					<td v-if="key.value===0" :colspan="2" v-for="key in row" @click="keyClick(key)" :class="{'bg-yellow': key.type === 'op', 'bg-gray': key.type === 'num'}">
						{{ key.label }}</td>
					<td v-if="key.value!=0" v-for="key in row" @click="keyClick(key)" :class="{'bg-yellow': key.type === 'op', 'bg-gray': key.type === 'num'}">
						{{ key.label }}</td>
				</tr>
			</table>
		</div>

		<script src="vue.js"></script>
		<script src="calculator.js"></script>
	</body>
</html>

第三步:CSS代码:

.bg-yellow {
	background-color: #FF9C34 !important;
}

.bg-gray {
	background-color: #62645D !important;
}


#cal {
	margin: unset;
}

#cal #output {
	background-color: #222 !important;
	text-align: right;
	font-size: 3em;
	font-weight: bold;
	padding: 0px .2em;
	height: 2em;
}

#cal td {
	font-size: 1.5rem;
	font-family: Arial;
	width: 3em;
	height: 2.8em;
	background-color: #333;
	color: white;
	text-align: center;
	vertical-align: middle;
	border-radius: 20px;
}

第四步:JS代码:

new Vue({
	el: '#app',
	data: {
		output: 0,
		str: '',
		b: false,
		d: false,
		count: 0,
		expr: '',
		keys: [
			[{
					label: 'AC',
					value: 'reset',
					type: 'reset'
				},
				{
					label: '+/-',
					value: 'sign',
					type: 'negate'
				},
				{
					label: '%',
					value: '%',
					type: 'op'
				},
				{
					label: '÷',
					value: '/',
					type: 'op'
				}
			],
			[{
					label: '7',
					value: 7,
					type: 'num'
				},
				{
					label: '8',
					value: 8,
					type: 'num'
				},
				{
					label: '9',
					value: 9,
					type: 'num'
				},
				{
					label: '×',
					value: '*',
					type: 'op'
				}
			],
			[{
					label: '4',
					value: 4,
					type: 'num'
				},
				{
					label: '5',
					value: 5,
					type: 'num'
				},
				{
					label: '6',
					value: 6,
					type: 'num'
				},
				{
					label: '+',
					value: '+',
					type: 'op'
				}
			],
			[{
					label: '1',
					value: 1,
					type: 'num'
				},
				{
					label: '2',
					value: 2,
					type: 'num'
				},
				{
					label: '3',
					value: 3,
					type: 'num'
				},
				{
					label: '-',
					value: '-',
					type: 'op'
				}
			],
			[{
					label: '0',
					value: 0,
					type: 'num'
				},
				{
					label: '',
					value: "",
					type: 'num'
				},
				{
					label: '.',
					value: '.',
					type: 'num'
				},
				{
					label: '=',
					value: '=',
					type: 'exec'
				}
			],
		]
	},
	methods: {
		keyClick: function(key) {
			switch (key.type) {
				case 'num':
				case 'op':
					this.expr += key.value;
					s = this.changeNumber(key.value)
					this.expr = s
					this.output = this.expr;
					break;
				case 'exec':
					this.d = true
					this.output = eval(this.expr);
					this.str = this.output + ''
					break;
				case 'reset':
					this.expr = '';
					this.str = '';
					this.output = 0;
					break;
				case 'negate':
					this.str = "-1*(" + this.str + ")";
					this.output = eval(this.str);
					this.str = this.output + "";
					break;
			}
		},
		changeNumber: function(num) {
			if (this.d == true && typeof num == "number") {
				this.str = num + '';
				this.d = false;
				return this.str;
			} else {
				this.d = false;
				if (this.str.length == 0) {
					if (num == 0) {
						this.str = "0";
						return this.str;
					} else {
						this.str += num;
						return this.str;
					}
				} else {
					if (num == 0) {
						if (this.str == "0") {
							this.str = "";
							this.str += num;
							return this.str;
						} else {
							a = this.str.charAt(this.str.length - 2) //判断前一位是否是符号
							c = this.isSign(a)
							if (this.str.charAt(this.str.length - 1) == "0" && c) {
								if (a == '.') {
									this.str += num;
									return this.str;
								} else {
									this.str = this.str
									return this.str;
								}
							} else {
								if (this.str.charAt(this.str.length - 1) == ".") {
									this.str += num;
									return this.str;
								} else {
									this.str += num;
									return this.str;
								}
							}
						}
					} else if (num != 0 && typeof num == "number") {
						if (this.str == "0") {
							this.str = "";
							this.str += num;
							return this.str;
						} else {
							if (this.str.charAt(this.str.length - 1) == "0" && !this.havePoint(this.str)) {
								this.str = this.str.substring(0, this.str.length - 1) + num;
								return this.str;
							} else {
								this.str += num;
								return this.str;
							}
						}
					} else {
						this.str = this.str + ''
						s = this.str.charAt(this.str.length - 1);
						this.b = this.isSign(s)
						if (this.b && num != '.') {
							if (this.isS(num)) {
								temp = this.str.charAt(this.str.length - 1)
								if (temp == num) {
									this.str = this.str.substring(0, this.str.length - 1) + num;
									return this.str;
								} else {
									if (this.havePoint(this.str)) {
										this.str += num;
										return this.str;
									} else {
										this.str = this.str.substring(0, this.str.length - 1) + num;
										return this.str;
									}
								}
							} else {
								this.str = this.str.substring(0, this.str.length - 1) + num;
								return this.str;
							}
						} else {
							if (this.str.charAt(this.str.length - 1) == '.' && num == '.') {
								this.str = this.str;
								return this.str;
							} else {
								is = this.havePoint(this.str)
								if (is && num == '.') {
									this.str = this.str + '';
									return this.str;
								} else {
									this.str += num;
									return this.str;
								}
							}
						}
					}
				}
			}

		},
		havePoint: function(string) {
			n = string.length - 1;
			while (n > 0) {
				if (string.charAt(n) == '.') {
					return true;
				} else {
					s = string.charAt(n)
					if (this.isSign(s)) {
						return false
					} else {
						n--;
					}
				}
			}
		},
		isSign: function(s) {
			if (s == '+' || s == '-' || s == '*' || s == '/' || s == '%' || s == '.') {
				return true;
			} else {
				return false;
			}
		},
		isAB: function(strs, num) {
			if (strs.charAt(strs.length - 1) == num) {
				return true
			} else {
				return false
			}
		},
		isS: function(s) {
			if (s == '+' || s == '-' || s == '*' || s == '/' || s == '%') {
				return true;
			} else {
				return false;
			}
		},
	}
})

最终的结果:

搞定苹果计算器。


楠哥-------一心想为IT行业添砖加瓦,却总是面向cv编程的程序员。
  谢谢阅读,无误点赞,有误还望评论区指正。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值