- list.h
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 //创建密码结点 7 typedef struct passinfo 8 { 9 //密码 10 char password[20]; 11 //出现次数 12 int ci; 13 //下一个节点 14 struct passinfo *pNext; 15 }INFO, *PINFO; 16 17 //头部插入 18 PINFO addback(PINFO phead, char password[20]); 19 //按照次数排序 20 PINFO sortbyci(PINFO phead); 21 //按照密码排序 22 PINFO sortbypass(PINFO phead); 23 //显示数据 24 PINFO show(PINFO phead); 25 //判断存在与否 26 int isin(PINFO phead, char password[20]); 27 //写入到文件 28 void writetofile(PINFO phead, char path[100]);
- list.c
1 #include "list.h" 2 3 //头部插入 4 PINFO addback(PINFO phead, char password[20]) 5 { 6 //开辟内存 7 PINFO pnew = calloc(1, sizeof(INFO)); 8 //拷贝密码 9 strcpy(pnew->password, password); 10 //初始化 11 pnew->ci = 1; 12 pnew->pNext = NULL; 13 //如果头结点为空直接插入 14 if (phead ==NULL) 15 { 16 phead = pnew; 17 } 18 //否则头部插入 19 else 20 { 21 pnew->pNext = phead; 22 phead = pnew; 23 } 24 return phead; 25 } 26 27 //按照密码次数排序 28 PINFO sortbyci(PINFO phead) 29 { 30 //冒泡排序法,一共冒泡这么多次 31 for (PINFO p1 = phead; p1 != NULL;p1=p1->pNext) 32 { 33 for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext) 34 { 35 if (p2->pNext!=NULL) 36 { 37 //如果次数小则进行交换 38 if (p2->ci < p2->pNext->ci) 39 { 40 //交换数据 41 int citemp = p2->ci; 42 p2->ci = p2->pNext->ci; 43 p2->pNext->ci = citemp; 44 45 char passtemp[100]; 46 strcpy(passtemp, p2->password); 47 strcpy(p2->password, p2->pNext->password); 48 strcpy(p2->pNext->password, passtemp); 49 } 50 } 51 } 52 } 53 } 54 55 //按照密码排序 56 PINFO sortbypass(PINFO phead) 57 { 58 //冒泡排序 59 for (PINFO p1 = phead; p1 != NULL; p1 = p1->pNext) 60 { 61 for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext) 62 { 63 if (p2->pNext != NULL) 64 { 65 if (strcmp(p2->password, p2->pNext->password) < 0) 66 { 67 //交换数据 68 int citemp = p2->ci; 69 p2->ci = p2->pNext->ci; 70 p2->pNext->ci = citemp; 71 72 char passtemp[100]; 73 strcpy(passtemp, p2->password); 74 strcpy(p2->password, p2->pNext->password); 75 strcpy(p2->pNext->password, passtemp); 76 77 } 78 } 79 } 80 } 81 } 82 83 //显示数据 84 PINFO show(PINFO phead) 85 { 86 if (phead==NULL) 87 { 88 return; 89 } 90 else 91 { 92 printf("%s,%d\n", phead->password, phead->ci); 93 show(phead->pNext); 94 } 95 return phead; 96 } 97 98 //判断密码是否存在 99 int isin(PINFO phead, char password[20]) 100 { 101 PINFO p = phead; 102 while (p!=NULL) 103 { 104 if (strcmp(p->password,password)==0) 105 { 106 p->ci += 1; 107 return 1; 108 } 109 p = p->pNext; 110 } 111 112 return 0;//不在链表内部 113 } 114 115 //写入到文件 116 void writetofile(PINFO phead, char path[100]) 117 { 118 FILE *pf = fopen(path, "w"); 119 PINFO p = phead; 120 while (p != NULL) 121 { 122 //格式化打印到文件 123 fprintf(pf, "%s %d\n", p->password, p->ci); 124 p = p->pNext; 125 } 126 fclose(pf); 127 }
- main.c
1 #include "list.h" 2 #include <Windows.h> 3 PINFO phead = NULL; 4 5 //是否满足格式要求 账号 # 密码 # 邮箱 6 int isoktosscanf(char *str) 7 { 8 //判断是否存在# 9 char *p = strstr(str, "#"); 10 if (p!=NULL) 11 { 12 //继续判断是否存在第二个# 13 if (strstr(p+1,"#")!=NULL) 14 { 15 return 1; 16 } 17 else 18 { 19 return 0; 20 } 21 } 22 else 23 { 24 return 0; 25 } 26 } 27 28 //消除空格 29 void eatspace(char *str) 30 { 31 //当前位置 32 int i = 0; 33 //游标 34 int j = 0; 35 //双指针错位 36 while ((str[i]=str[j++])!=‘\0‘) 37 { 38 if (str[i]!=‘ ‘) 39 { 40 i++; 41 } 42 } 43 } 44 45 //文件载入 46 void fileload() 47 { 48 //打开文件 49 FILE *pf = fopen("csdn.txt", "r"); 50 51 //如果没有到文件末尾 52 while (!feof(pf)) 53 { 54 char str[100] = { 0 }; 55 char password[100] = { 0 }; 56 //从文件中获取一行 57 fgets(str, 100, pf); 58 //找到第一个#的位置 59 char*p1 = strstr(str, "#"); 60 //找到第二个#的位置 61 char*p2 = strstr(p1+1, "#"); 62 //分别设置成‘\0‘ 63 *p1 = ‘\0‘; 64 *p2 = ‘\0‘; 65 //拷贝字符串 66 strcpy(password, p1 + 1); 67 //消除空格 68 eatspace(password); 69 //判断是否在链表中 70 if (isin(phead, password) == 0) 71 { 72 //添加到头结点 73 phead = addback(phead, password); 74 } 75 } 76 fclose(pf); 77 78 //按密码次数排序 79 sortbyci(phead); 80 //写入到文件 81 writetofile(phead, "C:\\ci.txt"); 82 //按照密码相似度排序 83 sortbypass(phead); 84 //写入到文件 85 writetofile(phead, "C:\\pass.txt"); 86 } 87 88 //main函数 89 void main4() 90 { 91 fileload(); 92 system("pause"); 93 }
原文地址:https://www.cnblogs.com/xiaochi/p/8454176.html
时间: 2024-11-02 09:05:08