请用Servlet加jsp文件完整实现复数加减乘除四则运算
时间: 2024-02-23 09:00:51 浏览: 104
jsp+servlet实现文件上传下载
4星 · 用户满意度95%
好的,以下是一个简单的实现:
1. 编写一个Servlet来处理复数运算:
```java
@WebServlet("/complex")
public class ComplexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String op = request.getParameter("op");
Complex c1 = new Complex(Double.parseDouble(request.getParameter("real1")), Double.parseDouble(request.getParameter("imag1")));
Complex c2 = new Complex(Double.parseDouble(request.getParameter("real2")), Double.parseDouble(request.getParameter("imag2")));
Complex result = null;
switch (op) {
case "add":
result = Complex.add(c1, c2);
break;
case "subtract":
result = Complex.subtract(c1, c2);
break;
case "multiply":
result = Complex.multiply(c1, c2);
break;
case "divide":
result = Complex.divide(c1, c2);
break;
default:
break;
}
request.setAttribute("result", result);
request.getRequestDispatcher("/complex.jsp").forward(request, response);
}
}
```
2. 编写一个Complex类来表示复数:
```java
public class Complex {
private double real;
private double imag;
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public static Complex add(Complex c1, Complex c2) {
return new Complex(c1.real + c2.real, c1.imag + c2.imag);
}
public static Complex subtract(Complex c1, Complex c2) {
return new Complex(c1.real - c2.real, c1.imag - c2.imag);
}
public static Complex multiply(Complex c1, Complex c2) {
double real = c1.real * c2.real - c1.imag * c2.imag;
double imag = c1.real * c2.imag + c1.imag * c2.real;
return new Complex(real, imag);
}
public static Complex divide(Complex c1, Complex c2) {
double denominator = c2.real * c2.real + c2.imag * c2.imag;
double real = (c1.real * c2.real + c1.imag * c2.imag) / denominator;
double imag = (c1.imag * c2.real - c1.real * c2.imag) / denominator;
return new Complex(real, imag);
}
@Override
public String toString() {
return real + (imag >= 0 ? "+" : "-") + Math.abs(imag) + "i";
}
}
```
3. 创建一个jsp页面来展示结果:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Complex Calculator</title>
</head>
<body>
<h1>Complex Calculator</h1>
<form action="${pageContext.request.contextPath}/complex" method="post">
<input type="hidden" name="op" value="${param.op}">
<label>Complex Number 1:</label>
<input type="text" name="real1" value="${param.real1}">
<input type="text" name="imag1" value="${param.imag1}">i
<br>
<label>Complex Number 2:</label>
<input type="text" name="real2" value="${param.real2}">
<input type="text" name="imag2" value="${param.imag2}">i
<br>
<button type="submit" name="op" value="add">+</button>
<button type="submit" name="op" value="subtract">-</button>
<button type="submit" name="op" value="multiply">*</button>
<button type="submit" name="op" value="divide">/</button>
</form>
<br>
<h2>Result:</h2>
<% if (request.getAttribute("result") != null) { %>
<p>${request.getAttribute("result")}</p>
<% } %>
</body>
</html>
```
现在,您可以在浏览器中访问该Servlet并进行复数运算了。
阅读全文