简单计算器的编写

1.代码部分

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>3-6</title>
		<style type="text/css">
			#div1{
				text-align: center;
				background-color: black;
				border-radius: 15px;
				width: 420px;
				height: 490px;
			}
			input[type =button]{
				border-radius: 5px;
				width: 90px;
				height: 50px;
				font-size: 30px;
			}
			#in1{
				width: 372px;
				height: 80px;
				margin-top: 40px;
				border-radius: 10px;
				font-size: 30px;
				text-align: right;
			}
		</style>
		<script>
			function calculate(m){
				var num = document.getElementById("in1");
				switch(m){
					case "=":
						num.value = eval(num.value);
						break;
					case "AC":
						num.value = "";
						break;
					case "+/-":
						num.value = (-(num.value));
						break;
					case "ESC":
						if(num.value.length > 0){
							num.value = num.value.substr(0,num.value.length-1);
						}
						break;
					case "%":
						num.value = num.value/100;
						break;
					default:
				  		num.value =num.value+ m;
						break;
				}
			}
		</script>
	</head>
	<body>
		<div id="div1">
			<p>
				<input type="text" id="in1"/><br/>
			</p>
			<p>
				<input type="button" value="AC" onclick="calculate(this.value)"/>
				<input type="button" value="ESC" onclick="calculate(this.value)"/>
				<input type="button" value="+/-" onclick="calculate(this.value)"/>
				<input type="button" value="/" onclick="calculate(this.value)"/>
			</p>
			<p>
				<input type="button" value="7" onclick="calculate(this.value)"/>
				<input type="button" value="8" onclick="calculate(this.value)"/>
				<input type="button" value="9" onclick="calculate(this.value)"/>
				<input type="button" value="*" onclick="calculate(this.value)"/>
			</p>
			<p>
				<input type="button" value="4" onclick="calculate(this.value)"/>
				<input type="button" value="5" onclick="calculate(this.value)"/>
				<input type="button" value="6" onclick="calculate(this.value)"/>
				<input type="button" value="-" onclick="calculate(this.value)"/>
			</p>
			<p>
				<input type="button" value="1" onclick="calculate(this.value)"/>
				<input type="button" value="2" onclick="calculate(this.value)"/>
				<input type="button" value="3" onclick="calculate(this.value)"/>
				<input type="button" value="+" onclick="calculate(this.value)"/>
			</p>
			<p>
				<input type="button" value="%" onclick="calculate(this.value)"/>
				<input type="button" value="0" onclick="calculate(this.value)"/>
				<input type="button" value="." onclick="calculate(this.value)"/>
				<input type="button" value="=" onclick="calculate(this.value)"/>
			</p>
		</div>
	</body>
</html>

2.效果图

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值