C语言基础-qsort/isspace/isdigit函数的用法和实现
2017-09-01 22:52
381 查看
序言
由于没有仔细研究过C语言库函数,很多函数都变成了自己实现了,这样必定会限制开发的速度。比如C语言自带的排序函数qsort()就能实现快排,只需要自己写一个简单的比较函数即可使用。
1. qsort()函数
函数原型:void qsort(void * buf, size_t num, size_t size, int (*compare)(const void *, const void *));buf:参与排序的数组首地址
num:数组元素个数
size:数组元素的大小(推荐使用sizeof(array[0])这样的表达)
compare:比较函数,需要自己单独写。
功能:qsort()函数根据给的比较函数进行快速排序,通过指针移动实现排序,排序的结果仍存在原数组中。
头文件:stdlib.h
返回值:无返回值
关于比较函数的用法:int compare(const void *value1, const void *value2);
返回值必须是int
参数必须是const void *:所以需要对value1和value2两个参数进行强制类型转换才能得到正确的返回值
升降序排序:
如果value1大于value2返回正值,为升序排序
如果value1大于value2返回负值,为降序排序
[1] int/char/double类型排序
int compare(const void *value1, const void *value2) { return *(int *)value1 - *(int *)value2; //这样是升序排序 return *(int *)value2 - *(int *)value1; //这样是降序排序 return *(char *)value1 - *(char *)value2; } 或 int compare(const void *value1, const void *value2) { return *(int *)value1 - *(int *)value2 > 0 ? 1 : -1; //这样是升序排序 return *(int *)value1 - *(int *)value2 > 0 ? -1 : 1; //这样是降序排序 }
[2] 结构体类型排序struct node{}
//以链表形式节点为例 struct list_node { int data; struct list_node *next; }Node; //此时比较函数可以写成 int compare(const void *value1, const void *value2) { return (Node *)value1 -> data - (Node *)value2 -> data; } 或 int compare(const void *value1, const void *value2) { struct Node *a = (Node *)value1; struct Node *b = (Node *)value2; return (a -> data) - (b -> data) > 0 ? 1 : -1; } //对于结构体二级排序 struct list_node { int data; int num; }Node; //如果data值相等,可以利用num来进行顺序排序 int compare(const void *value1, const void *value2) { Node *a = (Node *)value1; Node *b = (Node *)value2; if (a -> data != b -> data) return (a -> data) - (b -> data) > 0 ? 1 : -1; else return (a -> num) - (b -> num) > 0 ? 1 : -1; } //如果结构体有字符串的话,即结构体如下 struct list_node { int data; int num; char array[10] = "abcdef123"; }Node; int compare(const void *value1, const void *value2) { Node *a = (Node *)value1; Node *b = (Node *)value2; if (a -> data != b -> data) return (a -> data) - (b -> data); else return (a -> num) - (b -> num); else return strcmp(a->array, b->array); //按照结构体中字典顺序排序 }
[3] 字符串数组类型排序
char *array[10]; int compare(const void *value1, const void *value2) { return strcmp(*(char **)value1, *(char **)value2); }
qsort()函数应用举例:
#include <stdio.h> #include <stdlib.h> //for qsort() //参数1 - 参数2,非降序排序;反之,非升序排序 int compare(const void *value1, const void *value2) { return *(int *)value1 - *(int *)value2; } int main() { int array[] = {3, 2, 1, 4, 6, 5, 7, 8, 9, 0}; int len = sizeof(array) / sizeof(int); //int strlen(char *string); qsort(array, len, sizeof(int), compare); int i = 0; for (; i < len; i++) { printf("%d", array[i]); } return 0; }
2. isspace()函数
函数原型:int isspace(int c);头文件:ctype.h
功能:判断字符是否为空格/回车/制表符等
返回值:所以函数类型是int
成功:如果获取到的字符是空格/回车/制表符,返回非0值(为真)
失败:不是,返回0
使用举例:
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char array[] = "h ello,w orld"; char *p = array; int len = strlen(p); while (len > 0) { if (!isspace(*p)) //不是空格返回0,都输出 printf("%c\n", *p); p++; len--; } return 0; }
自己实现myisspace()函数:
int myisspace(char c) { if (c == ' ' || c == '\r' || c == '\t') return 1; else return 0; }
3. isdigit()函数
函数原型:int isdigit(int c);头文件:ctype.h
功能:判断字符c是否为数字
返回值:
成功:是数字,返回非0值,true,为真
失败:不是数字,返回0
应用举例:
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char array[] = "abc123#!456def"; char *p = array; int len = strlen(p); while (len > 0) { if (isdigit(*p)) //输出数字字符 printf("%c\n", *p); p++; len--; } return 0; }
Acknowledgements:
http://www.cnblogs.com/sooner/archive/2012/04/18/2455011.html
http://www.cnblogs.com/Bob-tong/p/6610647.html
2017.09.01
相关文章推荐
- 回调函数,回调函数应用,qsort函数用法,以及模拟实现qsort函数
- Linux-C基础知识学习:C语言作业-将5个学生成绩保存在一个数组中,单独实现一个计算平均成绩的average函数, 在main函数中获取该函数返回的平均值,并打印。
- 【C语言基础】九九乘法表(实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定)
- 通过对PHP语言的学习,应该知道它是基于函数的一款HTML脚本语言。庞大的函数库支持着PHP语言功能的实现。下面我们为大家介绍有关PHP函数isset()与empty()的相关用法。
- MmMapIoSpace以及MmUnmapIoSpace,VirtualAlloc和VirtualCopy 函数的实现原理以及实现方法
- 元语言基础技术之:在JS中如何自由地创建函数
- 元语言基础技术之:在JS中如何自由地创建函数
- 函数qsort()和bsearch()的用法
- mips汇编语言之实现swap函数
- D语言游戏编程(10):D语言基础之函数和操作符重载
- 深入浅出Dll(介绍函数导出、类导出、钓子dll、不同语言混合编程方法、插件等的实现方法)
- C++ 排序函数 sort(),qsort()的用法
- 深入浅出Dll(介绍函数导出、类导出、钓子dll、不同语言混合编程方法、插件等的实现方法)
- F#程序设计-F#语言基础之函数(2)
- 深入浅出Dll(介绍函数导出、类导出、钓子dll、不同语言混合编程方法、插件等的实现方法)
- 有关VA_LIST的用法--变参函数的实现
- C语言基础教程(五)函数
- C语言实现参数个数可变的函数(转)
- 深入浅出Dll(介绍函数导出、类导出、钓子dll、不同语言混合编程方法、插件等的实现方法)
- MmMapIoSpace以及MmUnmapIoSpace,VirtualAlloc和VirtualCopy 函数的实现原理以及实现方法