软件园学生在线

  • {{ item.name }}
  • 2023试用期

登录与注册

【后端一】卜轩

  • 卜 轩
  • 2022-10-14
  • 1

基于Spring boot的简易四则运算计算器

产品介绍

使用说明

本产品为基于Spring boot框架的REST API,通过html提供的输入框可以进行支持小数且无数值上限的四则运算并返回相应的答案。

特殊情况&解决方案

  • 数据溢出&浮点数精确度问题
  1. 统一使用BigDecimal声明变量,并使用相应的方法进行运算。
    如:a.add(b)进行a + b的运算

  2. 除法引入scale参数以对小数点后保留位数进行限制,防止出现无限不循环小数时抛出异常。同时使用RoundingMode.HALF_UP对计算结果四舍五入。

  • 通过GET请求的参数进行注入攻击

似乎并未出现样例中所述的情况。

  • 数据类型判断

使用instanceof进行数据类型的判断(未做)。

学习历程

BigDecimal

Java之BigDecimal详解

学习了简单的BigDecimal语句,并尝试声明变量时将double替换为BigDecimal。

HTML

学习了简单的html语句,尝试使用<code class="prettyprint" >创建输入框,使用进行页面的跳转。


MainController代码展示

(.src/main/java/com/example/backendwork/Controller/MainController.java)

package com.example.backendwork.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;

@RestController
public class MainController {

    @GetMapping("/add")
    public static BigDecimal add(@RequestParam BigDecimal a, @RequestParam BigDecimal b){
        return a.add(b);
    }

    @GetMapping("/subtract")
    public static BigDecimal subtract(@RequestParam BigDecimal a, @RequestParam BigDecimal b){
        return a.subtract(b);
    }

    @GetMapping("/multiply")
    public static BigDecimal multiply(@RequestParam BigDecimal a, @RequestParam BigDecimal b){
        return a.multiply(b);
    }

    @GetMapping("/divide")
    public static BigDecimal divide(@RequestParam BigDecimal a, @RequestParam BigDecimal b, @RequestParam int scale){
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        return a.divide(b, scale, RoundingMode.HALF_UP);
    }

}

HTML代码展示

(.src/main/resources/static/四则运算.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>四则运算</title>
</head>
<body>
    <h2>加法</h2>
        <form action="http://localhost:8080/add" method="get">
        第一个数字: <input type="BigDecimal" name="a"><br>
        第二个数字: <input type="BigDecimal" name="b"><br>
        <input type="submit" value="提交">
        </form>
    <h2>减法</h2>
        <form action="http://localhost:8080/subtract" method="get">
        被减数: <input type="BigDecimal" name="a"><br>
        减数: <input type="BigDecimal" name="b"><br>
        <input type="submit" value="提交">
        </form>
    <h2>乘法</h2>
        <form action="http://localhost:8080/multiply" method="get">
        第一个数字: <input type="BigDecimal" name="a"><br>
        第二个数字: <input type="BigDecimal" name="b"><br>
        <input type="submit" value="提交">
        </form>
    <h2>除法</h2>
        <form action="http://localhost:8080/divide" method="get">
        被除数: <input type="BigDecimal" name="a"><br>
        除数: <input type="BigDecimal" name="b"><br>
        精确到小数点后的位数: <input type="int" name="scale"><br>
        <input type="submit" value="提交">
        </form>
</body>
</html>
卜 轩
卜 轩
© 2025 软件园学生在线
Theme by Wing