网站被恶意解析/seo关键词优化软件app
Linux内核都是这样分层实现的,所以现在先学学分层的思想。
用分层的思想实现一个计算器。
由三个文件组成:cal.c,cal.h和framework.c
cal.h 是头文件,一定要有如下开头和结尾。
#ifndef __CAL_H__
#define __CAL_H__#endif
该文件中主要做声明。
framework.c实现了cal.h业务逻辑代码。
cal.c 上边好比定义好了模具,在这个文件中自定义生产原材料。
cal.h
#ifndef __CAL_H__
#define __CAL_H__typedef int (*pFunc)(int a,int b);//封装一个结构体,计算的工具和原材料
struct cal_t
{int a;int b;pFunc p;
};// 函数原型声明
int calculator(const struct cal_t *p);#endif
framework.c
#include "cal.h"// framework.c中应该写实际业务关联的代码// 计算器函数
int calculator(const struct cal_t *p)
{return p->p(p->a,p->b);
}
cal.c
#include <stdio.h>#include "cal.h"int divide(int a,int b)
{return a/b;
}int main()
{int ret = 0;struct cal_t myCal;myCal.a = 12;myCal.b = 4;myCal.p = divide;ret = calculator(&myCal);printf("ret = %d.\n",ret);return 1;
}
理解:cal.h好比定义了一个模具的尺寸,规格, 做此磨具要有的功能。
framework.c