(函数)求一元二次方程的根(C语言)

一、运行结果;

二、源代码;

cs 复制代码
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <math.h>

//声明函数;
//判断条件等于0时;
void zeor(double a, double b);

//判断条件大于0时;
void bigzeor(double p, double q);

//判断条件小于0时;
void smallzeor();

int main()
{
	//初始化变量值;
	double a, b, c, p, q, judge;

	//获取用户输入数据;
	printf("请输入a, b, c的值:");
	scanf("%lf %lf %lf", &a, &b, &c);

	//运算;
	judge = b * b - 4 * a * c;
	p = -b / (2.0 * a);
	q = sqrt(judge) / (2.0 * a);

	//判断;
	if (judge >= 0)
	{
		if (judge = 0)
		{
			//调用函数;
			zeor(a, b);
		}
		else
		{
			//调用函数;
			bigzeor(p, q);
		}
	}
	else
	{
		//调用函数;
		smallzeor();
	}

	return 0;
}

//实现zeor函数;
void zeor(double a, double b)
{
	//初始换变量值;
	double x1 = 0;
	double x2 = 0;

	//运算;
	x1 = x2 = (-2.0 * a) / b;

	//输出结果;
	printf("函数的根为:x1=x2=%.2f\n", x1);
	
}

//实现bigzeor函数;
void bigzeor(double p, double q)
{
	//初始换变量值;
	double x1 = 0;
	double x2 = 0;

	//运算;
	x1 = p + q;
	x2 = p - q;

	//输出结果;
	printf("函数的根为:x1=%.2f, x2=%.2f\n", x1, x2);

}

//实现smallzeor函数;
void smallzeor()
{
	//输出结果;
	printf("函数无根!\n");

}
相关推荐
MATLAB代码顾问2 分钟前
Simulink PI 参数扫描与批量仿真完整 MATLAB 代码
开发语言·matlab
Want59518 分钟前
Python跳动的爱心②
开发语言·python·数学建模
Navigator_Z1 小时前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode
小羊没烦恼!1 小时前
T-SQL查询进阶--理解SQL SERVER中的分区表
服务器·开发语言·前端·javascript·html
Nil2081 小时前
leetcode 64最小路径和
算法·leetcode·职场和发展
光影少年1 小时前
AI Agent、自动化与未来工作
人工智能·算法·架构
爱吃香菜的初学者1 小时前
十六.Linux——多线程(2)
linux·运维·开发语言
牧羊人.3332 小时前
计算机视觉基础 第16章|EigenFace,FisherFace,LBPH传统人脸识别算法
人工智能·算法·计算机视觉
jeo陽2 小时前
04-循环实战:素数、最大公约数、级数求和与枚举法
c语言·开发语言·数据结构
Nil2082 小时前
leetcode 72编辑距离
算法·leetcode·职场和发展