该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
using namespace std;
void fun(double a,double b,double c)
{
double det;
if (fabs(a) < 0.00000001)
{
cout << "这不是二次方程" << endl;
return ;
}
det = b*b - 4*a*c;
//cout<
if (fabs(det) < 0.00000001)
{
double x = -b*0.5/a;
cout << "方程只有一根:"<< endl << x << endl;
}
else if (det > 0.0)
{
cout << "方程有两个实根 :" << endl ;
double x1,x2;
x1 = (sqrt(det) - b) / (2 * a);
x2 = (-sqrt(det) - b) / (2 * a);
cout << x1 << endl << x2 << endl;
}
else
{
cout << "方程只有两个虚数解:" << endl ;
//对输出做下判断,+1和-1时不输出1;
double temp = sqrt(-det) / (2 * a);
double k = -b/(2 * a);
if (fabs(k) < 0.0000001)
{
if (fabs(b / (2 * a)))
{
}
if (fabs(fabs(temp) - 1) < 0.00000001)
{
cout << '+' << 'i' << endl;
cout << '-' << 'i' << endl;
}
else
{
cout << '+' << temp << 'i' << endl;
cout << '-' << temp << 'i' << endl;
}
}
else
{
if (fabs(b / (2 * a)))
{
}
if (fabs(fabs(temp) - 1) < 0.00000001)
{
cout << k << '+' << 'i' << endl;
cout << k << '-' << 'i' << endl;
}
else
{
cout << k << '+' << temp << 'i' << endl;
cout << k << '-' << temp << 'i' << endl;
}
}
}
}
int main()
{
fun(1,2,1);
getchar();
}
随便写下,你自己看看然后自己再写个。