c++求解一元二次方程
#include<iostream.h>
#include<math.h>
void main()
{
double a,b,c,d,e,x1,x2;
cout<<"请输入要求解的一元二次方程:"<<endl;
cout<<"a: ";
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"c: ";
cin>>c;
cout<<a<<"*x*x"<<"+"<<b<<"*x"<<"+"<<c<<"=0"<<endl;
d=b*b-4*a*c;
if(d<0)
{
cout<<"There is no x."<<endl;
}
if(d==0)
{
cout<<"There is only x."<<endl;
x1=x2=(-b)/(2*a);
cout<<"x1=x2="<<x1<<endl;
}
if(d>0)
{
e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
}
扩展资料:
一元二次方程成立必须同时满足三个条件:
1、是整式方程,即等号两边都是整式,方程中如果有分母;且未知数在分母上,那么这个方程就是分式方程,不是一元二次方程,方程中如果有根号,且未知数在根号内,那么这个方程也不是一元二次方程(是无理方程)。
2、只含有一个未知数;
3、未知数项的最高次数是2 。
#include<iostream.h>
#include<math.h>
void main()
{
double a,b,c,d,e,x1,x2;
cout<<"请输入要求解的一元二次方程:"<<endl;
cout<<"a: ";
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"c: ";
cin>>c;
cout<<a<<"*x*x"<<"+"<<b<<"*x"<<"+"<<c<<"=0"<<endl;
d=b*b-4*a*c;
if(d<0)
{
cout<<"There is no x."<<endl;
}
if(d==0)
{
cout<<"There is only x."<<endl;
x1=x2=(-b)/(2*a);
cout<<"x1=x2="<<x1<<endl;
}
if(d>0)
{
e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
}
复杂点的
#include<iostream>
#include<math.h>
using namespace std;
class funtion
{
public:
~funtion(){cout<<"求解完毕"<<endl;}
void set_value(); //输入函数
void display(); //求解函数
void show_value();//输出函数
private:
float a;
float b;
float c;
float x1;
float x2;
float r;
float i;
float pd;
};
void funtion::set_value ()
{
cout<<"输入 a b c 的值且 a b c 不可全为零"<<endl;
cin>>a;
cin>>b;
cin>>c;
}
void funtion::display ()
{
pd=b*b-4*a*c;
if(pd>0)
{
x1=-b/(2*a)+sqrt(pd)/(2*a);
x2=-b/(2*a)-sqrt(pd)/(2*a);
}
else if(pd==0)
{
x1=-b/(2*a);
x2=-b/(2*a);
}
else
{
r=-b/(2*a);
i=sqrt(-pd)/(2*a);
}
}
void funtion::show_value ()
{
if(pd>0)
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
else if(pd==0)
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
else
cout<<"x1="<<r<<'+'<<i<<"i"<<" "<<"x2="<<r<<'+'<<i<<"i"<<endl;
}
int main()
{
funtion f;
f.set_value ();
f.display ();
f.show_value ();
return 0;
}
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
void main()
{
float a,b,c;
double x1,x2;
cout<<"请输入a,b,c:"<<endl;
cin>>a>>b>>c;
if(a!=0)
{
if((b*b-4*a*c)>=0)
{
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
cout<<"x1="<<x1<<setw(5)<<"x2="<<x2<<endl;
}
else
cout<<"无解"<<endl;
}
else
{
if(b==0)//2个=是判断相等,一个是赋值。
{
if(c==0)//这里也是一样的。
cout<<"x为任意解"<<endl;
else
cout<<"无解"<<endl;
}
else
{ x1=-c/b;
x2=-c/b;}
cout<<"x1="<<x1<<setw(5)<<"x2="<<x2<<endl;
}
}
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
void sqrt1(double,double);
void sqrt2(double,double);
void printer(double,double);
void main()
{
float a,b,c;
double x1,x2;
cout<<"请输入a,b,c:"<<endl;
cin>>a>>b>>c;
if(b*b-4*a*c !=0)
{
if(b*b-4*a*c)
{
sqrt1(x1,x2);
}
else
{
printer(x1,x2);
}
}
else
{
sqrt2(x1,x2);
}
}
void sqrt1(double x1,double x2) //b*b-4ac>0
{
float a,b,c;
if(a!=0)
{
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
cout<<"x1="<<x1<<setw(5)<<"x2="<<x2<<endl;
}
else
{
if(b==0)
{
if(c==0)
cout<<"x为任意解"<<endl;
else
cout<<"无解"<<endl;
}
else
{
x1=x2=-c/b;
cout<<"x1="<<"x2="<<x1<<endl;
}
}
}
void sqrt2(double x1,double x2) //b*b-4ac=0
{
float a,b,c;
x1=x2=-b/(2*a);
cout<<"x1="<<"x2="<<x1<<endl;
}
void printer (double x1,double x2) //b*b-4ac<0
{
float a,b,c;
char i='i';//i后面为虚部
x1=(-b/(2*a))+(i*(sqrt(b*b-4*a*c)/(2*a)));
x2=(-b/(2*a))-(i*(sqrt(b*b-4*a*c)/(2*a)));
cout<<"x1="<<x1<<"\t"<<"x2="<<x2<<endl;
}