JavaBea+JSP、Servlet+JSP、JavaBean+Servlet+JSP实现复数运算器
目录
JavaBea+JSP
代码:
JavaBea
JSP
Servlet+JSP
代码:
Servlet:
JSP:
JavaBean+Servlet+JSP
代码:
JavaBean:
Servlet:
JSP:
JavaBea+JSP
代码:
JavaBea
public class Complex {
private double a;
private double b;
public Complex(double a, double b) {
this.a = a;
this.b = b;
}
public Complex add(Complex c) {
return new Complex(this.a + c.a, this.b + c.b);
}
public Complex subtract(Complex c) {
return new Complex(this.a - c.a, this.b - c.b);
}
public Complex multiply(Complex c) {
double a = this.a * c.a - this.b * c.b;
double b = this.a * c.b + this.b * c.a;
return new Complex(a, b);
}
public Complex divide(Complex c) {
double denominator = c.a * c.a + c.b * c.b;
double a = (this.a * c.a + this.b * c.b) / denominator;
double b = (this.b * c.a - this.a * c.b) / denominator;
return new Complex(a, b);
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
}
JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.dong.demo4.bean" %>
<%@ page import="com.dong.demo4.bean.Complex" %>
<html>
<head>
<title>复数计算器</title>
</head>
<body>
<h1>复数计算器</h1>
<form method="post">
<label>第一个复数:</label>
<input type="text" name="num1">
<label>+</label>
<input type="text" name="num2">i
<br>
<label>第二个复数:</label>
<input type="text" name="num3">
<label>+</label>
<input type="text" name="num4">i
<br>
<label>运算符:</label>
<select name="operator">
<option value="+">加法</option>
<option value="-">减法</option>
<option value="*">乘法</option>
<option value="/">除法</option>
</select>
<br>
<br>
<input type="submit" name="submit" value="计算">
</form>
<% if (request.getParameter("submit") != null) { %>
<br>
<h2>计算结果:</h2>
<% double num1 = Double.parseDouble(request.getParameter("num1")); %>
<% double num2 = Double.parseDouble(request.getParameter("num2")); %>
<% double num3 = Double.parseDouble(request.getParameter("num3")); %>
<% double num4 = Double.parseDouble(request.getParameter("num4")); %>
<% Complex c1 = new Complex(num1, num2); %>
<% Complex c2 = new Complex(num3, num4); %>
<%
String op = request.getParameter("operator");
Complex result = null;
switch (op) {
case "+":
result = c1.add(c2);
break;
case "-":
result = c1.subtract(c2);
break;
case "*":
result = c1.multiply(c2);
break;
case "/":
result = c1.divide(c2);
break;
default:
break;
}
%>
<p><%= result.getReal() %><%= result.getImaginary() >= 0 ? "+" : "-" %><%= Math.abs(result.getImaginary()) %>i</p>
<% } %>
</body>
</html>
Servlet+JSP
代码:
Servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/complexCalculatorServlet ")
public class ComplexCalculatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
double num1 = Double.parseDouble(request.getParameter("num1"));
double num2 = Double.parseDouble(request.getParameter("num2"));
double num3 = Double.parseDouble(request.getParameter("num3"));
double num4 = Double.parseDouble(request.getParameter("num4"));
String operator = request.getParameter("operator");
double resultA = 0;
double resultB = 0;
switch (operator) {
case "+":
resultA = num1 + num3;
resultB = num2 + num4;
break;
case "-":
resultA = num1 - num3;
resultB = num2 - num4;
break;
case "*":
resultA = num1 * num3 - num2 * num4;
resultB = num1 * num4 + num2 * num3;
break;
case "/":
double denominator = num3 * num3 + num4 * num4;
resultA = (num1 * num3 + num2 * num4) / denominator;
resultB = (num2 * num3 - num1 * num4) / denominator;
break;
default:
break;
}
// 将结果存储到 request 对象中
request.setAttribute("resultA", resultA);
request.setAttribute("resultB", resultB);
// 转发请求到 JSP 页面
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
}
JSP:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>复数计算器</title>
</head>
<body>
<h1>复数计算器</h1>
<form action="/complexCalculatorServlet"
method="post">
<label>第一个复数:</label>
<input type="text" name="num1">
<label>+</label>
<input type="text" name="num2">i
<br>
<label>第二个复数:</label>
<input type="text" name="num3">
<label>+</label>
<input type="text" name="num4">i
<br>
<label>运算符:</label>
<select name="operator">
<option value="+">加法</option>
<option value="-">减法</option>
<option value="*">乘法</option>
<option value="/">除法</option>
</select>
<br>
<br>
<input type="submit" name="submit" value="计算">
</form>
<% if (request.getAttribute("resultA") != null) { %>
<br>
<h2>计算结果:</h2>
<p><%= request.getAttribute("resultA") %><%= (Double)request.getAttribute("resultB") >= 0 ? "+" : "-" %><%= Math.abs((Double)request.getAttribute("resultB")) %>i</p>
<% } %>
</body>
</html>
JavaBean+Servlet+JSP
代码:
JavaBean:
package com.dong.demo1;
/**
* 模式三:JavaBean+Servlet+JSP
*/
/**
* 复数类---JavaBean类
*/
public class Complex {
private double real;//实部
private double imaginary;//虚部
public Complex() {
}
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
/**
* @Discription 加法
* @author Lidong
* @param other
* @return
*/
public Complex add(Complex other) {
return new Complex(this.real + other.real, this.imaginary + other.imaginary);
}
/**
* @Discription 减法
* @author Lidong
* @param other
* @return
*/
public Complex subtract(Complex other) {
return new Complex(this.real - other.real, this.imaginary - other.imaginary);
}
/**
* @Discription 乘法
* @author Lidong
* @param other
* @return
*/
public Complex multiply(Complex other) {
double realPart = this.real * other.real - this.imaginary * other.imaginary;
double imaginaryPart = this.real * other.imaginary + this.imaginary * other.real;
return new Complex(realPart, imaginaryPart);
}
/**
* @Discription 除法
* @author Lidong
* @param other
* @return
* @throws ArithmeticException
*/
public Complex divide(Complex other) throws ArithmeticException {
double denominator = other.real * other.real + other.imaginary * other.imaginary;
if (denominator == 0) {
throw new ArithmeticException("Divide by zero!");
}
double realPart = (this.real * other.real + this.imaginary * other.imaginary) / denominator;
double imaginaryPart = (-this.real * other.imaginary + this.imaginary * other.real) / denominator;
return new Complex(realPart, imaginaryPart);
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
}
Servlet:
package com.dong.demo1;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/complexCalc")
public class ComplexCalcServlet extends HttpServlet {
// private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取第一个复数
String real1Str = request.getParameter("real1");
String imaginary1Str = request.getParameter("imaginary1");
//获取第二个复数
String real2Str = request.getParameter("real2");
String imaginary2Str = request.getParameter("imaginary2");
//获取运算符:{ +、 —— 、* 、/ }
String operator = request.getParameter("operator");
double real1 = Double.parseDouble(real1Str);
double imaginary1 = Double.parseDouble(imaginary1Str);
double real2 = Double.parseDouble(real2Str);
double imaginary2 = Double.parseDouble(imaginary2Str);
Complex complex1 = new Complex(real1, imaginary1);
Complex complex2 = new Complex(real2, imaginary2);
Complex result;
switch (operator) {
case "+":
result = complex1.add(complex2);
break;
case "-":
result = complex1.subtract(complex2);
break;
case "*":
result = complex1.multiply(complex2);
break;
case "/":
try {
result = complex1.divide(complex2);
} catch (ArithmeticException e) {
request.setAttribute("error", e.getMessage());
request.getRequestDispatcher("/complex.jsp").forward(request, response);
return;
}
break;
default:
throw new IllegalArgumentException("无效的操作!");
}
request.setAttribute("result", result);
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
JSP:
输入页面:
<%—
Created by IntelliJ IDEA.
User: CaptainDong
Date: 2023/5/31
Time: 10:42
To change this template use File | Settings | File Templates.
—%>
<%@ page contentType=”text/html;charset=UTF-8” language=”java” %>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core“ %>
<!DOCTYPE html>
复数计算器 复数计算器
结果如下:
${result.real} + ${result.imaginary}i
错误:
${error}
结果展示:
<%@ page import=”com.dong.demo1.Complex” %>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core“ %>
<%—
Created by IntelliJ IDEA.
User: CaptainDong
Date: 2023/5/31
Time: 11:07
To change this template use File | Settings | File Templates.
—%>
<%@ page contentType=”text/html;charset=UTF-8” language=”java” %>
运算结果 结果如下:
<%
Complex result= (Complex) request.getAttribute(“result”);
String s1=result.getReal()+” “+”+”+”(“+result.getImaginary()+” “+”i”+”)”;
%>
<%=s1%>
结果如下:
${result.real} + ${result.imaginary}i
错误信息:
${error}
还没有评论,来说两句吧...