vue手写一个计算器

2 篇文章 0 订阅

计算器大家都不陌生 有计算器机器 有手机计算器 网页计算器!

在这里插入图片描述

那么好 今天我来给大家手写一个计算器

啥都不说上操作 请听题:vue手写计算器

一个个小方块拼成一个计算器  绿色比较好  可以缓解视力哦

index页面布局

<div class="calculator">
		<div class="screen">{{ display }}</div>
		<div class="buttons">
			<div class="button" @click="clear">C</div>
			<div class="button" @click="negate">+/-</div>
			<div class="button" @click="operation('%')">%</div>
			<div class="button operation" @click="operation('/')">÷</div>
			<div class="button" @click="append('7')">7</div>
			<div class="button" @click="append('8')">8</div>
			<div class="button" @click="append('9')">9</div>
			<div class="button operation" @click="operation('*')">x</div>
			<div class="button" @click="append('4')">4</div>
			<div class="button" @click="append('5')">5</div>
			<div class="button" @click="append('6')">6</div>
			<div class="button operation" @click="operation('-')">-</div>
			<div class="button" @click="append('1')">1</div>
			<div class="button" @click="append('2')">2</div>
			<div class="button" @click="append('3')">3</div>
			<div class="button operation" @click="operation('+')">+</div>
			<div class="button" @click="append('0')">0</div>
			<div class="button" @click="append('.')">.</div>
			<div class="button equals" @click="equals">=</div>
		</div>
	</div>

css样式

<style>
.calculator {
	margin-left: 900px;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	width: 380px;
	height: 700px;
	padding: 50px;
	/* background-color: #fff; */
	border-radius: 10px;
	/* box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2); */
	border: 1px solid red;
	font-family: Arial, sans-serif;
}
.screen {
	display: flex;
	align-items: center;
	justify-content: flex-end;
	width: 100%;
	height: 100px;
	font-size: 40px;
	font-weight: bold;
	padding: 0 20px;
	background-color: #f5f5f5;
	border-top-left-radius: 10px;
	border-top-right-radius: 10px;
	box-shadow: inset 0 -2px rgba(0, 0, 0, 0.1);
}
.buttons {
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	grid-gap: 10px;
	width: 100%;
	height: 320px;
	padding: 10px;
}
.button {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 100%;
	height: 80px;
	font-size: 30px;
	font-weight: bold;
	color: #fff;
	background-color: #4caf50;
	border-radius: 5px;
	box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
	cursor: pointer;
	user-select: none;
	transition: background-color 0.2s, box-shadow 0.2s;
}
.button:hover {
	background-color: #4a8f4e;
	box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}
.button:active {
	background-color: #3f7c42;
	box-shadow: none;
}
.operation {
	background-color: #2196f3;
}
.equals {
	background-color: #ff9800;
}
.equals:hover {
	background-color: #ef8800;
}
</style>

JS逻辑

  <script>
export default {
	data() {
		return {
			display: '0',
			operator: null,
			operand1: null,
			operand2: null,
			result: null,
			decimal: false,
		};
	},
	methods: {
		append(char) {
			if (this.display === '0') {
				this.display = char;
			} else {
				this.display += char;
			}
		},
		clear() {
			this.display = '0';
			this.operator = null;
			this.operand1 = null;
			this.operand2 = null;
			this.result = null;
			this.decimal = false;
		},
		negate() {
			if (this.display !== '0' && this.display !== '-') {
				this.display = (parseFloat(this.display) * -1).toString();
			}
		},
		operation(operator) {
			if (this.operand1 === null) {
				this.operand1 = parseFloat(this.display);
				this.operator = operator;
				this.display = '0';
				this.decimal = false;
			} else {
				this.operand2 = parseFloat(this.display);
				this.calculate();
				this.operator = operator;
				this.operand1 = this.result;
				this.operand2 = null;
				this.display = '0';
				this.decimal = false;
			}
		},
		calculate() {
			switch (this.operator) {
				case '+':
					this.result = this.operand1 + this.operand2;
					break;
				case '-':
					this.result = this.operand1 - this.operand2;
					break;
				case '*':
					this.result = this.operand1 * this.operand2;
					break;
				case '/':
					this.result = this.operand1 / this.operand2;
					break;
				case '%':
					this.result = this.operand1 % this.operand2;
					break;
			}
		},
		equals() {
			if (this.operand1 !== null && this.operand2 === null) {
				this.operand2 = parseFloat(this.display);
				this.calculate();
				this.display = this.result.toString();
				this.operand1 = null;
				this.operand2 = null;
				this.operator = null;
				this.decimal = false;
			}
		},
	},
	watch: {
		display(val) {
			if (val.indexOf('.') !== -1) {
				this.decimal = true;
			} else {
				this.decimal = false;
			}
		},
	},
};
</script>

