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;