Spring Boot 基础
说明
该计算工具设有一个主页,随后前往输入页面
用户可在该输入页面输入想计算的四则运算式子,格式为“第一个参与运算的数字(仅限整数及小数)+运算符号(加减乘除)+第二个数字”,如果输入不符合格式,会提示“请正确输入”
系统计算完后呈现结果
如果输入数字过大,会将int转换为long类型
(int上限为2的32次幂,为防止该项目运算两数相乘时出现问题,将阈值设为2的16次幂)
学习过程
wiki文档
创建project时要选maven
根据功能分包、分class
可改输入地址,注意对应url
利用idea辅助纠错功能
作业过程
1.分析需求
实现四则运算,用户输入一个式子(或依次输入三个参数,二选一),需提取、识别出其中的运算符号(加减乘除四段代码),算完后返回
controller写整体框架 计算写到service
由于对具体路线 框架缺乏把握,所以开始搜索
2.网络资料
在bing搜索到了github上的这一篇
[Github上的一个计算器](https://github.com/jackie-d/simple-calculator)
(下次直接去github搜
打开查看源代码,尝试理解各部分含义。这个计算器还加入了用户登录、历史记录等功能。我在这一份代码的基础上参考、删改、增加等等等等
main controller(类名为Main) 整体流程、框架
'''java
@Autowired
MainService mainService;
//输入要算的式子
@RequestMapping(value="/solve/{number1}/{sign}/{number2}", method = RequestMethod.GET)
public ResponseEntity solveEquation(
@PathVariable("number1") String number11,
@PathVariable("sign") String sign,
@PathVariable("number2") String number21,
@RequestHeader(value="client-token", required=false) String clientToken
) {
boolean isInputAcceptable = isInputAcceptable(number1, number2, sign); //输入是否合规
if (!isInputAcceptable) {
return ("请正确输入");
}
(String number1, String number2, String sign){
return isIntNumber(number1) && isIntNumber(number2) && isValidSign(sign);
}
private boolean isIntNumber (String value){
try {
Integer.valueOf(value);
} catch (NumberFormatException e) {
return false;
}
return true;
}
private boolean isValidSign (String value){
return Equation.SignSymbols.values().contains(value);
}
private Equation getEquationSolution (String number1, String number2, String signSymbol){
int int1 = Integer.valueOf(number1);
int int2 = Integer.valueOf(number2);
//调用另一个类里的加减乘除
Equation.Sign sign = Equation.getSignBySymbol(signSymbol);
double result = 0;
switch (sign) {
case PLUS:
result = MainService.add(int1, int2);
break;
case MINUS:
result = MainService.subtract(int1, int2);
break;
case MULTIPLICATION:
result = MainService.multiply(int1, int2);
break;
case DIVISION:
result = MainService.divide(int1, int2);
break;
}
Equation solution = new Equation(int1, int2, sign, result);
return solution;
}
}
'''
主页
'''java
@GetMapping
public String Home() {
return "forward:/index.html";
}
}
'''
service 计算部分都放在这里了
'''java
package com.example.spring.Service;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Service
public class MainService {
//计算
public double add(int number1, int number2) {
return Double.valueOf(number1) + Double.valueOf(number2);
}
public double subtract(int number1, int number2) {
return Double.valueOf(number1) - Double.valueOf(number2);
}
public double multiply(int number1, int number2) {
return Double.valueOf(number1) * Double.valueOf(number2);
}
public double divide(int number1, int number2) {
return Double.valueOf(number1) / Double.valueOf(number2);
}
}
'''
Equation 一个用来各种“等量代换”、形式转换、数据传递等等的类
'''java
@Entity
public class Equation {
//字符传递、转换
public enum Sign {PLUS, MINUS, MULTIPLICATION, DIVISION};
// map for casting between char symbol representation and enum value
final public static HashMap SignSymbols = new HashMap() {{
put(Sign.PLUS, "+");
put(Sign.MINUS, "-");
put(Sign.MULTIPLICATION, "x");
put(Sign.DIVISION, "/");
}};
@GeneratedValue
private int number11;
private int number21;
private Sign sign = null;
private String signSymbol = null;
private double result;
if(number11>=pow(2,16)){
long number1 = number11;
}else if(number21>=pow(2,16)){
long number2 = number21;
}else{
int number1 = number11;
int number2 = number21;
}
public Equation( int number1, int number2, Sign sign,double resultn){
this.number1 = number1;
this.number2 = number2;
this.sign = sign;
this.signSymbol = this.SignSymbols.get(sign);
this.result = result;
}
public Equation( int number1, int number2, Sign sign,double result ){
this.number1 = number1;
this.number2 = number2;
this.sign = sign;
this.signSymbol = this.SignSymbols.get(sign);
this.result = result;
}
public Equation( int number1, int number2, Sign sign,double result ){
this.number2 = number2;
this.sign = sign;
this.signSymbol = this.SignSymbols.get(sign);
this.result = result;
}
public Equation( int number1, int number2, Sign sign ){
this.number1 = number1;
this.number2 = number2;
this.sign = sign;
this.signSymbol = this.SignSymbols.get(sign);
}
public int getNumber1() {
return number1;
}
public void setNumber1(int number1) {
this.number1 = number1;
}
public int getNumber2() {
return number2;
}
public void setNumber2(int number2) {
this.number2 = number2;
}
public Sign getSign() {
return sign;
}
public void setSign(Sign sign) {
this.sign = sign;
this.signSymbol = this.SignSymbols.get(sign);
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public String getSignSymbol() {
return signSymbol;
}
public void setSignSymbol(String signSymbol) {
this.signSymbol = signSymbol;
this.sign = getSignBySymbol(signSymbol);
}
}
'''
PS.查到了一个似乎是四则运算计算器前端的代码
https://github.com/lihualu0208/comit-caculator/tree/master