企业网站推广的首选办法是,郑州网站推广优化,公司名称变更说明函,建设企业网站的需要多长时间目录
1.回调函数
2. qsort 函数的使用 2.1 排序整型数据
2.2 排序结构体数据
3. qsort 函数的模拟实现 1.回调函数
回调函数就是通过一个函数指针调用的函数。
你把函数的地址作为参数传递给另一个函数#xff0c;当这个指针被用来调用其所指向的函数时#xff0c;被调…目录
1.回调函数
2. qsort 函数的使用 2.1 排序整型数据
2.2 排序结构体数据
3. qsort 函数的模拟实现 1.回调函数
回调函数就是通过一个函数指针调用的函数。
你把函数的地址作为参数传递给另一个函数当这个指针被用来调用其所指向的函数时被调用的函数就是回调函数。该函数不是自己直接调用自己而是在特点的事件或条件发生时由另外的⼀⽅调⽤的⽤于对该事件或条件进行响应。
回调函数使用条件 这些函数的的函数类型都基本一致只是函数内容上有差距。
#include stdio.h
int add(int a, int b)
{return a b;
}
int sub(int a, int b)
{return a - b;
}
int mul(int a, int b)
{return a * b;
}
int div(int a, int b)
{return a / b;
}
void calc(int(*pf)(int, int))//回调函数接收函数的地址
{int ret 0;int x, y;printf(输入操作数);scanf(%d %d, x, y);ret pf(x, y);printf(ret %d\n, ret);
}
int main()
{int input 0;do{printf(*************************\n);printf( 1:add 2:sub \n);printf( 3:mul 4:div \n);printf( 0:exit \n);printf(*************************\n);printf(请选择);scanf(%d, input);switch (input){case 1:calc(add);break;case 2:calc(sub);break;case 3:calc(mul);break;case 4:calc(div);break;case 0:printf(退出程序\n);break;default:printf(选择错误\n);break;}} while (input);return 0;
}
2. qsort 函数的使用
qsort是库函数这个函数可以完成任意类型数据的排序。使用时包含头文件stdlib.h
void qsort(void*base,//base指向了要排序的数组的第一个元素size_t num,//base指向的数组中的元素个数待排序的数组的元素的个数size_t size,//base指向的数组中元素的大小单位是字节int(*compar)(const void* p1,const void*p2)//函数指针——指针指向的函数是用来比较数组中的两个元素的。
); 2.1 排序整型数据
#include stdio.h
#includestdlib.h
//qsort函数的使⽤者得实现⼀个比较函数
int int_cmp(const void* p1, const void* p2)
{return (*(int*)p1 - *(int*)p2);
}
void print(int* arr,int sz)
{for (int i 0; i sz; i){printf(%d , arr[i]);}printf(\n);
}
int main()
{int arr[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };int sz sizeof(arr) / sizeof(arr[0]);qsort(arr,sz , sizeof(arr[0]), int_cmp);//1.数组的第一个元素2.数组的长度数组的第一个元素的大小比较函数接收返回值print(arr,sz);return 0;
}
2.2 排序结构体数据
struct str
{char name[20];int eag;
};
//怎么比较两个结构体数据--不能直接使用比较
//1.可以按照名字比较
//2.可以按照年龄比较//按照年龄比较
int cmp1(const void* p1, const void* p2)
{return ((struct str*)p1)-eag - ((struct str*)p2)-eag;
}
void test1()
{struct str arr[] { {zhangsan,50},{lisi,60},{laowang,90} };int sz sizeof(arr) / sizeof(arr[0]);qsort(arr, sz, sizeof(arr[0]), cmp1);
}
//按照名字比较
//注意两个字符串不能使用比较
//而是使用库函数strcmp来比较的
int cmp2(const void* p1, const void* p2)
{return strcmp(((struct str*)p1)-name, ((struct str*)p2)-name);
}
void test2()
{struct str arr[] { {zhangsan,50},{lisi,60},{laowang,90} };int sz sizeof(arr) / sizeof(arr[0]);qsort(arr, sz, sizeof(arr[0]), cmp2);
}int main()
{test1();test2();printf(\n);return 0;
}
3. qsort 函数的模拟实现
使⽤回调函数模拟实现qsort采⽤冒泡的⽅式。
int int_cmp(const void* p1, const void* p2)
{return (*(int*)p1 - *(int*)p2);
}
void swap(void* p1, void* p2, int size)
{int i 0;for (i 0; i size; i){char tmp *((char*)p1 i);*((char*)p1 i) *((char*)p2 i);*((char*)p2 i) tmp;}
}
void bubble(void* base, int count, int size, int(*cmp)(const void*,const void*))
{int i 0;int j 0;for (i 0; i count - 1; i){for (j 0; j count - i - 1; j){if (cmp((char*)base j * size, (char*)base (j 1) * size) 0){swap((char*)base j * size, (char*)base (j 1) * size, size);}}}
}
int main()
{int arr[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };int i 0;bubble(arr, sizeof(arr) / sizeof(arr[0]), sizeof(int), int_cmp);for (i 0; i sizeof(arr) / sizeof(arr[0]); i){printf(%d , arr[i]);}printf(\n);return 0;
} 文章转载自: http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.zypnt.cn.gov.cn.zypnt.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.kqqk.cn.gov.cn.kqqk.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.lxhny.cn.gov.cn.lxhny.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.ndpwg.cn.gov.cn.ndpwg.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.gfjgq.cn.gov.cn.gfjgq.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.ssgqc.cn.gov.cn.ssgqc.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.wljzr.cn.gov.cn.wljzr.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.skcmt.cn.gov.cn.skcmt.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.tbplf.cn.gov.cn.tbplf.cn