C语言 解一元二次方程

C语言 解一元二次方程,第1张

# define _CRT_SECURE_NO_WARNINGS
# include 
# include 

int main(void)
{
	int a, b, c;
	double delta, x1, x2;
	char ch;

	do
	{
		printf("请输入一元二次方程的系数:\n");

		printf("a = ");
		scanf("%d", &a);

		printf("b = ");
		scanf("%d", &b);

		printf("c = ");
		scanf("%d", &c);

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

		if (delta > 0)
		{
			x1 = (-b + sqrt(delta)) / (2 * a);
			x2 = (-b - sqrt(delta)) / (2 * a);

			printf("该方程有两个不同的实根\nx1 = %lf\nx2 = %lf", x1, x2);
		}

		else if (delta == 0)
		{
			x1 = x2 = (-b) / (2 * a);

			printf("该方程有两个相同的实根\nx1 = x2 = %lf", x1);
		}

		else
		{
			printf("该方程无实数根");
		}

		printf("\n是否再来一次?(Y/N)");
		scanf(" %c", &ch);   //%c前一定要加空格!!!

	} while (ch == 'Y' || ch == 'y');
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1499310.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-25
下一篇 2022-06-25

随机推荐

发表评论

登录后才能评论

评论列表(0条)

保存