第五篇 求方程ax^2 +bx+c=0的根

时间:2024-04-13 13:41:16

求方程ax^2 +bx+c=0的根,用3个函数分别求当:b^2-4ac大于0,等于0和小于0时的根并输出结果。从主函数输入a,b,c的值。

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void l(double a, double b, double c, double x, double y)//判断德尔塔
{
	if ((b*b - 4 * a*c) > 0)
		n(a, b, c, x, y);//当德尔塔大于0时,调用n函数
	else
		if ((b*b - 4 * a*c) == 0)
			m(a, b, c, x);//当德尔塔等于0时,调用m函数
		else
			if ((b*b - 4 * a*c) < 0)
				k(a, b, c);//当德尔塔小于0时,调用k函数
}
void n(double a, double b, double c, double x, double y)
{
	x = (-b + sqrt(b*b - 4 * a*c)) / (2 * a);
	y= (-b - sqrt(b*b - 4 * a*c)) / (2 * a);
	printf("方程的根是:%.2lf和%.2lf", x, y);
}
void m(double a, double b, double c, double x)
{
	x = (-b) / (2 * a);
	printf("方程的根是:%.2lf", x);
}
void k(double a, double b, double c)
{
	printf("方程无实根\n");
}
int main()
{
	double a, b, c, x = 0, y = 0;
	printf("输入三个函数值:a,b,c\n");
	scanf_s("%lf %lf %lf", &a, &b, &c);
	l(a, b, c, x, y);
	getchar();
	getchar();
	return 0;
}


第五篇 求方程ax^2 +bx+c=0的根