C语言基础-qsort/isspace/isdigit函数的用法和实现
来源:互联网 发布:mac usr include 编辑:程序博客网 时间:2024/06/08 13:47
序言
由于没有仔细研究过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
阅读全文
0 0
- C语言基础-qsort/isspace/isdigit函数的用法和实现
- 【c++语言基础】isdigit函数和putback的一些用法
- C++/C语言函数:ispunct()和isspace()
- C语言字符检测函数:isalnum、isalpha、isascii、iscntrl、isdigit、isgraph、islower、isspace、ispunct、isupper
- C语言qsort函数的用法
- C语言qsort的用法和使用
- c语言函数--stat、isspace
- C语言qsort()函数的实现
- isdigit(char c)函数用法
- C语言qsort的用法
- C语言qsort的用法
- C语言qsort的用法
- C语言qsort的用法
- C语言qsort的用法
- C语言关于qsort函数的用法详细说明
- C语言中qsort函数做排序的用法
- C语言里的快排函数用法(qsort)
- C语言的qsort函数
- table表格中,将边框合并成单线条的边框
- 【持续更新】机器学习常见面试题
- 猫都能学会的Unity3D Shader入门指南(一)
- 谢旭升 《操作系统教程》 课后习题答案 word版下载
- aaa
- C语言基础-qsort/isspace/isdigit函数的用法和实现
- Maven学习(八)- 构建MyBatis项目
- poj1208 The Blocks Problem(模拟)
- 【九度OJ】1191:矩阵最大值
- GIT和SVN的区别
- 关于函数作为参数传递给函数
- kde 崩溃,如何重启 kde plasma 5 桌面
- java中的线程及interrupt操作后状态的变化
- inflater用法