因格式问题,截图上传。
26.func_pointer_act
/*
* 公司:XXXX
* 作者:Rston
* 博客:http://blog.csdn.net/rston
* GitHub:https://github.com/rston
* 项目:函数指针实战和typedef
* 功能:通过函数指针指向不同的函数实现多态,即同一个执行,得到不同的执行结果。
*/
#include <stdio.h>
// 函数声明
int add(int a, int b);
int sub(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);
// 定义函数指针类型pFunc
typedef int (*pFunc)(int, int);
int main(int argc, char **argv)
{
int a = 0, b = 0, result = 0;
char c = 0;
pFunc func = NULL; // 定义函数指针变量并赋值为NULL
printf("please input two arguments:\n");
scanf("%d %d", &a, &b);
printf("please input the symbol:\n");
do
{
scanf("%c", &c);
}while ('\n' == c); // 清除缓存区
switch (c) // 通过C决定func指向某个具体的函数
{
case '+':
func = add; break;
case '-':
func = sub; break;
case '*':
func = multiply; break;
case '/':
func = divide; break;
default:
func = NULL; break;
}
result = func(a, b); // 调用函数
printf("%d %c %d = %d.\n", a, c, b, result);
return 0;
}
int add(int a, int b)
{
return (a + b);
}
int sub(int a, int b)
{
return (a - b);
}
int multiply(int a, int b)
{
return (a * b);
}
int divide(int a, int b)
{
return (a / b);
}
26.calculator
framework.c
/*
* 公司:XXXX
* 作者:Rston
* 博客:http://blog.csdn.net/rston
* GitHub:https://github.com/rston
* 项目:函数指针实战和typedef
* 功能:结构体内嵌函数指针实现分层,此文件为上层,主要实现计算器业务逻辑。
*/
#include "framework.h"
// 实现计算器业务逻辑
int calculator(const struct cal_t *p)
{
return (p->func(p->a, p->b));
}
26.calculator
framework.h
#ifndef __FRAMEWORK_H__
#define __FRAMEWORK_H__
// 定义函数指针类型pFunc
typedef int (*pFunc)(int, int);
// 定义结构体类型struct cal_t
struct cal_t
{
int a; // 运算数据a
int b; // 运算数据b
pFunc func; // 具体函数的封装
};
// 计算器上层函数接口,被下层调用
int calculator(const struct cal_t *p);
#endif
26.calculator
cal.c
/*
* 公司:XXXX
* 作者:Rston
* 博客:http://blog.csdn.net/rston
* GitHub:https://github.com/rston
* 项目:函数指针实战和typedef
* 功能:结构体内嵌函数指针实现分层,此文件为下层,主要实现具体的计算器运算。
*/
#include <stdio.h>
#include "framework.h"
#include "cal.h"
int main(int argc, char **argv)
{
int ret = 0;
struct cal_t myCal; // 定义结构体变量
myCal.a = 14; // 给结构体变量赋值
myCal.b = 2;
myCal.func = multiply;
ret = calculator(&myCal); // 调用上层接口
printf("ret = %d.\n", ret);
return 0;
}
// 加法
int add(int a, int b)
{
return (a + b);
}
// 减法
int sub(int a, int b)
{
return (a - b);
}
// 乘法
int multiply(int a, int b)
{
return (a * b);
}
// 除法
int divide(int a, int b)
{
return (a / b);
}
26.calculator
cal.h
#ifndef __CAL_H__
#define __CAL_H__
// 具体的运算函数声明
int add(int a, int b);
int sub(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);
#endif
26.typedef
/*
* 公司:XXXX
* 作者:Rston
* 博客:http://blog.csdn.net/rston
* GitHub:https://github.com/rston
* 项目:函数指针实战和typedef
* 功能:演示typedef的各种用法。
*/
#include <stdio.h>
// 定义了两个类型:
// 1个为结构体类型,有2个名字:struct teacher和teacher
// 1个为结构体指针类型,有2个名字:struct teacher *和pTeacher。
typedef struct teacher
{
char name[20];
int age;
}teacher, *pTeacher;
// typedef与const结合
typedef int *PINT;
typedef int const *CPINT;
int main(int argc, char **argv)
{
teacher t1 = // 定义结构体变量
{
.name = "Rston",
.age = 25,
};
pTeacher p = &t1; // 定义结构体指针变量
// p->name = Rston. p->age = 25.
printf("p->name = %s. p->age = %d.\n", p->name, p->age);
int a = 5, b = 6;
const PINT p1 = &a;
*p1 = 10;
printf("*p1 = %d.\n", *p1); // *p1 = 10.
// p1 = &b; // error: assignment of read-only variable ‘p1’
const PINT p2 = &a;
*p2 = 15;
printf("*p2 = %d.\n", *p2); // *p2 = 15.
//p2 = &b; // error: assignment of read-only variable ‘p2’
CPINT p3 = &a;
//*p3 = 18; // error: assignment of read-only location ‘*p3’
printf("*p3 = %d.\n", *p3); // *p3 = 15.
p3 = &b;
return 0;
}