考虑到很多小同学是新手 我直接给你展示源码 复制粘贴就能使用 仅需一步操作就好!一次就好~

<template>
	<div class="calculator">
		<div class="screen">{{ display }}</div>
		<div class="buttons">
			<div class="button" @click="clear">C</div>
			<div class="button" @click="negate">+/-</div>
			<div class="button" @click="operation('%')">%</div>
			<div class="button operation" @click="operation('/')">÷</div>
			<div class="button" @click="append('7')">7</div>
			<div class="button" @click="append('8')">8</div>
			<div class="button" @click="append('9')">9</div>
			<div class="button operation" @click="operation('*')">x</div>
			<div class="button" @click="append('4')">4</div>
			<div class="button" @click="append('5')">5</div>
			<div class="button" @click="append('6')">6</div>
			<div class="button operation" @click="operation('-')">-</div>
			<div class="button" @click="append('1')">1</div>
			<div class="button" @click="append('2')">2</div>
			<div class="button" @click="append('3')">3</div>
			<div class="button operation" @click="operation('+')">+</div>
			<div class="button" @click="append('0')">0</div>
			<div class="button" @click="append('.')">.</div>
			<div class="button equals" @click="equals">=</div>
		</div>
	</div>
</template>
  <script>
export default {
	data() {
		return {
			display: '0',
			operator: null,
			operand1: null,
			operand2: null,
			result: null,
			decimal: false,
		};
	},
	methods: {
		append(char) {
			if (this.display === '0') {
				this.display = char;
			} else {
				this.display += char;
			}
		},
		clear() {
			this.display = '0';
			this.operator = null;
			this.operand1 = null;
			this.operand2 = null;
			this.result = null;
			this.decimal = false;
		},
		negate() {
			if (this.display !== '0' && this.display !== '-') {
				this.display = (parseFloat(this.display) * -1).toString();
			}
		},
		operation(operator) {
			if (this.operand1 === null) {
				this.operand1 = parseFloat(this.display);
				this.operator = operator;
				this.display = '0';
				this.decimal = false;
			} else {
				this.operand2 = parseFloat(this.display);
				this.calculate();
				this.operator = operator;
				this.operand1 = this.result;
				this.operand2 = null;
				this.display = '0';
				this.decimal = false;
			}
		},
		calculate() {
			switch (this.operator) {
				case '+':
					this.result = this.operand1 + this.operand2;
					break;
				case '-':
					this.result = this.operand1 - this.operand2;
					break;
				case '*':
					this.result = this.operand1 * this.operand2;
					break;
				case '/':
					this.result = this.operand1 / this.operand2;
					break;
				case '%':
					this.result = this.operand1 % this.operand2;
					break;
			}
		},
		equals() {
			if (this.operand1 !== null && this.operand2 === null) {
				this.operand2 = parseFloat(this.display);
				this.calculate();
				this.display = this.result.toString();
				this.operand1 = null;
				this.operand2 = null;
				this.operator = null;
				this.decimal = false;
			}
		},
	},
	watch: {
		display(val) {
			if (val.indexOf('.') !== -1) {
				this.decimal = true;
			} else {
				this.decimal = false;
			}
		},
	},
};
</script>
  <style>
.calculator {
	margin-left: 900px;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	width: 380px;
	height: 700px;
	padding: 50px;
	/* background-color: #fff; */
	border-radius: 10px;
	/* box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2); */
	border: 1px solid red;
	font-family: Arial, sans-serif;
}
.screen {
	display: flex;
	align-items: center;
	justify-content: flex-end;
	width: 100%;
	height: 100px;
	font-size: 40px;
	font-weight: bold;
	padding: 0 20px;
	background-color: #f5f5f5;
	border-top-left-radius: 10px;
	border-top-right-radius: 10px;
	box-shadow: inset 0 -2px rgba(0, 0, 0, 0.1);
}
.buttons {
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	grid-gap: 10px;
	width: 100%;
	height: 320px;
	padding: 10px;
}
.button {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 100%;
	height: 80px;
	font-size: 30px;
	font-weight: bold;
	color: #fff;
	background-color: #4caf50;
	border-radius: 5px;
	box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
	cursor: pointer;
	user-select: none;
	transition: background-color 0.2s, box-shadow 0.2s;
}
.button:hover {
	background-color: #4a8f4e;
	box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}
.button:active {
	background-color: #3f7c42;
	box-shadow: none;
}
.operation {
	background-color: #2196f3;
}
.equals {
	background-color: #ff9800;
}
.equals:hover {
	background-color: #ef8800;
}
</style>

大功告成
在这里插入图片描述
有需要的小伙伴直接可以拿去用了 不谢不谢下次再见!

在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值