回调函数

  有点类似模板的功能,可以使用函数指针作为参数,当调用函数时,使用void *进行传递参数,细致比较时,再用int *之类的进行强制转换。回调函数,其实就是在参数中定义函数,调用时,回到主函数去调用这个函数。仔细用法如下:

首先定义查找函数

Node * search_list(Node *node, void const *value, int (*compare)(void const *,void const *)){
    while(node!=NULL){
        if(compare(&node->data,value) == 0 )
            break;
        node = node->next;
    }
    return node;
}

比较函数

int compare_ints(void const *a,void const *b){
    if( *(int *)a == *(int *)b)
        return 0;
    else
        return 1;
}

函数中的调用

 

    int *desire = (int *)malloc(sizeof(int));
    *desire = 3;
    Node *n1 = (Node *)malloc(sizeof(Node)); 
    n1 = search_list(L->next,desire,compare_ints);
    if(n1!=NULL)
        printf("找到了%d",n1->data);

 

全部代码

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct Node{
  5     int data;
  6     struct Node * next;
  7 }Node;
  8 
  9 void createList(Node * L,int len);
 10 void showList(Node *L);
 11 void clearList(Node *L);
 12 void getNode(Node *L,int n,Node *tar);
 13 int insertNode(Node *L,int n,int num);
 14 int deleteNode(Node *L,int n);
 15 Node * search_list(Node *node, void const *value, int (*compare)(void const *,void const *));
 16 int compare_ints(void const *a,void const *b);
 17 
 18 int main()
 19 {
 20     Node *L= (Node *)malloc(sizeof(Node));
 21 
 22     createList(L,5);
 23     showList(L);
 24 
 25     Node *tar= (Node *)malloc(sizeof(Node));
 26     getNode(L,3,tar);
 27     printf("the third is:%d\n",tar->data);
 28     
 29     if(insertNode(L,3,0))
 30         showList(L);
 31     
 32     printf("回调函数:\n");
 33     int *desire = (int *)malloc(sizeof(int));
 34     *desire = 3;
 35     Node *n1 = (Node *)malloc(sizeof(Node)); 
 36     n1 = search_list(L->next,desire,compare_ints);
 37     if(n1!=NULL)
 38         printf("找到了%d",n1->data);
 39 
 40     /*
 41     if(deleteNode(L,3))
 42         showList(L);
 43 
 44     clearList(L);
 45     showList(L);
 46     */
 47 
 48     /**desire = 9;
 49     Node *n2 = search_list(L,desire,compare_ints);
 50     if(n2!=NULL)
 51         printf("找到了%d",n2->data);
 52     *desire = 7;
 53     Node *n3 = search_list(L,desire,compare_ints);
 54     if(n3!=NULL)
 55         printf("找到了%d",n3->data);*/
 56     getchar();
 57     return 0;
 58 }
 59 Node * search_list(Node *node, void const *value, int (*compare)(void const *,void const *)){
 60     while(node!=NULL){
 61         if(compare(&node->data,value) == 0 )
 62             break;
 63         node = node->next;
 64     }
 65     return node;
 66 }
 67 int compare_ints(void const *a,void const *b){
 68     if( *(int *)a == *(int *)b)
 69         return 0;
 70     else
 71         return 1;
 72 }
 73 
 74 void createList(Node * L,int len){
 75     int i;
 76     Node * p;
 77     L->next = NULL;
 78     for(i=0;i<len;i++){
 79         p = (Node *)malloc(sizeof(Node));
 80         p->data = 2*i+1;
 81         p->next = L->next;
 82         L->next = p;
 83     }
 84 }
 85 
 86 void showList(Node *L){
 87     Node *p = (Node *)malloc(sizeof(Node));
 88     p=L->next;
 89     while(p){
 90         printf("%d->",p->data);
 91         p=p->next;
 92     }
 93     printf("null\n");
 94     free(p);
 95 }
 96 
 97 void clearList(Node *L){
 98     Node *p,*q;
 99     p=L->next;
100     while(p){
101         q=p->next;
102         free(p);
103         p=q;
104     }
105     L->next=NULL;
106 }
107 
108 void getNode(Node *L,int n,Node *tar){
109     int i=1;
110     Node *p;
111     p=L->next;
112     while(p && i<n){
113         p=p->next;
114         i++;
115     }
116     if(!p || i>n)
117         printf("error!");
118     tar->data=p->data;
119 }
120 
121 int insertNode(Node *L,int n,int num){
122     int i=1;
123     Node *p = L->next;
124     while( p && i<n-1){
125         p=p->next;
126         i++;
127     }
128     if(!p || i>n-1)
129         return 0;
130     Node *q = (Node *)malloc(sizeof(Node));
131     q->data = num;
132     q->next = p->next;
133     p->next = q;
134     return 1;
135 }
136 
137 int deleteNode(Node *L,int n){
138     int i=1;
139     Node *p = L->next;
140     Node *q;
141     while( p->next && i<n-1){
142         p=p->next;
143         i++;
144     }
145     if( !(p->next) || i>n-1)
146         return 0;
147     q=p->next;
148     p->next = q->next;
149     free(q);
150     return 1;
151 }
View Code

 

运行示例

1. 用户与身体信息管理模块 用户信息管理: 注册登录:支持手机号 / 邮箱注册,密码加密存储,提供第三方快捷登录(模拟) 个人资料:记录基本信息(姓名、年龄、性别、身高、体重、职业) 健康目标:用户设置目标(如 “减重 5kg”“增肌”“维持健康”)及期望周期 身体状态跟踪: 体重记录:定期录入体重数据,生成体重变化曲线(折线图) 身体指标:记录 BMI(自动计算)、体脂率(可选)、基础代谢率(根据身高体重估算) 健康状况:用户可填写特殊情况(如糖尿病、过敏食物、素食偏好),系统据此调整推荐 2. 膳食记录与食物数据库模块 食物数据库: 基础信息:包含常见食物(如米饭、鸡蛋、牛肉)的名称、类别(主食 / 肉类 / 蔬菜等)、每份重量 营养成分:记录每 100g 食物的热量(kcal)、蛋白质、脂肪、碳水化合物、维生素、矿物质含量 数据库维护:管理员可添加新食物、更新营养数据,支持按名称 / 类别检索 膳食记录功能: 快速记录:用户选择食物、输入食用量(克 / 份),系统自动计算摄入的营养成分 餐次分类:按早餐 / 午餐 / 晚餐 / 加餐分类记录,支持上传餐食照片(可选) 批量操作:提供常见套餐模板(如 “三明治 + 牛奶”),一键添加到记录 历史记录:按日期查看过往膳食记录,支持编辑 / 删除错误记录 3. 营养分析模块 每日营养摄入分析: 核心指标计算:统计当日摄入的总热量、蛋白质 / 脂肪 / 碳水化合物占比(按每日推荐量对比) 微量营养素分析:检查维生素(如维生素 C、钙、铁)的摄入是否达标 平衡评估:生成 “营养平衡度” 评分(0-100 分),指出摄入过剩或不足的营养素 趋势分析: 周 / 月营养趋势:用折线图展示近 7 天 / 30 天的热量、三大营养素摄入变化 对比分析:将实际摄入与推荐量对比(如 “蛋白质摄入仅达到推荐量的 70%”) 目标达成率:针对健
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值