请用JavaBean加jsp文件完整实现复数加减乘除四则运算
时间: 2024-02-23 15:00:23 浏览: 66
首先,我们需要创建一个JavaBean类来表示复数:
```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) {
double real = this.real + other.real;
double imag = this.imag + other.imag;
return new Complex(real, imag);
}
public Complex subtract(Complex other) {
double real = this.real - other.real;
double imag = this.imag - other.imag;
return new Complex(real, imag);
}
public Complex multiply(Complex other) {
double real = this.real * other.real - this.imag * other.imag;
double imag = this.real * other.imag + this.imag * other.real;
return new Complex(real, imag);
}
public Complex divide(Complex other) {
double denominator = other.real * other.real + other.imag * other.imag;
double real = (this.real * other.real + this.imag * other.imag) / denominator;
double imag = (this.imag * other.real - this.real * other.imag) / denominator;
return new Complex(real, imag);
}
@Override
public String toString() {
return String.format("%.2f + %.2fi", real, imag);
}
}
```
然后,我们可以在JSP文件中使用这个JavaBean类来实现复数的加减乘除四则运算:
```jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.text.DecimalFormat" %>
<jsp:useBean id="complex1" class="Complex" scope="request">
<jsp:setProperty name="complex1" property="real" value="${param.real1}" />
<jsp:setProperty name="complex1" property="imag" value="${param.imag1}" />
</jsp:useBean>
<jsp:useBean id="complex2" class="Complex" scope="request">
<jsp:setProperty name="complex2" property="real" value="${param.real2}" />
<jsp:setProperty name="complex2" property="imag" value="${param.imag2}" />
</jsp:useBean>
<%
Complex result = null;
String operation = request.getParameter("operation");
if ("+".equals(operation)) {
result = complex1.add(complex2);
} else if ("-".equals(operation)) {
result = complex1.subtract(complex2);
} else if ("*".equals(operation)) {
result = complex1.multiply(complex2);
} else if ("/".equals(operation)) {
result = complex1.divide(complex2);
}
%>
<html>
<head>
<title>复数计算器</title>
</head>
<body>
<h1>复数计算器</h1>
<form method="post">
<label>第一个复数:</label>
<input type="text" name="real1" value="${complex1.real}" /> +
<input type="text" name="imag1" value="${complex1.imag}" />i
<br />
<label>第二个复数:</label>
<input type="text" name="real2" value="${complex2.real}" /> +
<input type="text" name="imag2" value="${complex2.imag}" />i
<br />
<label>运算符:</label>
<select name="operation">
<option value="+">加</option>
<option value="-">减</option>
<option value="*">乘</option>
<option value="/">除</option>
</select>
<br />
<input type="submit" value="计算" />
</form>
<hr />
<h2>结果:</h2>
<c:if test="${not empty result}">
<p>${complex1} ${operation} ${complex2} = ${result}</p>
</c:if>
</body>
</html>
```
在这个JSP文件中,我们使用了JSP的EL表达式和JSTL标签库来简化代码。我们首先使用`<jsp:useBean>`标签来创建两个复数对象,并从HTTP请求参数中获取输入的实部和虚部值。然后,我们从HTTP请求参数中获取运算符,并根据运算符调用相应的方法来计算结果。最后,我们使用JSTL的<c:if>标签来判断是否有结果,并将结果显示在页面上。
需要注意的是,为了避免XSS攻击,我们应该对输入的实部和虚部值进行适当的过滤和转义。
阅读全文