您的位置:首页 > 编程语言 > C语言/C++

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