87.概率密码库生成,按密码出现次数写入文件生成密码库

  • 密码相似度排序后的文件地址和按密码出现次数排序后的文件地址
    1 //按密码相似度排序后的文件地址
    2 char pathsortbypass[512] = "QQpasswordsort.txt";
    3 //按密码出现次数排序的文件地址
    4 char pathsortbyci[512] = "QQpasswordcisort.txt";
  • 获取文件哟多少行
    1 #define  N 61437723 
  • 密码结构体信息
    1 //密码结构体信息
    2 struct info
    3 {
    4     char str[17];
    5     int ci;
    6 };
    7 //指向所有结构体的指针
    8 struct info *pall = NULL;
  • 文件载入内存
     1 //文件载入到内存
     2 void init()
     3 {
     4     time_t start, end;
     5     time(&start);
     6 
     7     //分配内存
     8     pall = calloc(N, sizeof(struct info));
     9     if (pall == NULL)
    10     {
    11         puts("calloc fail");
    12         return;
    13     }
    14     //打开按密码排序后的文件
    15     FILE *pf = fopen(pathsortbypass, "r");
    16     for (int i = 0; i < N; i++)
    17     {
    18         char str[50] = { 0 };
    19         //读取
    20         fgets(str, 50, pf);
    21         //读取一行的格式 12----asdfg
    22         //判断首元素
    23         if (*str>='0' &&*str<='9')
    24         {
    25             //获取一行长度,判断合法性 最短1----123456
    26             int length = strlen(str);
    27             if (length >= 11)
    28             {
    29                 //查找"----"
    30                 char *pfind = strstr(str, "----");
    31                 if (*pfind!=NULL)
    32                 {
    33                     *pfind = '';
    34                     //获取字符串出现的次数
    35                     pall[i].ci = atoi(str);
    36                     //读取
    37                     strcpy(pall[i].str, pfind + 4);
    38                 }
    39             }
    40         }
    41     }
    42     //关闭文件
    43     fclose(pf);
    44     //获取时间
    45     time(&end);
    46     printf("文件读取花费%f秒
    ", difftime(end, start));
    47 }
  • 排序函数
     1 //快速排序比较文件
     2 int com(void *p1, void *p2)
     3 {
     4     //转化成结构体类型
     5     struct info *pinfo1 = p1;
     6     struct info *pinfo2 = p2;
     7     //按次数从大到小排序
     8     if (pinfo1->ci >pinfo2->ci)
     9     {
    10         return -1;
    11     }
    12     else if (pinfo1->ci <pinfo2->ci)
    13     {
    14         return 1;
    15     }
    16     else
    17     {
    18         return 0;
    19     }
    20 }
  • 快速排序
     1 //排序函数
     2 void sort()
     3 {
     4     time_t start, end;
     5     time(&start);
     6     //快速排序
     7     qsort(pall, N, sizeof(struct info), com);
     8     time(&end);
     9     printf("sort话费%f秒
    ", difftime(end, start));
    10 }
  • 获取文件行数
     1 //获取文件行数
     2 int getN(char *pathsortbypass)
     3 {
     4     int i = 0;
     5     FILE *pf = fopen(pathsortbypass, "r");
     6     if (pf == NULL)
     7     {
     8         return -1;
     9 
    10     }
    11     else
    12     {
    13         while (!feof(pf))
    14         {
    15             char str[50] = { 0 };
    16             fgets(str, 50, pf);
    17             i++;
    18         }
    19 
    20         fclose(pf);
    21         return i;
    22     }
    23 }
  • 写入文件
     1 //写入文件
     2 void writetofile()
     3 {
     4     //记录世界
     5     time_t start, end;
     6     time(&start);
     7     //以写的方式打开文件
     8     FILE *pf = fopen(pathsortbyci, "w");
     9     //写入
    10     for (int i = 0; i < N; i++)
    11     {
    12         //合法性判断
    13         if (pall[i].ci != 0)
    14         {
    15             char strall[100] = { 0 };
    16             //打印进入
    17             sprintf(strall, "%d  %s", pall[i].ci, pall[i].str);
    18             //写入
    19             fputs(strall, pf);
    20         }
    21     }
    22 
    23     //关闭文件
    24     fclose(pf);
    25     time(&end);
    26     printf("sort花费%f秒
    ", difftime(end, start));
    27 }

完整代码

  1 #define  _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<string.h>
  5 #include<time.h>
  6 
  7 //按密码相似度排序后的文件地址
  8 char pathsortbypass[512] = "QQpasswordsort.txt";
  9 //按密码出现次数排序的文件地址
 10 char pathsortbyci[512] = "QQpasswordcisort.txt";
 11 #define  N 61437723 
 12 
 13 //密码结构体信息
 14 struct info
 15 {
 16     char str[17];
 17     int ci;
 18 };
 19 //指向所有结构体的指针
 20 struct info *pall = NULL;
 21 
 22 //写入文件
 23 void writetofile()
 24 {
 25     //记录世界
 26     time_t start, end;
 27     time(&start);
 28     //以写的方式打开文件
 29     FILE *pf = fopen(pathsortbyci, "w");
 30     //写入
 31     for (int i = 0; i < N; i++)
 32     {
 33         //合法性判断
 34         if (pall[i].ci != 0)
 35         {
 36             char strall[100] = { 0 };
 37             //打印进入
 38             sprintf(strall, "%d  %s", pall[i].ci, pall[i].str);
 39             //写入
 40             fputs(strall, pf);
 41         }
 42     }
 43 
 44     //关闭文件
 45     fclose(pf);
 46     time(&end);
 47     printf("sort花费%f秒
", difftime(end, start));
 48 }
 49 
 50 //快速排序比较文件
 51 int com(void *p1, void *p2)
 52 {
 53     //转化成结构体类型
 54     struct info *pinfo1 = p1;
 55     struct info *pinfo2 = p2;
 56     //按次数从大到小排序
 57     if (pinfo1->ci >pinfo2->ci)
 58     {
 59         return -1;
 60     }
 61     else if (pinfo1->ci <pinfo2->ci)
 62     {
 63         return 1;
 64     }
 65     else
 66     {
 67         return 0;
 68     }
 69 }
 70 
 71 //排序函数
 72 void sort()
 73 {
 74     time_t start, end;
 75     time(&start);
 76     //快速排序
 77     qsort(pall, N, sizeof(struct info), com);
 78     time(&end);
 79     printf("sort话费%f秒
", difftime(end, start));
 80 }
 81 
 82 //文件载入到内存
 83 void init()
 84 {
 85     time_t start, end;
 86     time(&start);
 87 
 88     //分配内存
 89     pall = calloc(N, sizeof(struct info));
 90     if (pall == NULL)
 91     {
 92         puts("calloc fail");
 93         return;
 94     }
 95     //打开按密码排序后的文件
 96     FILE *pf = fopen(pathsortbypass, "r");
 97     for (int i = 0; i < N; i++)
 98     {
 99         char str[50] = { 0 };
100         //读取
101         fgets(str, 50, pf);
102         //读取一行的格式 12----asdfg
103         //判断首元素
104         if (*str>='0' &&*str<='9')
105         {
106             //获取一行长度,判断合法性 最短1----123456
107             int length = strlen(str);
108             if (length >= 11)
109             {
110                 //查找"----"
111                 char *pfind = strstr(str, "----");
112                 if (*pfind!=NULL)
113                 {
114                     *pfind = '';
115                     //获取字符串出现的次数
116                     pall[i].ci = atoi(str);
117                     //读取
118                     strcpy(pall[i].str, pfind + 4);
119                 }
120             }
121         }
122     }
123     //关闭文件
124     fclose(pf);
125     //获取时间
126     time(&end);
127     printf("文件读取花费%f秒
", difftime(end, start));
128 }
129 
130 //获取文件行数
131 int getN(char *pathsortbypass)
132 {
133     int i = 0;
134     FILE *pf = fopen(pathsortbypass, "r");
135     if (pf == NULL)
136     {
137         return -1;
138 
139     }
140     else
141     {
142         while (!feof(pf))
143         {
144             char str[50] = { 0 };
145             fgets(str, 50, pf);
146             i++;
147         }
148 
149         fclose(pf);
150         return i;
151     }
152 }
153 
154 void main()
155 {
156     //初始化
157     init();
158     //排序
159     sort();
160     //写入文件
161     writetofile();
162     system("pause");
163 }
原文地址:https://www.cnblogs.com/xiaochi/p/8458465.html