html实现简单的计算器(+-*/)

0 篇文章 0 订阅

使用递归和indexof来实现对加减乘除的计算

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		        
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="css/bootstrap.min.css" />
		<link rel="stylesheet" href="css/glyphicons.css" />
		<script type="text/javascript" src="js/jquery-3.3.1.min.js" ></script>
		<script type="text/javascript" src="js/popper.min.js" ></script>
		<script type="text/javascript" src="js/bootstrap.min.js" ></script> 
		<style type="text/css">
			table{
				border-collapse: collapse;
				width: 300px;
				height: 200px;
			}
			
			table,td,tr{
				border: 1px solid gray;
				text-align: center;
			}
			
			button{
				border: 0px;
				font-size: 20px;
				background-color: white;
				width: 100%;
				height: 100%;
			}
			
			.plusone{
				color: coral;
				background-color: rgb(249,249,249);
			}	
		</style>
		
		<script type="text/javascript">
			$(function(){
				var str="";
				$("button").click(function(){
					var btn = event.target;
					var num = $(btn).text();
					str +=num
					$("#val").val(str)
				})
				
				$("#cacu").click(function(){
					var str1 = str.replace("=","");
					var str2 =  caculate(str1)
					$("#val").val(str+str2);
				})
				
				function caculate(str){
					var index = str.indexOf("+");
					if(index != -1){
						return caculate(str.substring(0,index))+caculate(str.substring(index+1));
					}
					var index = str.indexOf("-");
					if(index != -1){
						return caculate(str.substring(0,index))-caculate(str.substring(index+1));
					}
					
					var index = str.indexOf("*");
					if(index != -1){
						return caculate(str.substring(0,index))*caculate(str.substring(index+1));
					}
					
					var index = str.indexOf("/");
					if(index != -1){
						return caculate(str.substring(0,index))/caculate(str.substring(index+1));
					}
					return parseFloat(str)
					
				}
				
			})
		</script>
		
	</head>
	<body>
		<div class="container" style="margin: 0px auto;border: 1px;" id="container">
			<div align="center" >
						<form action="countercheck.jsp" method="post" >
							<input type="text" name="num" value="" style="width: 300px;height: 60px;" id="val"/>
						<table>
							<tr>
								<td><button type="button"  class="plusone">+</button> </td>
								<td><button type="button"  class="plusone">/</button> </td>
								<td><button type="button"  class="plusone">*</button></td>
								<td><button type="button"  class="plusone">-</button></td>
							</tr>
							<tr>
								<td><button type="button">7</button></td>
								<td><button type="button">8</button></td>
								<td><button type="button">9</button></td>
								<td ><button type="button"   class="plusone">(</button> </td>
							</tr>
							<tr>
								<td><button type="button"  >4</button></td>
								<td><button type="button"  >5</button></td>
								<td><button type="button"  >6</button></td>
								<td><button type="button"   class="plusone">)</button></td>
							</tr>
							<tr>
								<td><button type="button"  >1</button></td>
								<td><button type="button"  >2</button></td>
								<td><button type="button"  >3</button></td>
								<td><button type="button"  class="plusone">.</button></td>
							</tr>
							<tr>
								<td colspan="2"><button type="button"   >0</button></td>
								<td colspan="2"><button type="submit"  class="plusone" id="cacu">=</button></td>
								<!--<td></td>
								<td></td>-->
							</tr>
						</table>
						<!--<input type="submit" value=""/>-->
					</form>
			</div>
		</div>
	</body>
</html>

计算器计算部分代码,使用了这位大大的代码,很简洁但是也有纰漏,如果想看详细计算代码,请参考https://blog.csdn.net/u011692041/article/details/49796343#commentBox

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值