多文件编程

1多文件编程

针对c语言 实现模块化编程 声明文件.h .c文件对声明文件.h定义 后使用.c文件做验证 例题为求解一元二次方程的模块设计。
.h声明接口函数文件 声明的每个函数 在.c实现文件中定义功能

1.1 .h声明文件

#ifndef yyec_h
#define yyec_h
extern float qiugen0(float a, float b, float c, float x0);
extern void getRoots(float a, float b, float c, float* x1, float* x2);
extern float duichen(float a, float b, float dcz);
extern float zuizhi(float a, float b, float c, float max);
#endif

1.2 .c 功能实现

.h文件对所有接口函数做出声明 其功能实现在.c文件里
#include <stdio.h>
#include <math.h>
#include “yyec.h”//对于定义函数接口的引用 要用""
float qiugen0(float a, float b, float c, float x0) {
//求根 x0 = (-b) / (2 * a); return x0;
}
void getRoots(float a, float b, float c, float* x1, float* x2)
{ *x1 = (-b + sqrt((b * b - 4 * a * c)) / (2 * a)); *x2 = (-b - sqrt((b * b - 4 * a * c)) / (2 * a));}
float duichen(float a, float b, float dcz)
{ dcz = (-b) / (2 * a);
return dcz;}
float zuizhi(float a, float b, float c, float max) //求最值{ max = (((4 * a * c) - (b * b)) / (4 * a)); return max;}

1.3 验证文件

验证.c文件
#include <stdio.h>
#include “yyec.h”
int main()
{
float a, b, c;
float x0 = 0;
float x1 = 0;
float x2 = 0;
float dcz = 0;
float max = 0;
printf(“请依次输入a,b,c 中间空格分开 回车运行\n”);
scanf_s("%f %f %f", &a, &b, &c);
if (b * b - 4 * a * c == 0)
{
printf(“此时方程根为%f\n”, qiugen1(a, b, c, x0)); }
else if (b * b - 4 * a * c > 0)
{
printf("此时方程的根为 %f %f \n ", qiugen1(a, b, c, x1), qiugen2(a, b, c, x2)); } else {
printf(“无解\n”);
}
printf(“对称轴为: %f\n”, duichen(a, b, dcz));
if(a>0)
{ printf(“最小值是%f\n”, zuizhi(a, b, c, max)); }
else
{ printf(“最大值是%f\n”, zuizhi(a, b, c, max));
}
}

可输入值进行测试
编译环境为vs2019

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李时珍的瓜皮怪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值