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

}
相关推荐
大可不必了5 小时前
03 函数定义和调用
开发语言·python
I_belong_to_jesus5 小时前
C++关于分支预测的优化1
开发语言·c++·算法
承渊政道6 小时前
【Python编程—从入门到实践】(记分与闯关:用Python+Pygame,打造越战越精彩的太空大战!)
开发语言·python·pycharm·游戏程序·pygame
爱学习的程序媛6 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
eggwyw8 小时前
Anaconda安装卸载的详细流程指南
开发语言·python
another heaven12 小时前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
Code_Solitude12 小时前
C语言动态内存 + 柔性数组 + 数组名退化
c语言·算法·柔性数组
上海潭龙东海科技有限公司12 小时前
客运站管控之动态孪生技术方案(新增一屏管控模块
数据仓库·人工智能·算法·重构·动态规划
SomeOtherTime13 小时前
Rust 闭包语法和原理(AI回答)
开发语言·后端·rust