十一级指针实现百万qq号的增删查改以及排序写入
生活随笔
收集整理的這篇文章主要介紹了
十一级指针实现百万qq号的增删查改以及排序写入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
運行結果:
內存使用情況:
寫入文件排序好的數據:
- 創建文件地址以及創建十一級指針 1 char *path = "QQ.txt"; 2 char *sortpath = "QQchiguowei2018.txt"; 3 //創建十一級指針 4 char *********** allP = NULL;
- 初始化十一級指針 1 //初始化2 void init()3 {4 5 FILE *pf = fopen(path, "r");6 if (pf==NULL)7 {8 9 printf("文件打開失敗"); 10 return; 11 } 12 else 13 { 14 //讀取每一行加載到allP中 15 for (int i = 0; i < allN; i++) 16 { 17 char str[50] = { 0 }; 18 fgets(str, 50, pf);//讀取 19 char *tmpstr = convertQQ(str);//獲取QQ號 20 21 if (isallnum(tmpstr)) 22 { 23 assignmem(&allP, 11, tmpstr);//分配內存 24 25 strcpy(allP[getnum(tmpstr[0])][getnum(tmpstr[1])][getnum(tmpstr[2])][getnum(tmpstr[3])][getnum(tmpstr[4])][getnum(tmpstr[5])][getnum(tmpstr[6])][getnum(tmpstr[7])][getnum(tmpstr[8])][getnum(tmpstr[9])], str); 26 27 } 28 } 29 } 30 fclose(pf); 31 }
- 字符串轉提取qq號的函數 1 //字符串提取qq號2 char *convertQQ(char *str)3 {4 int length = strlen(str);//獲取長度5 char *tmp = malloc(length + 1);6 strcpy(tmp, str);//拷貝,避免數據自動回收7 //獲取第一個'-'的位置8 char *p = strchr(tmp, '-');9 if (p!=NULL) 10 { 11 *p = '\0'; 12 13 } 14 int count = strlen(tmp);//獲取QQ長度 15 //如果長度小于10,則在前面置零,轉換成十位,最后一位/0也寫入,所以要從下標10開始,一共11位 16 if (count< 10) 17 { 18 for (int i = 10; i >= 0; i--, count--) 19 { 20 if (count>=0)//移動 21 { 22 tmp[i] = tmp[count]; 23 } 24 else 25 { 26 //填充0 27 tmp[i] = '0'; 28 } 29 } 30 } 31 return tmp; 32 }
- 判斷是不是全是數字 1 //判斷是不是全是數字2 int isallnum(char *str)//判斷是否有非法字符3 {4 while (*str)5 {6 if (*str<'0' || *str>'9')7 {8 return 0;9 } 10 11 str++; 12 } 13 return 1; 14 15 }
- 獲取對應數字 1 //獲取對應的數字 2 int getnum(char ch) 3 { 4 return ch - '0';//0 1 5 }
- 分配內存 ?深度表示前面還有幾顆星 如果還有一顆星則進行內存分配,剛開始前面有十一顆星 1 //分配內存 深度表示前面還有幾顆星 如果還有一顆星則進行內存分配,剛開始前面有十一顆星2 void assignmem(char **pp,int deep,char *str)3 {4 //如果前面有一顆星5 if (deep==1)6 {7 //分配內存 獲取qq號最后一位的數字8 pp[ getnum(*(str + 10 - deep))] = malloc(sizeof(char) * 50);9 //內存清零 10 memset(pp[getnum(*(str + 10 - deep))], 0, sizeof(char) * 50); 11 return; 12 } 13 14 //如果前邊有十一顆星 15 if (deep==11) 16 { 17 if (allP) 18 { 19 assignmem(*pp, deep - 1, str);//遞歸調用 20 return; 21 } 22 else 23 { 24 //allP指向十個十級指針 25 allP = malloc(sizeof(char**********) * 10);//分配指針數組 26 memset(allP, '\0', sizeof(char*) * 10);//清零 27 assignmem(*pp, deep - 1, str);//遞歸調用 28 return; 29 } 30 } 31 32 //如果已經分配內存 33 if (pp[getnum(*(str + 10 - deep))]) 34 { 35 //遞歸調用 36 assignmem(pp[getnum(*(str + 10 - deep))], deep - 1, str); 37 } 38 //否則分配內存 39 else 40 { 41 pp[getnum(*(str + 10 - deep))] = malloc(sizeof(char*) * 10); 42 memset(pp[getnum(*(str + 10 - deep))], 0, sizeof(char*) * 10); 43 assignmem(pp[getnum(*(str + 10 - deep))], deep - 1, str);//遞歸調用 44 } 45 }
- 查詢 1 //查詢2 void search(char *str)3 {4 //判斷是不是數字5 if (isallnum(str)==0)6 {7 return;8 }9 //如果查詢的長度大于10 10 if (strlen(str)>10) 11 { 12 return; 13 } 14 15 //格式化成10位 16 char *tmp = malloc(11); 17 int count = strlen(str); 18 if (count<=10) 19 { 20 for (int i = 10; i>=0; i--,count--) 21 { 22 if (count>=0) 23 { 24 tmp[i] = str[count]; 25 } 26 else 27 { 28 tmp[i] = '0'; 29 } 30 } 31 } 32 else 33 { 34 printf("NO NO"); 35 return; 36 } 37 38 //依次判斷內存是否存在 39 if (allP[getnum(tmp[0])]) 40 { 41 42 if (allP[getnum(tmp[0])][getnum(tmp[1])]) 43 { 44 45 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])]) 46 { 47 48 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])]) 49 { 50 51 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])]) 52 { 53 54 55 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])]) 56 { 57 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])]) 58 { 59 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])]) 60 { 61 62 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])]) 63 { 64 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])]) 65 { 66 67 printf("%s\n", allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])]); 68 return; 69 70 } 71 72 73 } 74 75 } 76 77 78 79 } 80 81 82 } 83 84 } 85 86 } 87 88 } 89 90 } 91 92 } 93 94 printf("沒有找到"); 95 96 }
- 刪除 1 //刪除某個數據2 void deleteit(char *str)3 {4 if (isallnum(str) == 0)5 {6 return;7 }8 if (strlen(str)>10)9 {10 return;11 12 }13 char *tmp = malloc(11);14 int count = strlen(str);15 if (count <= 10)//007702507716 {17 for (int i = 10; i >= 0; i--, count--)18 {19 if (count >= 0)20 {21 tmp[i] = str[count];22 }23 else24 {25 tmp[i] = '0';26 }27 28 }29 30 }31 else32 {33 printf("NO NO");34 return;35 }36 //037 if (allP[getnum(tmp[0])])38 {39 40 if (allP[getnum(tmp[0])][getnum(tmp[1])])41 {42 43 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])])44 {45 46 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])])47 {48 49 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])])50 {51 52 53 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])])54 {55 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])])56 {57 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])])58 {59 60 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])])61 {62 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])])63 {64 free(allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])]);65 66 allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])] = NULL;67 return;68 69 }70 71 72 }73 74 }75 76 77 78 }79 80 81 }82 83 }84 85 }86 87 }88 89 }90 91 }92 93 94 95 96 97 printf("沒有找到");98 99 100 }
- 增加QQ號購買平臺 1 //插入某個數據2 void insert(char *str, char *pass)3 {4 5 6 if (isallnum(str) == 0)7 {8 return;9 }10 if (strlen(str)>10)11 {12 return;13 14 }15 char *tmp = malloc(11);16 int count = strlen(str);17 if (count <= 10)//007702507718 {19 for (int i = 10; i >= 0; i--, count--)20 {21 if (count >= 0)22 {23 tmp[i] = str[count];24 }25 else26 {27 tmp[i] = '0';28 }29 30 }31 32 }33 else34 {35 printf("NO NO");36 return;37 }38 //039 if (allP[getnum(tmp[0])])40 {41 42 if (allP[getnum(tmp[0])][getnum(tmp[1])])43 {44 45 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])])46 {47 48 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])])49 {50 51 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])])52 {53 54 55 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])])56 {57 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])])58 {59 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])])60 {61 62 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])])63 {64 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])])65 {66 printf("%s\n", allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])]);67 printf("已經存在");68 return;69 }70 else71 {72 int length = strlen(pass);73 allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])] = calloc(length + 1, 1);74 strcpy(allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])], pass);75 76 return;77 }78 79 80 }81 82 }83 84 85 86 }87 88 89 }90 91 }92 93 }94 95 }96 97 }98 99 } 100 101 printf("沒有找到"); 102 103 104 }
- 修改 1 //改變某個數據2 void changeit(char *str, char *newpass)3 {4 5 6 7 if (isallnum(str) == 0)8 {9 return;10 }11 if (strlen(str)>10)12 {13 return;14 15 }16 char *tmp = malloc(11);17 int count = strlen(str);18 if (count <= 10)//007702507719 {20 for (int i = 10; i >= 0; i--, count--)21 {22 if (count >= 0)23 {24 tmp[i] = str[count];25 }26 else27 {28 tmp[i] = '0';29 }30 31 }32 33 }34 else35 {36 printf("NO NO");37 return;38 }39 //040 if (allP[getnum(tmp[0])])41 {42 43 if (allP[getnum(tmp[0])][getnum(tmp[1])])44 {45 46 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])])47 {48 49 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])])50 {51 52 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])])53 {54 55 56 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])])57 {58 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])])59 {60 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])])61 {62 63 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])])64 {65 if (allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])])66 {67 68 printf("已經存在");69 free(allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])]);70 int length = strlen(newpass);71 allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])] = calloc(length + 1, 1);72 strcpy(allP[getnum(tmp[0])][getnum(tmp[1])][getnum(tmp[2])][getnum(tmp[3])][getnum(tmp[4])][getnum(tmp[5])][getnum(tmp[6])][getnum(tmp[7])][getnum(tmp[8])][getnum(tmp[9])], newpass);73 74 75 return;76 }77 78 79 80 }81 82 }83 84 85 86 }87 88 89 }90 91 }92 93 }94 95 }96 97 }98 99 } 100 printf("沒有找到"); 101 }
- 順序寫入文件 1 //遞歸寫入到文件2 void memtofile(char **p, int deep, FILE *pf)3 {4 if (p == NULL)5 {6 return;7 }8 //如果深度為1(前面還有一顆星)寫入9 if (deep==1) 10 { 11 for (int i = 0; i <10; i++) 12 { 13 if (p[i] != NULL) 14 { 15 fputs(p[i], pf);//寫入 16 } 17 } 18 19 return; 20 } 21 22 for (int i = 0; i <10; i++) 23 { 24 memtofile(p[i], deep - 1, pf);//遞歸寫入 25 } 26 } 27 28 29 //按qq號順序寫入 30 void sorttofile() 31 { 32 FILE *pf = fopen(sortpath, "w"); 33 memtofile(allP, 10, pf);//寫入 34 fclose(pf); 35 }
- 測試 1 init();2 printf("init over sorttofile start");3 sorttofile();4 printf("\n sorttofile end");5 while (1)6 {7 int num;8 scanf("%d", &num);9 switch (num) 10 { 11 case 1: 12 { 13 char str[100] = { 0 }; 14 printf("請輸入要查找的QQ", str); 15 scanf("%s", str); 16 search(str); 17 puts("search over"); 18 break; 19 } 20 case 2: 21 { 22 char str[100] = { 0 }; 23 printf("請輸入要刪除的QQ", str); 24 scanf("%s", str); 25 deleteit(str); 26 puts("delete over"); 27 break; 28 } 29 case 3: 30 { 31 char str[100] = { 0 }; 32 printf("請輸入要插入的QQ", str); 33 scanf("%s", str); 34 35 char str1[100] = { 0 }; 36 printf("請輸入要插入的QQpass", str1); 37 scanf("%s", str1); 38 39 insert(str, str1); 40 puts("insert over"); 41 break; 42 } 43 case 4: 44 { 45 char str[100] = { 0 }; 46 printf("請輸入要change的QQ", str); 47 scanf("%s", str); 48 49 char str1[100] = { 0 }; 50 printf("請輸入要change的QQpass", str1); 51 scanf("%s", str1); 52 53 changeit(str, str1); 54 puts("change over"); 55 break; 56 } 57 58 default: 59 break; 60 } 61 62 }
總結
以上是生活随笔為你收集整理的十一级指针实现百万qq号的增删查改以及排序写入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 校园二手交易平台的开发和利用
- 下一篇: 关于游戏汉化