65.十一级指针实现百万qq号的增删查改以及排序写入
Posted 喵小喵~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了65.十一级指针实现百万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)//0077025077 16 { 17 for (int i = 10; i >= 0; i--, count--) 18 { 19 if (count >= 0) 20 { 21 tmp[i] = str[count]; 22 } 23 else 24 { 25 tmp[i] = \'0\'; 26 } 27 28 } 29 30 } 31 else 32 { 33 printf("NO NO"); 34 return; 35 } 36 //0 37 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 }
- 增加
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)//0077025077 18 { 19 for (int i = 10; i >= 0; i--, count--) 20 { 21 if (count >= 0) 22 { 23 tmp[i] = str[count]; 24 } 25 else 26 { 27 tmp[i] = \'0\'; 28 } 29 30 } 31 32 } 33 else 34 { 35 printf("NO NO"); 36 return; 37 } 38 //0 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[以上是关于65.十一级指针实现百万qq号的增删查改以及排序写入的主要内容,如果未能解决你的问题,请参考以下文章
数据结构学习笔记(单链表单循环链表带头双向循环链表)的增删查改排序等)