利用C语言模拟实现qsort,strcpy,strcat,strcmp函数

发布时间:

这篇文章主要为大家详细介绍了如何通过C语言模拟实现qsort(采用冒泡的方式),strcpy,strcat,strcmp等函数,文中的示例代码讲解详细,感兴趣的可以了解一下!

利用C语言模拟实现qsort,strcpy,strcat,strcmp函数

1.采用冒泡的方式模拟实现qsort

简述回调函数:

回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。

模拟实现qsort函数源代码(采用冒泡的方式):

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void swap(void* p1, void* p2, int n)
{
	for (int i = 0; i < n; ++i)
	{
		char tmp = *((char*)p1 + i);
		*((char*)p1 + i) = *((char*)p2 + i);
		*((char*)p2 + i) = tmp;
	}
}
int cmp(const void* elem1, const void* elem2)
{
	return (*((int*)elem1) - *((int*)elem2));
}
void Bubble(void* base, int count, int size, int(*cmp)(void*, 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);
		}
	}
}


void  PrintArray(int ar[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", ar[i]);
	}
	printf("\n");
}
void main()
{
	int ar[10] = { 1,3,4,6,2,7,9,8,22,11 };
	int sz = sizeof(ar) / sizeof(ar[0]);
	PrintArray(ar, sz);
	Bubble(ar, sz, sizeof(ar[0]), cmp);
	PrintArray(ar, sz);
}

2.模拟实现strcpy函数规定

  • 源字符串必须以 ‘\0’ 结束。
  • 源字符串中的 ‘\0’ 也将会拷贝到。
  • 目标空间必须足够大,以确保能存放源字符串。
  • 目标空间必须可变。

源代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
char* my_strcpy(char* strDestination, const char* strSource)
{
	//要判断参数的有效性
	assert(strDestination != NULL && strSource != NULL);
	//参数保护
	char* pDest = strDestination;
	while (*strSource != '
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
char* my_strcpy(char* strDestination, const char* strSource)
{
//要判断参数的有效性
assert(strDestination != NULL && strSource != NULL);
//参数保护
char* pDest = strDestination;
while (*strSource != '\0')
{
*pDest++ = *strSource++;
}
*pDest = '\0';
return strDestination;
}
void main()
{
char str1[20] = "HelloABC";
char* str2 = "Linux";
printf("str1 = %s\n", str1);
char* res = my_strcpy(str1, str2);
printf("str1 = %s\n", res);
}
') { *pDest++ = *strSource++; } *pDest = '
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
char* my_strcpy(char* strDestination, const char* strSource)
{
//要判断参数的有效性
assert(strDestination != NULL && strSource != NULL);
//参数保护
char* pDest = strDestination;
while (*strSource != '\0')
{
*pDest++ = *strSource++;
}
*pDest = '\0';
return strDestination;
}
void main()
{
char str1[20] = "HelloABC";
char* str2 = "Linux";
printf("str1 = %s\n", str1);
char* res = my_strcpy(str1, str2);
printf("str1 = %s\n", res);
}
'; return strDestination; } void main() { char str1[20] = "HelloABC"; char* str2 = "Linux"; printf("str1 = %s\n", str1); char* res = my_strcpy(str1, str2); printf("str1 = %s\n", res); }

3.模拟实现strcat函数规定

  • 源字符串必须以 ‘\0’ 结束。
  • 目标空间必须有足够的大,能容纳下源字符串的内容。
  • 目标空间必须可修改

源代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>

char* my_strcat(char* strDestination, const char* strSource)
{
	//要判断参数的有效性
	assert(strDestination != NULL && strSource != NULL);
	//参数保护
	char* pDest = strDestination;
	while (*pDest != '
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
char* my_strcat(char* strDestination, const char* strSource)
{
//要判断参数的有效性
assert(strDestination != NULL && strSource != NULL);
//参数保护
char* pDest = strDestination;
while (*pDest != '\0')
pDest++;
while (*strSource != '\0')
*pDest++ = *strSource++;
*pDest = '\0';
return strDestination;
}
void main()
{
char str1[20] = "Helloabc";
char* str2 = "Linux";
printf("str1 = %s\n", str1);
char* res = my_strcat(str1, str2);
printf("str1 = %s\n", res);
}
') pDest++; while (*strSource != '
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
char* my_strcat(char* strDestination, const char* strSource)
{
//要判断参数的有效性
assert(strDestination != NULL && strSource != NULL);
//参数保护
char* pDest = strDestination;
while (*pDest != '\0')
pDest++;
while (*strSource != '\0')
*pDest++ = *strSource++;
*pDest = '\0';
return strDestination;
}
void main()
{
char str1[20] = "Helloabc";
char* str2 = "Linux";
printf("str1 = %s\n", str1);
char* res = my_strcat(str1, str2);
printf("str1 = %s\n", res);
}
') *pDest++ = *strSource++; *pDest = '
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
char* my_strcat(char* strDestination, const char* strSource)
{
//要判断参数的有效性
assert(strDestination != NULL && strSource != NULL);
//参数保护
char* pDest = strDestination;
while (*pDest != '\0')
pDest++;
while (*strSource != '\0')
*pDest++ = *strSource++;
*pDest = '\0';
return strDestination;
}
void main()
{
char str1[20] = "Helloabc";
char* str2 = "Linux";
printf("str1 = %s\n", str1);
char* res = my_strcat(str1, str2);
printf("str1 = %s\n", res);
}
'; return strDestination; } void main() { char str1[20] = "Helloabc"; char* str2 = "Linux"; printf("str1 = %s\n", str1); char* res = my_strcat(str1, str2); printf("str1 = %s\n", res); }

4.模拟实现strcmp函数规定

  • 第一个字符串大于第二个字符串,则返回大于0的数字
  • 第一个字符串等于第二个字符串,则返回0
  • 第一个字符串小于第二个字符串,则返回小于0的数字

源代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>

int my_strcmp(const char* string1, const char* string2)
{
	assert(string1 != NULL && string2 != NULL);
	int res = 0;
	while (*string1 != '
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
int my_strcmp(const char* string1, const char* string2)
{
assert(string1 != NULL && string2 != NULL);
int res = 0;
while (*string1 != '\0' || *string2 != '\0')
{
//通过减法的方式完成比较
if ((res = *string1 - *string2) != 0)
break;
string1++;
string2++;
}
if (res > 0)
res = 1;
else if (res < 0)
res = -1;
return res;
}
void main()
{
char* str1 = "Helloab";
char* str2 = "HelloABCab";
int res = my_strcmp(str1, str2);
printf("res = %d\n", res);
}
' || *string2 != '
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
int my_strcmp(const char* string1, const char* string2)
{
assert(string1 != NULL && string2 != NULL);
int res = 0;
while (*string1 != '\0' || *string2 != '\0')
{
//通过减法的方式完成比较
if ((res = *string1 - *string2) != 0)
break;
string1++;
string2++;
}
if (res > 0)
res = 1;
else if (res < 0)
res = -1;
return res;
}
void main()
{
char* str1 = "Helloab";
char* str2 = "HelloABCab";
int res = my_strcmp(str1, str2);
printf("res = %d\n", res);
}
') { //通过减法的方式完成比较 if ((res = *string1 - *string2) != 0) break; string1++; string2++; } if (res > 0) res = 1; else if (res < 0) res = -1; return res; } void main() { char* str1 = "Helloab"; char* str2 = "HelloABCab"; int res = my_strcmp(str1, str2); printf("res = %d\n", res); }