c語言求一元二次方程的根
㈠ 用C語言編寫一程序求解一元二次方程的根。
#include<stdio.h>
#include<math.h>
void m(float a,float b,float c)
{
double x1,x2;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("方程的根是%.2lf和%.2lf",x1,x2);
}
void n(float a,float b,float c)
{
double x;
x=(-b)/(2*a);
printf("方程的根為%.2lf",x);
}
void f(float a,float b,float c)
{
printf("方程無實數根\n");
}
main()
{
float a,b,c;
printf("請輸入a,b,c的值\n");
scanf("%f%f%f",&a,&b,&c);
if(b*b-4*a*c>0)
m(a,b,c);
if(b*b-4*a*c==0)
n(a,b,c);
if(b*b-4*a*c<0)
f(a,b,c);
}
㈡ c璇璦奼備竴鍏冧簩嬈℃柟紼嬬殑鏍
Ax2+bx+c=0
涓鍏冧簩嬈℃柟紼嬪彲鑳芥湁涓や釜瀹炴暟瑙o紝鎴栬呬竴涓瀹炴暟瑙o紝鎴栬呮棤瀹炴暟瑙c傛牴鎹杈撳叆鐨勪笁涓緋繪暟 a銆乥銆乧 鐨勫垽鏂瑙g殑鎯呭喌錛#include<stdio.h>#include<math.h>int main(){double disc,a,b,c,p,q,x1,x2;printf("璇瘋緭鍏a鍜宐鍜宑鐨勫礆細 ");
scanf("%lf %lf%lf",&a,&b,&c);disc=b*b-4*a*c;if(disc<0)printf("鏀規柟紼嬫棤瀹炴暟鏍筡n");else{p=(-b)/(2*a);q=sqrt(disc)/(2*a);x1=p+q;x2=p-q;printf("x1=%7.2f
x2=%7.2f
",x1,x2);}return 0;