C++一元二次方程求虚根,谁会,先谢了

2024-12-16 03:10:30
推荐回答(2个)
回答1:

#include
#include
using namespace std;
int main(void)
{
float a=1.0f, b = .0f, c = .0f, d =.0f;
cout << "a, b, c=?" << end;
cin >> a >> b >> c;
d = b * b - 4 * a * c;
a *= 2.0f; b = -b;
if (d < .0f) {
cout << "方程有一对虚根:" << endl;
d = sqrt(-d) / a;
b /= a;
cout << "x1 = " << b << "+ " << d << "i" << endl;
cout << "x2 = " << b << "- " << d << "i" << endl;
return 0;
}//end if
if (d > .0f) {
cout << "方程有一对实根:" << endl;
d = sqrt(d) / a;
b /= a;
cout << "x1 = " << b << "+ " << d << endl;
cout << "x2 = " << b << "- " << d << endl;
return 0;
}//end if
cout << "方程有一对相等的实根:x1 = x2 = " << b/a << end;
return 0;
}

回答2:

# include
# include
# include
# include
int main(void)
{
double a=1;//数据输入
double b=0;//数据输入
double c=1;//数据输入
double delta;//验根公式
double x1;//实数调用
double x2;//实数调用
double del;//虚数调用
double xi; //虚数调用
double x2i;//虚数调用
delta=b*b-4*a*c;
del=sqrt(4*a*c-b*b);//虚数调用
xi=(-b)/(2*a);//虚数调用
x2i=del/(2*a);//虚数调用
if (delta>0)
{x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
printf("该一元二次方程有两个实数根,x1=%f,x2=%f\n",x1,x2);
}
else if (delta==0)
{x1=xi;
x1=x2;
printf("该一元二次方程有一个实数根,x1=x2=%f\n",x1);
}
else if (delta<0)
{ if (b==0)
{if(x2i==1)
{cout<<"该一元二次方程有两个虚数根,x1=i"< cout<<"x2=-i"< } //B等于零,x2i等于一

else
{cout<<"该一元二次方程有两个虚数根,x1="< cout<<"x2=-"< };
}
else
{if (x2i==1)
{cout<<"该一元二次方程有两个虚数根,x1="< cout<<"x2="< }//b不等于0,x2i等于一
else
{cout<<"该一元二次方程有两个虚数根,x1="< cout<<"x2="< };//b不等于0,x2i不等于1
}
};
system("PAUSE");
return 0;
}