当前位置:   article > 正文

密码库_密码库文本

密码库文本

生成密码库可以查询密码使用最多的字符串:

思路:从文本文件中提取密码字符串,保存在链表中,链表有密码字段和密码出现次数的字段,当有新的密码出现就开辟一个新的空间,如果出现的密码在链表字段中,就把相应的密码次数加 1 。

相应代码如下:

密码.h

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. typedef struct pwdata
  6. {
  7. char pw[50];
  8. int ci;
  9. struct pwdata *pNext;
  10. }info,*PINFO;
  11. PINFO addFront(PINFO phead, char *pw); //头插
  12. void show(PINFO phead); //显示
  13. int isin(PINFO phead, char *pw); //判断密码是否在链表中,有返回 1 没有返回 0
  14. PINFO sortByCi(PINFO phead); //按出现次数排序
  15. PINFO sortByPw(PINFO phead); //按密码排序
  16. void writeTofile(PINFO phead,char *path); //写入到文件

密码.c

  1. #include"密码库.h"
  2. PINFO addFront(PINFO phead, char *pw) //头插
  3. {
  4. //初始化结构体
  5. PINFO pnew = calloc(1, sizeof(info)); //开辟链表空间
  6. strcpy(pnew->pw, pw);
  7. pnew->ci = 1;
  8. pnew->pNext = NULL;
  9. if (phead == NULL)
  10. {
  11. phead = pnew;
  12. }
  13. else
  14. {
  15. //插在链表头部
  16. pnew->pNext = phead;
  17. phead = pnew;
  18. }
  19. return phead;
  20. }
  21. void show(PINFO phead) //显示
  22. {
  23. if (phead == NULL)
  24. {
  25. return;
  26. }
  27. else
  28. {
  29. printf("%s %d \n", phead->pw, phead->ci);
  30. show(phead->pNext);
  31. }
  32. }
  33. int isin(PINFO phead, char *pw) //判断密码是否在链表中,有返回 1 没有返回 0
  34. {
  35. PINFO p = phead;
  36. while (p)
  37. {
  38. if (strcmp(p->pw, pw) == 0)
  39. {
  40. p->ci += 1;
  41. return 1;
  42. }
  43. p = p->pNext;
  44. }
  45. return 0; //数据库中没有此密码
  46. }
  47. PINFO sortByCi(PINFO phead) //按出现次数排序
  48. {
  49. for (PINFO p1 = phead; p1 != NULL; p1 = p1->pNext)
  50. {
  51. for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext)
  52. {
  53. if (p2->pNext != NULL)
  54. {
  55. if (p2->ci <p2->pNext->ci)
  56. {
  57. char pass[100] = { 0 };
  58. strcpy(pass, p2->pw);
  59. strcpy(p2->pw, p2->pNext->pw);
  60. strcpy(p2->pNext->pw, pass);
  61. int data = p2->ci;
  62. p2->ci = p2->pNext->ci;
  63. p2->pNext->ci = data;
  64. }
  65. }
  66. }
  67. }
  68. return phead;
  69. }
  70. PINFO sortByPw(PINFO phead) //按密码排序
  71. {
  72. for (PINFO p1 = phead; p1 != NULL; p1 = p1->pNext)
  73. {
  74. for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext)
  75. {
  76. if (p2->pNext != NULL)
  77. {
  78. if (strcmp(p2->pw, p2->pNext->pw) > 0)
  79. {
  80. char pass[100] = { 0 };
  81. strcpy(pass,p2->pw);
  82. strcpy(p2->pw, p2->pNext->pw);
  83. strcpy(p2->pNext->pw, pass);
  84. int data = p2->ci;
  85. p2->ci = p2->pNext->ci;
  86. p2->pNext->ci = data;
  87. }
  88. }
  89. }
  90. }
  91. return phead;
  92. }
  93. void writeTofile(PINFO phead,char *path) //写入到文件
  94. {
  95. FILE *pfw = fopen(path, "w");
  96. if (pfw == NULL)
  97. {
  98. printf("file open fail!\n");
  99. return;
  100. }
  101. PINFO p = phead;
  102. while (p != NULL)
  103. {
  104. fprintf(pfw, "%s,%d\n", p->pw, p->ci);
  105. p = p->pNext;
  106. }
  107. fclose(pfw);
  108. }

main.c

  1. void main()
  2. {
  3. PINFO phead = NULL;
  4. char *pw[10] = { "123", "123", "234", "234", "234", "3456", "3456",
  5. "098", "1323", "12345" };
  6. for (int i = 0; i < 10; i++)
  7. {
  8. if (isin(phead,pw[i]) == 0)
  9. {
  10. phead = addFront(phead, pw[i]);
  11. }
  12. }
  13. show(phead);
  14. phead = sortByCi(phead);
  15. printf("\n按次数排序后:\n");
  16. show(phead);
  17. printf("\n按密码排序后:\n");
  18. show(phead);
  19. char *path = "c:\\demo.txt";
  20. writeTofile(phead, path);
  21. printf("\n>>>>>>>>>>>>>>>>>>>>>>>\n");
  22. readToread("c:\\demo.txt");
  23. system("pause");
  24. }


声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号