//求解任意一元三次方程 a*x^3+b*x^2+c*x+d=0
//输入a,b,c,d; 输出三个解分别为x1,x2,x3
//
#include <complex>
#include <iostream>
#include <math.h>
using namespace std;
void quadratic_equation(double a4,double b4,double c4,complex<double> &z1,complex<double> &z2);
int main()
{
float a,b,c,d;
double p,q,delta;
double M,N;
complex<double> temp1,temp2;
complex<double> x1,x2,x3;
complex<double> y1,y2;
cout<<" please input the coefficients a, b, c, d : ";
cin>>a>>b>>c>>d;
//输入一元三次方的系数,得方程为a*x^3+b*x^2+c*x+d=0;
if(a==0)
{
quadratic_equation(b,c,d,y1,y2);
x1 = y1;
x2 = y2;
x3 = 0;
std::cout<<" x1="<< x1 <<endl;<br> std::cout<<" x2="<< x2 <<endl;<br> std::cout<<" x3="<< x3 <<endl;<br><br> }<br> else<br> {<br> <br> p = -1.0/3*pow((b*1.0/a),2.0)+c*1.0/a; <br> q = 2.0/27*pow((b*1.0/a),3.0)-1.0/3*b*c/(a*a)+d*1.0/a; <br> <br> // p = -1/3*(b/a)^2+c/a;<br> // q = 2/27*(b/a)^3-1/3*b*c/(a*a)+d/a; <br> //化成 y^3+p*y+q=0 形式<br><br><br> delta = pow((q/2.0),2.0)+pow((p/3.0),3.0);<br> <br> //判别式 delta = (q/2)^2+(p/3)^3;<br><br><br> <br> cout<<" delta>0,有一个实根和两个复根;delta=0,有三个实根;delta<0,有三个不等的实根"<<endl;
cout<<" 判别式的值 delta="<<delta<<endl;<br> //delta>0,有一个实根和两个复根;<br> //delta=0,有三个实根;<br> //delta<0,有三个不等的实根。<br><br> <br><br> <br> complex<double> omega1(-1.0/2, sqrt(3.0)/2.0);<br> complex<double> omega2(-1.0/2, -sqrt(3.0)/2.0);<br><br> complex<double> yy(b/(3.0*a),0.0); <br><br> <br> M = -q/2.0;<br><br> if(delta<0)<br> {<br> N = sqrt(fabs(delta));<br> complex<double> s1(M,N); <br> complex<double> s2(M,-N); <br> <br> x1 = (pow(s1,(1.0/3))+pow(s2,(1.0/3)))-yy;<br> x2 = (pow(s1,(1.0/3))*omega1+pow(s2,(1.0/3))*omega2)-yy;<br> x3 = (pow(s1,(1.0/3))*omega2+pow(s2,(1.0/3))*omega1)-yy; <br> <br><br> std::cout<<" x1="<< x1 <<endl;<br> std::cout<<" x2="<< x2 <<endl;<br> std::cout<<" x3="<< x3 <<endl;<br><br> //输出结果 x1=y1-b/(3*a); x2=y2-b/(3*a); x3=y3-b/(3*a);<br> }<br> else<br> {<br> N = sqrt(delta);<br><br> //cout<<" please output the M+N="<<M+N<<endl;<br> //cout<<" please output the M-N="<<M-N<<endl;<br><br> // M+N= -q/2+sqrt((q/2)^2+(p/3)^3);<br> // M-N= -q/2-sqrt((q/2)^2+(p/3)^3);<br><br> complex<double> f1(M+N,0); <br> complex<double> f2(M-N,0);<br><br> if(M+N >= 0)<br> temp1 = pow((f1),1.0/3);<br> else<br> temp1 = -norm(pow(sqrt(f1),1.0/3));<br><br><br> if(M-N >= 0)<br> temp2 = pow((f2),1.0/3);<br> else <br> temp2 = -norm(pow(sqrt(f2),1.0/3));<br><br><br> x1 = temp1+temp2-yy; <br> x2 = omega1*temp1+omega2*temp2-yy;<br> x3 = omega2*temp1+omega1*temp2-yy; <br><br><br> std::cout<<" x1="<< x1 <<endl;<br> std::cout<<" x2="<< x2 <<endl;<br> std::cout<<" x3="<< x3 <<endl;<br><br> //输出结果 x1=y1-b/(3*a); x2=y2-b/(3*a); x3=y3-b/(3*a);<br> }<br><br> }<br><br> return 0;<br><br>}<br><br><br><br><br><br>void quadratic_equation(double a4,double b4,double c4,complex<double> &z1,complex<double> &z2)<br>{<br> <br> double delta;<br> complex<double> temp1,temp2;<br><br> delta=b4*b4-4*a4*c4; <br><br> <br> complex<double> temp(delta,0);<br><br> temp1 = (-b4)/(2*a4);<br> temp2 = sqrt(temp)/(2*a4);<br><br> z1=temp1+temp2;<br> z2=temp1-temp2;<br> <br>}
200字以内,仅用于支线交流,主线讨论请采用回复功能。