📄 cubicequation.java
字号:
package MyPakeage;
public class CubicEquation implements t1 {
double m, n, t, s, x1 = 0, x2 = 0, x3 = 0;
public void x1() {
if (m == 0) {
System.out
.print("Error Input!\n System will put the equation as a quadratic equation to solve.");
QuadraticEquation x = new QuadraticEquation();
x.a = n;
x.b = t;
x.c = s;
x.x1();
} else {
double a = n / m;
double b = t / m;
double c = s / m;
double q = (a * a - 3 * b) / 9;
double r = (2 * a * a * a - 9 * a * b + 27 * c) / 54;
if (r * r < q * q * q) {
System.out.println("The equation has three answer:");
t = Math.acos(r / Math.sqrt(q * q * q));
x1 = -2 * Math.sqrt(q) * Math.cos(t / 3) - a / 3;
x2 = -2 * Math.sqrt(q) * Math.cos((t + 2 * Math.PI) / 3) - a
/ 3;
x3 = -2 * Math.sqrt(q) * Math.cos((t - 2 * Math.PI) / 3) - a
/ 3;
System.out.println("x1=" + x1 + "," + "x2=" + x2 + "," + "x3="
+ x3);
} else {
System.out.println("The equation just has one answer:");
int sgn = (r >= 0) ? 1 : -1;
double u = -sgn
* Math.pow(
(Math.abs(r) + Math.sqrt(r * r - q * q * q)),
1. / 3);
double v = (u != 0) ? q / u : 0;
x1 = u + v - a / 3;
System.out.println("x=" + x1);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -