一元四次方程的简单修正算法

类别:编程语言 点击:0 评论:0 推荐:

//本程序基于.NET平台,编译环境是Microsoft Visual C++ .NET
//对于VC++6.0平台的修改,只须把stdafx.h换成iostream.h,然后去掉主函数里的“using namespace std”
#include "stdafx.h"
#include "math.h"
using namespace std;
double du,dv;

void solu_w(double b,double c)
{
 double tem,tem1;
 tem1=b*b-4*c;
 double m,n;
 m=(-1)*b/2;
 if(tem1>=0)
 {
  tem=sqrt(tem1);
  n=tem/2;
  cout<<"\n方程根为:"<<'\n'<<"\t X1 = "<<m+n<<'\n'<<"\t X2 = "<<m-n<<endl;
 }
 else
 {
  tem=sqrt((-1)*tem1);
  n=tem/2;
  cout<<"\n方程根为:"<<'\n'<<"\t X1 = "<<m<<" + "<<n<<" i"<<'\n'<<"\t X2 = "<<m<<" - "<<n<<" i"<<'\n'<<endl;
 }
}
void get_du_dv(double a,double b,double c,double d,double e,double f)
{
 dv=(b*d-a*e)/(c*e-b*f);
 du=(c*d-a*f)/(b*f-c*e);
}

void main()
{
 cout<<"输入四次方程的各个系数"<<endl;
 double a[5];
 double temp;
 for(int t=4;t>=0;t--)
 {  
  cout<<"\t a["<<t<<"]=";
  cin>>temp;
  a[t]=temp;
 }
 cout<<"\n方程为:\n\t "<<a[4]<<" (X*X*X*X) + "<<a[3]<<" (X*X*X) + ";
 cout<<a[2]<<" (X*X) + "<<a[1]<<" (X) + "<<a[0]<<" = 0"<<'\n'<<endl;
 double u,v;
 cout<<"输入因子的常数项和一次项系数:"<<endl;
 cout<<"\t u = ";
 cin>>u;
 cout<<"\t v = ";
 cin>>v;
 cout<<"\n因子式为:\n\t w(X) = "<<"(X*X) + "<<u<<" (X) + "<<v<<endl;

 double p0,p1,p2,r0,r1;

 cout<<"\n输入修正次数: ";
 int count1;
 cin>>count1;

 for(count1;count1>=0;count1--)
 {
  p2=a[4];
  p1=a[3]-u*p2;
  p0=a[2]-v*p2-u*p1;

  r0=a[1]-v*p1-u*p0;
  r1=a[0]-v*p0;

  //下面一行加在程序中,将导致的问题是:如果修正次数太大,程序运行时间大大延长,主要时间消耗在屏幕显示上。 
  //cout<<"\t p(X) = "<<p2<<" (X*X) + "<<p1<<" (X) + "<<p0<<" = 0"<<endl;

  double pp3,pp2,pp1,pp0,r0v,r1v,r0u,r1u;
  pp3=(-1)*p2;
  pp2=(-1)*p1;
  pp1=(-1)*p0;
  pp0=0;

  r0v=u*p2-p1;
  r1v=v*p2-p0;
  r0u=r1v-u*r0v;
  r1u=(-1)*v*r0v;

  get_du_dv(r0,r0u,r0v,r1,r1u,r1v);

  u=u+du;
  v=v+dv;
 }

 solu_w(u,v);
}

本文地址:http://com.8s8s.com/it/it22808.htm