日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

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

作者:Fug_Lee ? 更新時(shí)間: 2022-12-07 編程語言

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

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

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

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

  • 源字符串必須以 ‘\0’ 結(jié)束。
  • 源字符串中的 ‘\0’ 也將會(huì)拷貝到。
  • 目標(biāo)空間必須足夠大,以確保能存放源字符串。
  • 目標(biāo)空間必須可變。

源代碼:

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

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

源代碼:

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

  • 第一個(gè)字符串大于第二個(gè)字符串,則返回大于0的數(shù)字
  • 第一個(gè)字符串等于第二個(gè)字符串,則返回0
  • 第一個(gè)字符串小于第二個(gè)字符串,則返回小于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);
}

原文鏈接:https://blog.csdn.net/Onion_521257/article/details/105498713

欄目分類
最近更新