C练习——一元二次方程求解

题目:编程计算并输出一元二次方程ax '+bx+c=0的两个根

解析:先判断delta与0的关系,

        若delta>0,则有两个不同的实根,利用求根公式( -b 土  √b^2 -4*a*c)/2*a ;

        若delta=0,则有两个相同的实根,则根为x1 = x2 = -(b/2*a);

        若delta<0,则无实根;

逻辑:if判读选择

# include 
# include 

int main()
{
	double a = 0;
	double b = 0;
	double c = 0;
	double delta =0;
	double x1 = 0;
    double x2 = 0;
	int flag = 0;
	do
	{
		printf("输入a,b,c:\n");
		scanf("%lf %lf %lf", &a, &b, &c);

		delta = b * b - 4 * a * c;

		if (delta > 0)
		{
			x1 = (-b + sqrt(delta)) / (2 * a);
			x2 = (-b - sqrt(delta)) / (2 * a);
			printf("有两个解,x1 = %f, x2 = %f\n", x1, x2);
		}
		else if (0 == delta)
		{
			x1 = x2 = (-b) / (2 * a);
			printf("x1 = x2 = %f\n", x1, x2);
		}
		else
		{
			printf("无实数解!\n");
		}

		printf("继续 1,退出 0: ");
		scanf("%d", &flag);  //
	} while (flag);
	return 0;
}

你可能感兴趣的:(C练习题,c语言,学习)