求解一元二次方程

#include<stdio.h>
#include<math.h>

int main(void)
{
	int a, b, c;
	int delt;
	double x1, x2;
	int flag = 1;

	do
	{
		printf("求解一元二次方程\n");
		printf("请输入二次项系数:");
		scanf_s("%d", &a);
		printf("请输入一次项系数:");
		scanf_s("%d", &b);
		printf("请输入常数项:");
		scanf_s("%d", &c);

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

		if (delt > 0)
		{
			x1 = (-b + sqrt(delt)) / (2 * a);	//有算术溢出,还不知道是什么意思
			x2 = (-b - sqrt(delt)) / (2 * a);
			printf("该一元二次方程有两个解,x1 = %f,x2 = %f\n", x1, x2);
		}
		else
		{
			if (delt == 0)
			{
				x1 = -b / (2 * a);
				x2 = x1;
				printf("该一元二次方程有两个相同的解,x1 = x2 = %f\n", x1);
			}
			else
			{
				printf("此方程无解。\n");
			}
		}

		printf("继续求解请按“1”,退出请按“2”:");
		scanf_s("%d", &flag);

	} while (flag==1);
		
	return 0;
}

  

posted @ 2019-10-23 10:16  jason2018  阅读(121)  评论(0编辑  收藏  举报