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

}
相关推荐
失散1313 分钟前
Python——1 概述
开发语言·python
夏乌_Wx14 分钟前
练题100天——DAY23:存在重复元素Ⅰ Ⅱ+两数之和
数据结构·算法·leetcode
萧鼎15 分钟前
Python 图像哈希库 imagehash——从原理到实践
开发语言·python·哈希算法
小小8程序员31 分钟前
STL 库(C++ Standard Template Library)全面介绍
java·开发语言·c++
立志成为大牛的小牛32 分钟前
数据结构——五十六、排序的基本概念(王道408)
开发语言·数据结构·程序人生·算法
老王熬夜敲代码1 小时前
C++中的atomic
开发语言·c++·笔记·面试
沿着路走到底1 小时前
将数组倒序,不能采用reverse,算法复杂度最低
算法
IDIOT___IDIOT1 小时前
KNN and K-means 监督与非监督学习
学习·算法·kmeans
a努力。1 小时前
腾讯Java面试被问:String、StringBuffer、StringBuilder区别
java·开发语言·后端·面试·职场和发展·架构