随缘四则运算工具
产品说明
本网页计算器能进行一些简单的四则运算,至于随缘这一点是因为可能还会有很多错误没被测试出来,日后使用过程中可能会出错。
如何使用
在IDEA内运行Demo1Application后,在浏览器打开localhost:8080/First.html后使用。
学习历程
-
学习了springboot入门基础和html入门基础,学会了怎么传递请求给后端,怎么写表单传数据。除了文件演示d的url之外,主要是学会用form表单,但是只是照葫芦画瓢的模仿,只会简单操作,讲不出原理。了解了几种利用注解传数据的方法,但是没搞懂。
-
继续学习了java解析运算,
拓展思考
- 防止前端的数据类型有误或许可以限制type=number,这样就不能输入字符串等其他类型数据了,但是仍然不会判断数据类型是否有误
- 数据溢出或许能够通过在前端限制输入的数字长度来偷懒。
代码
- 后端
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/nan")
public class Main {
@RequestMapping("/Plus")
public int zhengshujiafa(int a, int b) {int Right = a + b;
if (((a ^ Right) & (b ^ Right)) < 0)
throw new ArithmeticException("整数数据溢出");return Right;
}
@RequestMapping("/Minus")
public int zhengshujianfa(int a, int b) {int Right = a - b;
if (((a ^ b) & (a ^ Right)) < 0)
throw new ArithmeticException("整数数据溢出");return Right;
}
@RequestMapping("/Times")
public int zhengshuchengfa(int a, int b) {long Right = (long) a * b;
if ((int)Right != Right) {
throw new ArithmeticException("数据溢出");
}
return (int)Right;
}
@RequestMapping("/Dividedby")
public int zhengshuchufa(int a, int b) {
return a / b;
}
@RequestMapping("/DoublePlus")
public double xiaoshijiafa(double a, double b) {
return a + b;
}
@RequestMapping("/DoubleMinus")
public double xiaoshujianfa(double a, double b) {
return a - b;
}
@RequestMapping("/DoubleTimes")
public double xiaoshuchengfa(double a, double b) {
return a * b;
}
@RequestMapping("/DoubleDividedby")
public double xiaoshuchufa(double a, double b) {
return a / b;
}
}
- 前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>随缘四则运算工具</title>
</head>
<body>
<h1>整数加法</h1>
<form action="/nan/Plus" method="POST">
<div>
<label>请输入a
<input type="number" name="a" placeholder="a的值">
</label>
<label>请输入b
<input type="number" name="b" placeholder="b的值">
</label>
<br>
<br>
<input type="submit" name="submit" value="确定">
</div>
</form>
<h1>整数减法</h1>
<form action="/nan/Minus" method="POST">
<div>
<label>请输入a
<input type="number" name="a" placeholder="a的值">
</label>
<label>请输入b
<input type="number" name="b" placeholder="b的值">
</label>
<br>
<br>
<input type="submit" name="submit" value="确定">
</div>
</form>
<h1>整数乘法</h1>
<form action="/nan/Times" method="POST">
<div>
<label>请输入a
<input type="number" name="a" placeholder="a的值">
</label>
<label>请输入b
<input type="number" name="b" placeholder="b的值">
</label>
<br>
<br>
<input type="submit" name="submit" value="确定">
</div>
</form>
<h1>整数除法</h1>
<form action="/nan/Dividedby" method="POST">
<div>
<label>请输入a
<input type="number" name="a" placeholder="a的值">
</label>
<label>请输入b
<input type="number" name="b" placeholder="b的值">
</label>
<br>
<br>
<input type="submit" name="submit" value="确定">
</div>
</form>
<h1>小数加法</h1>
<form action="/nan/DoublePlus" method="POST">
<div>
<label>请输入a
<input type="number" step="0.01" name="a" placeholder="a的值">
</label>
<label>请输入b
<input type="number" name="b" step="0.01" placeholder="b的值">
</label>
<br>
<br>
<input type="submit" name="submit" value="确定">
</div>
</form>
<h1>小数减法</h1>
<form action="/nan/DoubleMinus" method="POST">
<div>
<label>请输入a
<input type="number" step="0.01" name="a" placeholder="a的值">
</label>
<label>请输入b
<input type="number" name="b" step="0.01" placeholder="b的值">
</label>
<br>
<br>
<input type="submit" name="submit" value="确定">
</div>
</form>
<h1>小数乘法</h1>
<form action="/nan/DoubleTimes" method="POST">
<div>
<label>请输入a
<input type="number" step="0.01" name="a" placeholder="a的值">
</label>
<label>请输入b
<input type="number" name="b" step="0.01" placeholder="b的值">
</label>
<br>
<br>
<input type="submit" name="submit" value="确定">
</div>
</form>
<h1>小数除法</h1>
<form action="/nan/DoubleDividedby" method="POST">
<div>
<label>请输入a
<input type="number" step="0.01" name="a" placeholder="a的值">
</label>
<label>请输入b
<input type="number" name="b" step="0.01" placeholder="b的值">
</label>
<br>
<br>
<input type="submit" name="submit" value="确定">
</div>
</form>
</body>
</html>