⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cubic_.cpp

📁 编写的一元四次方程的VC++程序代码
💻 CPP
字号:

 
//求解任意一元三次方程 a*x^3+b*x^2+c*x+d=0
//输入a,b,c,d;  输出三个解分别为x1,x2,x3
//

#include "stdafx.h"
#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;
		std::cout<<" x2="<< x2 <<endl;
		std::cout<<" x3="<< x3 <<endl;

	}
	else
	{
		
	p = -1.0/3*pow((b*1.0/a),2.0)+c*1.0/a;                  
    q = 2.0/27*pow((b*1.0/a),3.0)-1.0/3*b*c/(a*a)+d*1.0/a;  
	
	// p = -1/3*(b/a)^2+c/a;
	// q = 2/27*(b/a)^3-1/3*b*c/(a*a)+d/a;	
	//化成 y^3+p*y+q=0 形式


	delta = pow((q/2.0),2.0)+pow((p/3.0),3.0);
	
    //判别式 delta = (q/2)^2+(p/3)^3;


	
	cout<<" delta>0,有一个实根和两个复根;delta=0,有三个实根;delta<0,有三个不等的实根"<<endl; 

    cout<<" 判别式的值 delta="<<delta<<endl;
	//delta>0,有一个实根和两个复根;
	//delta=0,有三个实根;
    //delta<0,有三个不等的实根。

	

	
    complex<double>  omega1(-1.0/2, sqrt(3.0)/2.0);
	complex<double>  omega2(-1.0/2, -sqrt(3.0)/2.0);

	complex<double>  yy(b/(3.0*a),0.0);	

    
    M = -q/2.0;

	if(delta<0)
	{
		 N = sqrt(fabs(delta));
         complex<double>  s1(M,N);  
         complex<double>  s2(M,-N);	
		 
		 x1 = (pow(s1,(1.0/3))+pow(s2,(1.0/3)))-yy;
		 x2 = (pow(s1,(1.0/3))*omega1+pow(s2,(1.0/3))*omega2)-yy;
		 x3 = (pow(s1,(1.0/3))*omega2+pow(s2,(1.0/3))*omega1)-yy;		 
		 

		std::cout<<" x1="<< x1 <<endl;
		std::cout<<" x2="<< x2 <<endl;
		std::cout<<" x3="<< x3 <<endl;

		//输出结果 x1=y1-b/(3*a); x2=y2-b/(3*a); x3=y3-b/(3*a);
	}
	else
	{
		N = sqrt(delta);

		//cout<<" please output the M+N="<<M+N<<endl;
		//cout<<" please output the M-N="<<M-N<<endl;

		//  M+N= -q/2+sqrt((q/2)^2+(p/3)^3);
		//  M-N= -q/2-sqrt((q/2)^2+(p/3)^3);

		complex<double>  f1(M+N,0); 
		complex<double>  f2(M-N,0);

		 if(M+N >= 0)
		     temp1 = pow((f1),1.0/3);
		 else
			 temp1 = -norm(pow(sqrt(f1),1.0/3));


		 if(M-N >= 0)
			 temp2 = pow((f2),1.0/3);
		 else		
			 temp2 = -norm(pow(sqrt(f2),1.0/3));


		 x1 = temp1+temp2-yy;		 
         x2 = omega1*temp1+omega2*temp2-yy;
         x3 = omega2*temp1+omega1*temp2-yy; 


		 std::cout<<" x1="<< x1 <<endl;
	     std::cout<<" x2="<< x2 <<endl;
		 std::cout<<" x3="<< x3 <<endl;

		//输出结果 x1=y1-b/(3*a); x2=y2-b/(3*a); x3=y3-b/(3*a);
	}

	}

  return 0;

}





void  quadratic_equation(double a4,double b4,double c4,complex<double> &z1,complex<double> &z2)
{
		
	double delta;
	complex<double> temp1,temp2;

	delta=b4*b4-4*a4*c4; 

	
	complex<double> temp(delta,0);

    temp1 = (-b4)/(2*a4);
	temp2 = sqrt(temp)/(2*a4);

	z1=temp1+temp2;
	z2=temp1-temp2;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -