欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用C語言模擬實現(xiàn)qsort,strcpy,strcat,strcmp函數(shù)

 更新時間:2022年11月06日 17:00:26   作者:Fug_Lee  
這篇文章主要為大家詳細介紹了如何通過C語言模擬實現(xiàn)qsort(采用冒泡的方式),strcpy,strcat,strcmp等函數(shù),文中的示例代碼講解詳細,感興趣的可以了解一下

1.采用冒泡的方式模擬實現(xiàn)qsort

簡述回調(diào)函數(shù):

回調(diào)函數(shù)就是一個通過函數(shù)指針調(diào)用的函數(shù)。如果你把函數(shù)的指針(地址)作為參數(shù)傳遞給另一個函數(shù),當這個指針被用來調(diào)用其所指向的函數(shù)時,我們就說這是回調(diào)函數(shù)?;卣{(diào)函數(shù)不是由該函數(shù)的實現(xiàn)方直接調(diào)用,而是在特定的事件或條件發(fā)生時由另外的一方調(diào)用的,用于對該事件或條件進行響應(yīng)。

模擬實現(xiàn)qsort函數(shù)源代碼(采用冒泡的方式):

#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.模擬實現(xiàn)strcpy函數(shù)規(guī)定

  • 源字符串必須以 ‘\0’ 結(jié)束。
  • 源字符串中的 ‘\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)
{
	//要判斷參數(shù)的有效性
	assert(strDestination != NULL && strSource != NULL);
	//參數(shù)保護
	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);
}

3.模擬實現(xiàn)strcat函數(shù)規(guī)定

  • 源字符串必須以 ‘\0’ 結(jié)束。
  • 目標空間必須有足夠的大,能容納下源字符串的內(nèi)容。
  • 目標空間必須可修改

源代碼:

#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)
{
	//要判斷參數(shù)的有效性
	assert(strDestination != NULL && strSource != NULL);
	//參數(shù)保護
	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);
}

4.模擬實現(xiàn)strcmp函數(shù)規(guī)定

  • 第一個字符串大于第二個字符串,則返回大于0的數(shù)字
  • 第一個字符串等于第二個字符串,則返回0
  • 第一個字符串小于第二個字符串,則返回小于0的數(shù)字

源代碼:

#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);
}

到此這篇關(guān)于利用C語言模擬實現(xiàn)qsort,strcpy,strcat,strcmp函數(shù)的文章就介紹到這了,更多相關(guān)C語言qsort strcpy strcat strcmp內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論