一元二次方程C语言实现
// 一元二次方程.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "math.h"
int main(int argc, char* argv[])
{
double a,b,c,d,x1,x2,temp;
scanf("%lf %lf %lf",&a,&b,&c);
if(0==a&&0!=b)
{
x1=-c/b;
printf("一次方程只有一个根%.2lf",x1);
}
else if(0==a&&0==b)
{
printf("Not an equation");
}
else if(0!=a)
{
d=b*b-4*a*c;
if(d>=0)
{
x1=(-b+sqrt(d))/2*a;
x2=(-b-sqrt(d))/2*a;
if(x1<x2)
{
temp=x1;
x1=x2;
x2=temp;
}
printf("x1=%.2lf,x2=%.2lf",x1,x2);
}
else
{
x1=-b/2*a;
x2=sqrt(-d)/2*a;
if(0>x2)
{
x2=-x2;
}
printf("x1=%.2lf+%.2lfi,x2=%.2lf+%.2lfi",x1,x2,x1,x2);
}
}
printf("\n");
return 0;
}
浙公网安备 33010602011771号