(函数)求一元二次方程的根(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");

}
相关推荐
jerryinwuhan5 小时前
基于各城市站点流量的复合功能比较
开发语言·php
浅念-5 小时前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
Kiling_07045 小时前
Java集合进阶:Set与Collections详解
算法·哈希算法
迈巴赫车主6 小时前
Java基础:list、set、map一遍过
java·开发语言
智者知已应修善业6 小时前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
洛水水6 小时前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展
SimpleLearingAI6 小时前
聚类算法详解
算法·数据挖掘·聚类
南 阳7 小时前
Python从入门到精通day66
开发语言·python
刀法如飞7 小时前
Go 字符串查找的 20 种实现方式,用不同思路解决问题
算法·面试·程序员
十八旬8 小时前
快速安装ClaudeCode完整指南
开发语言·windows·python·claude