使用JS+HTML书写一个简易计算器

HTML部分:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<table border="5"style="background-color: #55ffff;">
			<tr>
				<th colspan="5"><input type="text"id="pingmu" value="0" style="width: 265px;height: 40px;"/></th>
			</tr>
				<td><input type="button"value="C"style="width:50px;height:50px;"οnclick="empty()"/></td>
				<td><input type="button"value="+/-"style="width:50px;height:50px;"οnclick="sign()"/></td>
				<td><input type="button"value="%"style="width:50px;height:50px;"οnclick="operation(this.value)"/></td>
				<td colspan="2"><input type="button"value="&larr;"style="width: 102px;height: 50px;margin-left:3px;"οnclick="returnOne()"/></td>
			</tr>
			<tr>
				<td><input type="button"value="7" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="8" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="9" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="+" style="width: 50px;height: 50px;"οnclick="operation(this.value)"/></td>
				<td><input type="button"value="-" style="width: 50px;height: 50px;"οnclick="operation(this.value)"/></td>
			</tr>
			<tr>
				<td><input type="button"value="4" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="5" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="6" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="*" style="width: 50px;height: 50px;"οnclick="operation(this.value)"/></td>
				<td><input type="button"value="/" style="width: 50px;height: 50px;"οnclick="operation(this.value)"/></td>
			</tr>
			<tr>
				<td><input type="button"value="1" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="2" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="3" style="width: 50px;height: 50px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="x²"style="width: 50px;height: 50px;"οnclick="square(this.value)"/></td>
				<td><input type="button"value="√" style="width: 50px;height: 50px;"οnclick="nsqrt()"/></td>
			</tr>
			<tr>
				<td colspan="2"><input type="button"value="0"style="width:102px;height:50px;margin-left:3px;"οnclick="num(this.value)"/></td>
				<td><input type="button"value="."style="width:50px;height:50px;"οnclick="point()"/></td>
				<td colspan="2"><input type="button"value="="style="width:102px;height:50px;margin-left:3px;"οnclick="result()"/></td>
			</tr>
		</table>
		<script type="text/javascript"src="js/计算器.js"></script>
	</body>
</html>

JS部分:

var xianshi="";
var shuzi="";
var huancun="";
//处理数字
function num(a){
	var pingmu=document.getElementById("pingmu");
	xianshi+=a;
	pingmu.value=xianshi;
	shuzi+=a;
}
//基础运算
function operation(b){
	if(shuzi==""&&huancun=="")return;
	huancun+=shuzi+b;
	shuzi="";
	xianshi="";
}
//显示结果
function result(){
	huancun+=shuzi;
	var pingmu=document.getElementById("pingmu");
	huancun=eval(huancun);
	pingmu.value=huancun;
	shuzi="";
	xianshi="";
}
//为数字添加符号
function sign(){
	var pingmu=document.getElementById("pingmu");
	var value =pingmu.value;
	value="(-("+value+"))"
	value=eval(value)
	pingmu.value=value;
	if(shuzi=="")huancun=value;
	else{shuzi=value;}
}
//平方根
function nsqrt(){
	var pingmu=document.getElementById("pingmu");
	var value=pingmu.value;
	value=eval(value);
	value=Math.sqrt(value);
	pingmu.value=value;
	if(shuzi=="")huancun=value;
	else{shuzi=value;}
}
//平方
function square(c){
	var pingmu=document.getElementById("pingmu");
	var value=pingmu.value;
	value=eval(value);
	pingmu.value=value*value;
	if(shuzi=="")huancun=value;
	else{shuzi=value;}
}
//小数点
function point(){
	var pingmu=document.getElementById("pingmu");
	if(shuzi==""||xianshi==""){shuzi="0";xianshi="0"}
	shuzi+=".";
	xianshi+=".";
	pingmu.value=xianshi;
}
//清空
function empty(){
	shuzi="";
	huancun="";
	xianshi="";
	document.getElementById("pingmu").value="";
}
//退一位
function returnOne(){
	shuzi=shuzi.substring(0,shuzi.length-1)
	xianshi=xianshi.substring(0,xianshi.length-1)
	pingmu.value=xianshi;
}

效果如下:
效果图](http://127.0.0.1:8848/lession02/%E8%AE%A1%E7%AE%97%E5%99%A8.html)
效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个简易的jsp+servlet计算器的实现步骤: 1. 创建一个JSP页面,包含一个表单,其中表单中有两个输入框,一个下拉框和一个提交按钮。下拉框中包含加、减、乘、除四个选项。 2. 在表单中提交按钮的onclick事件中,调用JavaScript函数,将表单中的数据通过Ajax方式发送到后台Servlet中。 3. 创建一个Servlet,用于接收前端发送过来的数据并进行计算,计算完成后将结果返回给前端。 4. 在Servlet中,通过request.getParameter()方法获取前端发送过来的数据,然后根据选择的运算符进行计算,并将结果存储在request作用域中。 5. 在Servlet中,通过request.getRequestDispatcher()方法跳转到结果展示的JSP页面。 6. 在JSP页面中,通过EL表达式获取Servlet中计算得到的结果,并将结果展示给用户。 7. 完成以上步骤后,即可实现一个简单的jsp+servlet计算器。 这里是一个简单的示例代码: index.jsp: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>简易计算器</title> <script type="text/javascript"> function calculate() { var num1 = document.getElementById("num1").value; var num2 = document.getElementById("num2").value; var operator = document.getElementById("operator").value; if (num1 == "" || num2 == "") { alert("请输入数字!"); return false; } var url = "calculate?num1=" + num1 + "&num2=" + num2 + "&operator=" + operator; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { document.getElementById("result").innerHTML = xhr.responseText; } } xhr.open("GET", url, true); xhr.send(); } </script> </head> <body> <h1>简易计算器</h1> <form> <input type="text" id="num1" name="num1"> <select id="operator" name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="text" id="num2" name="num2"> <input type="button" value="计算" onclick="calculate()"> </form> <div id="result"></div> </body> </html> ``` CalculateServlet.java: ```java import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CalculateServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int num1 = Integer.parseInt(request.getParameter("num1")); int num2 = Integer.parseInt(request.getParameter("num2")); String operator = request.getParameter("operator"); int result = 0; switch (operator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; } request.setAttribute("result", result); request.getRequestDispatcher("result.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ``` result.jsp: ```html <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>计算结果</title> </head> <body> <h1>计算结果</h1> <p><%= request.getAttribute("result") %></p> </body> </html> ``` 希望这个示例代码能够帮到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值