第七章:7.2求方程a*x*x+b*x+c=0的根,用3个函数,分别求当:b*b-4*a*c大于0、等于0和小于0时的根并输出结果。从主函数输入a、b、c的值

//求方程a*x*x+b*x+c=0的根,用3个函数,分别求当:b*b-4*a*c大于0、等于0和小于0时的根并输出结果。

//从主函数输入a、b、c的值

cs 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
void s1(float a, float b, float c)
{
	float x1, x2;
	float tmp = b * b - 4 * a * c;
	x1 = (-b + sqrt(tmp)) / 2 * a;
	x2 = (-b - sqrt(tmp)) / 2 * a;
	printf("x1=%.2f\nx2=%.2f\n", x1, x2);
}
void s2(float a, float b, float c)
{
	float x1, x2;
	float tmp = b * b - 4 * a * c;
	x1 = (-b ) / 2 * a;
	x2 = (-b ) / 2 * a;
	printf("x1=%.2f\nx2=%.2f\n", x1, x2);
}
void s3(float a, float b, float c)
{
	float x1, x2;
	float p, q;
	float tmp = b * b - 4 * a * c;
	p = (-b) / 2 * a;
	q = (sqrt(-tmp)) / 2 * a;
	printf("x1=%.2f+%.2fi\nx2=%.2f-%.2fi\n", p, q, p, q);
}
int main()
{
	float a = 0.0, b = 0.0, c = 0.0;
	printf("请输入a的值:");
	scanf("%f", &a);
	printf("请输入b的值:");
	scanf("%f", &b);
	printf("请输入c的值:");
	scanf("%f", &c);
	if (b * b - 4 * a * c > 0)
	{
		s1(a, b, c);
	}
	else if (b * b - 4 * a * c == 0) 
	{
		s2(a, b, c);
	}
	else
	{
		s3(a, b, c);
	}
	return 0;
}

输出结果:

1.当b*b-4*a*c=0时

2.当b*b-4*a*c<0时

3.当b*b-4*a*c>0时

相关推荐
LightYoungLee16 小时前
算法(五)树 Trees V2
学习·算法·深度优先
enmouhuadou16 小时前
什么是I/Q信号?
算法·信息与通信
寻星探路16 小时前
【Python 全栈测开之路】Python 进阶:库的使用与第三方生态(标准库+Pip+实战)
java·开发语言·c++·python·ai·c#·pip
2301_8002561117 小时前
第九章:空间网络模型(空间网络查询、数据模型、Connected、with Recursive、pgRouting)
网络·数据库·算法·postgresql·oracle
逑之18 小时前
C语言笔记10:sizeof和strlen,指针与数组
c语言·笔记·算法
求梦82018 小时前
【力扣hot100题】旋转图像(15)
算法·leetcode·职场和发展
C雨后彩虹1 天前
任务最优调度
java·数据结构·算法·华为·面试
SmartRadio1 天前
CH585M+MK8000、DW1000 (UWB)+W25Q16的低功耗室内定位设计
c语言·开发语言·uwb
kylezhao20191 天前
C#winform数据绑定
c#
rfidunion1 天前
QT5.7.0编译移植
开发语言·qt