Java通过Servelt与JSP实现两个复数加减乘除
时间: 2024-03-25 13:41:53 浏览: 126
两个复数的加减乘除
可以通过以下步骤实现Java Servlet与JSP实现两个复数的加减乘除:
1. 在JSP页面中,创建一个表单,包含输入两个复数的实部和虚部的文本框,以及选择要执行的操作的下拉框。
2. 在选择要执行的操作后,将表单数据提交到Servlet。
3. 在Servlet中,获取表单数据,并将实部和虚部转换为复数对象。
4. 根据所选操作执行相应的计算,并将结果存储在一个新的复数对象中。
5. 在Servlet中将结果作为属性设置并将请求转发回JSP页面。
6. 在JSP页面中,获取结果属性并将其显示在页面上。
下面是一个示例代码:
复数类:
```java
public class Complex {
private double real;
private double imag;
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImag() {
return imag;
}
public void setImag(double imag) {
this.imag = imag;
}
public Complex add(Complex other) {
return new Complex(real + other.real, imag + other.imag);
}
public Complex subtract(Complex other) {
return new Complex(real - other.real, imag - other.imag);
}
public Complex multiply(Complex other) {
double newReal = real * other.real - imag * other.imag;
double newImag = real * other.imag + imag * other.real;
return new Complex(newReal, newImag);
}
public Complex divide(Complex other) {
double denominator = other.real * other.real + other.imag * other.imag;
double newReal = (real * other.real + imag * other.imag) / denominator;
double newImag = (imag * other.real - real * other.imag) / denominator;
return new Complex(newReal, newImag);
}
@Override
public String toString() {
return String.format("%.2f + %.2fi", real, imag);
}
}
```
Servlet类:
```java
@WebServlet("/ComplexCalculator")
public class ComplexCalculatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
double real1 = Double.parseDouble(request.getParameter("real1"));
double imag1 = Double.parseDouble(request.getParameter("imag1"));
double real2 = Double.parseDouble(request.getParameter("real2"));
double imag2 = Double.parseDouble(request.getParameter("imag2"));
Complex complex1 = new Complex(real1, imag1);
Complex complex2 = new Complex(real2, imag2);
String operation = request.getParameter("operation");
Complex result = null;
if ("add".equals(operation)) {
result = complex1.add(complex2);
} else if ("subtract".equals(operation)) {
result = complex1.subtract(complex2);
} else if ("multiply".equals(operation)) {
result = complex1.multiply(complex2);
} else if ("divide".equals(operation)) {
result = complex1.divide(complex2);
}
request.setAttribute("result", result);
request.getRequestDispatcher("complex-calculator.jsp").forward(request, response);
}
}
```
JSP页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Complex Calculator</title>
</head>
<body>
<h1>Complex Calculator</h1>
<form method="post" action="ComplexCalculator">
<label>Complex 1:</label>
<input type="text" name="real1" placeholder="Real Part">
<input type="text" name="imag1" placeholder="Imaginary Part">
<br>
<label>Complex 2:</label>
<input type="text" name="real2" placeholder="Real Part">
<input type="text" name="imag2" placeholder="Imaginary Part">
<br>
<label>Operation:</label>
<select name="operation">
<option value="add">Addition</option>
<option value="subtract">Subtraction</option>
<option value="multiply">Multiplication</option>
<option value="divide">Division</option>
</select>
<br>
<input type="submit" value="Calculate">
</form>
<%
Complex result = (Complex) request.getAttribute("result");
if (result != null) {
%>
<p>Result: <%= result %></p>
<%
}
%>
</body>
</html>
```
阅读全文