js实现计算器

js实现计算器

代码如下:

<head>
	<meta charset="UTF-8">
	<title></title>
	<style type="text/css">
		.panel {
			border: 4px solid #ddd;
			width: 192px;
			margin: 100px auto;
			/*border-radius: 6px;*/
		}
		
		.panel p,
		.panel input {
			font-family: "微软雅黑";
			font-size: 20px;
			margin: 4px;
			float: left;
			/*border-radius: 4px;*/
		}
		
		.panel p {
			width: 122px;
			height: 26px;
			border: 1px solid #ddd;
			padding: 6px;
			overflow: hidden;
		}
		
		.panel input {
			width: 40px;
			height: 40px;
			border: 1px solid #ddd;
		}
	</style>

</head>

<body>
	<div class="panel">
		<div>
			<p id="screen"></p>
			<input type="button" value="C" onclick="clean()">
			<div style="clear:both"></div>
		</div>
		<div>
			<input type="button" value="7" onclick="inputnumber(this)">
			<input type="button" value="8" onclick="inputnumber(this)">
			<input type="button" value="9" onclick="inputnumber(this)">
			<input type="button" value="/" onclick="operate(this)">

			<input type="button" value="4" onclick="inputnumber(this)">
			<input type="button" value="5" onclick="inputnumber(this)">
			<input type="button" value="6" onclick="inputnumber(this)">
			<input type="button" value="*" onclick="operate(this)">

			<input type="button" value="1" onclick="inputnumber(this)">
			<input type="button" value="2" onclick="inputnumber(this)">
			<input type="button" value="3" onclick="inputnumber(this)">
			<input type="button" value="-" onclick="operate(this)">

			<input type="button" value="0" onclick="inputnumber(this)">
			<input type="button" value="." onclick="inputnumber(this)">
			<input type="button" value="=" onclick="equal(this)">
			<input type="button" value="+" onclick="operate(this)">

			<div style="clear:both"></div>
		</div>
	</div>

	<script type="text/javascript">
		var oScreen = document.getElementById("screen");
		var str = "";
		var firstTemp = 0;
		var secondTemp = 0;
		var sym = ""; //符合

		function inputnumber(num) {
			str = num.value;
			oScreen.innerText += str;
			secondTemp = parseFloat(oScreen.innerText);
		}

		function operate(symbol) {
			sym = symbol.value;		
			if(sym == "+") {				
				firstTemp = parseFloat(oScreen.innerText);
				oScreen.innerText = "";
			} else if(sym == "-") {
				firstTemp = parseFloat(oScreen.innerText);
				oScreen.innerText = "";
			} else if(sym == "*") {
				firstTemp = parseFloat(oScreen.innerText);
				oScreen.innerText = "";
			} else if(sym == "/") {
				firstTemp = parseFloat(oScreen.innerText);
				oScreen.innerText = "";
			}
		}

		function calculation(num1, num2, s) {
			switch(s) {
				case "+":
					endTemp = num1 + num2;

					break;
				case "-":
					endTemp = num1 - num2;
					break;
				case "*":
					endTemp = num1 * num2;
					break;
				case "/":
					endTemp = num1 / num2;
					break;
			}
			return firstTemp = endTemp;
		}

		function equal(eq) {
			if(eq.value == "=") {
				oScreen.innerText = calculation(firstTemp, secondTemp, sym);

			}
		}

		function clean() {
			firstTemp = 0;
			secondTemp = 0;
			sym = "";
			oScreen.innerText = "";
		}
	</script>
</body>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值