利用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)文章希望大家以后多多支持腳本之家!
- C語言中strcpy和strcat的使用和模擬實現(xiàn)
- C語言string庫strcpy、strcmp、strcat函數(shù)的使用
- C語言strlen,strcpy,strcmp,strcat,strstr字符串操作函數(shù)實現(xiàn)
- C語言詳細講解strcpy strcat strcmp函數(shù)的模擬實現(xiàn)
- 徹底掌握C語言strcat函數(shù)的用法
- C語言 模擬實現(xiàn)strcpy與strcat函數(shù)詳解
- C語言字符串函數(shù)操作(strlen,strcpy,strcat,strcmp)詳解
- strcat 函數(shù)的使用指南
- strcat函數(shù)實現(xiàn)簡單示例
- c++實現(xiàn)strcat字符串連接庫函數(shù)的方法詳解
- c++ 連接兩個字符串實現(xiàn)代碼 實現(xiàn)類似strcat功能
- C語言strcat函數(shù)詳解:字符串追加的利器
相關(guān)文章
Visual Studio 2019配置OpenCV4.1.1詳細圖解教程
這篇文章主要介紹了Visual Studio 2019配置OpenCV4.1.1詳細圖解教程 ,需要的朋友可以參考下2020-02-02C++使用MySQL-Connector/C++連接MySQL出現(xiàn)LNK2019錯誤的解決方法
這篇文章主要介紹了C++使用MySQL-Connector/C++連接MySQL出現(xiàn)LNK2019錯誤的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03