C11-函數指針

一、函數指針定義
二、函數回調
三、動態排序
四、函數返回值是函數指針

代碼:
int maxValue(int a, int b)
{
return a > b ? a : b;
}

void printHello(void)
{
printf(“hello !\n”);
}

int sum(int a, int b)
{
return a + b;
}

// 給此函數指針類型起名爲FN

typedef int (*FN)(int a, int b);

typedef void (*VO)(void);

int getVal(int a, int b, FN p)
{
return p(a, b);
}
typedef struct student {
char name[20];
int num;
float score;
} Student;

//typedef void (*v)(Student *stu, int count);
//
//void getValu(Student *stu, int count, v q)
//{
// for (int i = 0; i < count; i++) {
// if (stu[i].score >= 90) {
// printf(“%s高富帥\n”, stu[i].name);
// }
// }
//}

// 回調函數!!!
// 女 + 白富美

void catNameM(char *p)
{
strcat(p, “高富帥”);
}
// 男 + 高富帥
void catNameF(char *p)
{
strcat(p, “白富美”);
}
typedef void (*C)(char *p);
// 分數大於90,名字後加 catname
void addName(Student *stu, int count, C name)
{
for (int i = 0; i < count; i++) {
if (stu[i].score >= 90) {
name(stu[i].name);
}
}
}
// 打印
void printName(Student *stu, int count)
{
for (int i = 0; i < count ; i++) {
printf(“%s %d %.2f\n”, stu[i].name, stu[i].num, stu[i].score);
}
}

typedef struct student1 {
char name[20];
int num;
float score;
} Student1;
void arrNum(Student1 *stu, int count)
{
//for (int i; i < count; i++) {
while (stu[count].num > stu[count - 1].num) {
if (count < 0) {
break;
}
Student1 temp = stu[count];
stu[count] = stu[count - 1];
stu[count - 1] = temp;
}
//}
}
void arrName(Student1 *stu, int count)
{
//for (int i = 0; i < count; i++) {
while (strcmp(stu[count].name, stu[count - 1].name) > 0) {
if (count < 0) {
break;
}
Student1 temp = stu[count];
stu[count] = stu[count - 1];
stu[count - 1] = temp;
}
//}
}
void arrScore(Student1 *stu, int count)
{
//for (int i = 0; i < count; i++) {
while (stu[count].score > stu[count - 1].score) {
if (count < 0) {
break;
}
Student1 temp = stu[count];
stu[count] = stu[count + 1];
stu[count + 1] = temp;
}
//}
}
typedef void (*arr)(Student1 *stu, int count);
void arrStudent(Student1 *stu, int count, arr z)
{
char a[10] = {0};
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
scanf(“%s”, a);
}
}
}

int opeSum(int a, int b)
{
return a + b;
}

int opeSub(int a, int b)
{
return a - b;
}
int opeMul(int a, int b)
{
return a * b;
}
int opeDiv(int a, int b)
{
return a / b;
}
typedef int (*ariOpe)(int a, int b);
int getValu(int a, int b, ariOpe ao)
{

return ao(a, b);

}

//void ariOpe(, )
//{
//
//}

int main(int argc, const char * argv[]) {

// int ret = p(3, 5);
// printf(“%d\n”, ret);

// 定義一個函數指針
// 函數指針->即函數類型的指針
// 定義函數指針需先有函數的定義

// FN p = NULL;
// p = maxValue;
// int retP = p(3, 5);
// printf(“%d\n”, retP);

// VO p = NULL;
// p = printHello;
// p();

// 練習2.

// char fun[10] = {0};
// //int (*p)(int a, int b) = NULL;
// FN p = NULL;
// scanf(“%s”, fun);
// // 判斷
// if ((strcmp(fun, “max”) != 0) && (strcmp(fun, “sum”) != 0)) {
// // 報錯
// printf(“error\n”);
// // 返回錯誤碼
// return -1;
// }
// if (0 == strcmp(fun, “max”)) {
// p = maxValue;
// }
// if (0 == strcmp(fun, “sum”)) {
// p = sum;
// }
// // int ret = p(3, 5);
// int ret = getVal(3, 5, p);
// printf(“%d\n”, ret);

// Student stu[5] = {
// {“yi”, 1, 60},
// {“er”, 2, 70},
// {“san”, 3, 80},
// {“si”, 4, 90},
// {“wu”, 5, 100}
// };
// int count = sizeof(stu) / sizeof(stu[0]);
// addName(stu, count, catNameM);
// printName(stu, count);

// Student1 stu[5] = {
// {“yi”, 3, 58},
// {“er”, 4, 67},
// {“san”, 1, 45},
// {“si”, 2, 23},
// {“wu”, 5, 98}
// };
// int count = sizeof(stu) / sizeof(stu[0]);

char fun[20] = {0};
int a = 0, b = 0;
//int (*p)(int a, int b) = NULL;
ariOpe p = NULL;
printf("請輸入(eg:sum 3 5):\n");
scanf("%s%d%d", fun, &a, &b);
// 判斷
if ((strcmp(fun, "sum") != 0) && (strcmp(fun, "sub") != 0) && (strcmp(fun, "mul") != 0) && (strcmp(fun, "div") != 0)) {
    // 報錯
    printf("error\n");
    // 返回錯誤碼
    return -1;
}
if (0 == strcmp(fun, "sub")) {
    p = opeSub;
}
if (0 == strcmp(fun, "sum")) {
    p = opeSum;
}
if (0 == strcmp(fun, "mul")) {
    p = opeMul;
}
if (0 == strcmp(fun, "div")) {
    p = opeDiv;
}
// int ret = p(3, 5);
int ret = getVal(a, b, p);
printf("%d\n", ret);

return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章