#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
double f(double a, double b) {
double q;
q = a * a - 4 * b;
if (q >= 0) {
q = sqrt(q);
return q;
}
else {
q = sqrt(-q);
return -q;
}
}
double r(double x) {
if (x >= 0) {
return 1;
}
else
return -1;
}
int main() {
cout << "请输入三次方程的各项系数,若没有某一项则按0输入:" << endl;
double a, b, c, d;
cin >> a >> b >> c >> d;
double p, q, m, s, x1, x2, ans11=0,ans12=0, ans21=0,ans22=0,ans31=0,ans32=0;
p = c / a - (b * b / (a * a)) / 3;
q = (b * b * b / (a * a * a)) * 2 / 27 - (b * c / (a * a)) / 3 + d / a;
m = -(p * p * p) / 27;
s = f(q, m);
if (s >= 0) {
x1 = (-q + s) / 2;
x2 = (-q - s) / 2;
ans11 = r(x1)*pow(r(x1)*x1, 1.0 / 3) + r(x2)*pow(r(x2)*x2, 1.0 / 3)-b/(3*a);
ans12 = 0;
ans21 = -(ans11+b/(3*a))/2-b/(3*a);
ans22 = (r(x1) * pow(r(x1) * x1, 1.0 / 3) - r(x2) * pow(r(x2) * x2, 1.0 / 3))*sqrt(3)/2;
ans31 = -(ans11+b/(3*a))/2-b / (3 * a);
ans32 = (-r(x1) * pow(r(x1) * x1, 1.0 / 3) + r(x2) * pow(r(x2) * x2, 1.0 / 3)) * sqrt(3) / 2;
}
else
cout<<"很抱歉,判别式小于0的情况的计算还在设计中,敬请期待更新的版本"<<endl
cout << "方程的三个解分别是:" << endl;
cout << ans11<<'+'<<ans12<<'i' << endl;
cout << ans21 << '+' << ans22 << 'i' << endl;
cout << ans31 << '+' << ans32 << 'i' << endl;
return 0;
}