密码库
生成密码库可以查询密码使用最多的字符串:
思路:从文本文件中提取密码字符串,保存在链表中,链表有密码字段和密码出现次数的字段,当有新的密码出现就开辟一个新的空间,如果出现的密码在链表字段中,就把相应的密码次数加 1 。
相应代码如下:
密码.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct pwdata
{
char pw[50];
int ci;
struct pwdata *pNext;
}info,*PINFO;
PINFO addFront(PINFO phead, char *pw); //头插
void show(PINFO phead); //显示
int isin(PINFO phead, char *pw); //判断密码是否在链表中,有返回 1 没有返回 0
PINFO sortByCi(PINFO phead); //按出现次数排序
PINFO sortByPw(PINFO phead); //按密码排序
void writeTofile(PINFO phead,char *path); //写入到文件
密码.c
#include"密码库.h"
PINFO addFront(PINFO phead, char *pw) //头插
{
//初始化结构体
PINFO pnew = calloc(1, sizeof(info)); //开辟链表空间
strcpy(pnew->pw, pw);
pnew->ci = 1;
pnew->pNext = NULL;
if (phead == NULL)
{
phead = pnew;
}
else
{
//插在链表头部
pnew->pNext = phead;
phead = pnew;
}
return phead;
}
void show(PINFO phead) //显示
{
if (phead == NULL)
{
return;
}
else
{
printf("%s %d \n", phead->pw, phead->ci);
show(phead->pNext);
}
}
int isin(PINFO phead, char *pw) //判断密码是否在链表中,有返回 1 没有返回 0
{
PINFO p = phead;
while (p)
{
if (strcmp(p->pw, pw) == 0)
{
p->ci += 1;
return 1;
}
p = p->pNext;
}
return 0; //数据库中没有此密码
}
PINFO sortByCi(PINFO phead) //按出现次数排序
{
for (PINFO p1 = phead; p1 != NULL; p1 = p1->pNext)
{
for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext)
{
if (p2->pNext != NULL)
{
if (p2->ci <p2->pNext->ci)
{
char pass[100] = { 0 };
strcpy(pass, p2->pw);
strcpy(p2->pw, p2->pNext->pw);
strcpy(p2->pNext->pw, pass);
int data = p2->ci;
p2->ci = p2->pNext->ci;
p2->pNext->ci = data;
}
}
}
}
return phead;
}
PINFO sortByPw(PINFO phead) //按密码排序
{
for (PINFO p1 = phead; p1 != NULL; p1 = p1->pNext)
{
for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext)
{
if (p2->pNext != NULL)
{
if (strcmp(p2->pw, p2->pNext->pw) > 0)
{
char pass[100] = { 0 };
strcpy(pass,p2->pw);
strcpy(p2->pw, p2->pNext->pw);
strcpy(p2->pNext->pw, pass);
int data = p2->ci;
p2->ci = p2->pNext->ci;
p2->pNext->ci = data;
}
}
}
}
return phead;
}
void writeTofile(PINFO phead,char *path) //写入到文件
{
FILE *pfw = fopen(path, "w");
if (pfw == NULL)
{
printf("file open fail!\n");
return;
}
PINFO p = phead;
while (p != NULL)
{
fprintf(pfw, "%s,%d\n", p->pw, p->ci);
p = p->pNext;
}
fclose(pfw);
}
main.c
void main()
{
PINFO phead = NULL;
char *pw[10] = { "123", "123", "234", "234", "234", "3456", "3456",
"098", "1323", "12345" };
for (int i = 0; i < 10; i++)
{
if (isin(phead,pw[i]) == 0)
{
phead = addFront(phead, pw[i]);
}
}
show(phead);
phead = sortByCi(phead);
printf("\n按次数排序后:\n");
show(phead);
printf("\n按密码排序后:\n");
show(phead);
char *path = "c:\\demo.txt";
writeTofile(phead, path);
printf("\n>>>>>>>>>>>>>>>>>>>>>>>\n");
readToread("c:\\demo.txt");
system("pause");
}
文章最后发布于: 2016-08-25 15:10:25
相关阅读
两行命令查看电脑wifi密码
SVN客户端使用——如何访问库文件本地选择一个空间足够大的存储盘,新建一个名为SVN_WORK 文件夹专门用来存放SVN文件,在SVN_WORK文
随着wordpress网站的时间越来越久,文章越来越多,大家是不是也感受到自己的wordpress网站的运行速度也随之下降?这是什么原因呢?前面的
在Excel中经常录入重要数据的朋友都知道,如果想要数据不被篡改只能为表格添加密码保护,但有时候却忘记了密码,这个时候就需要用到破
供练习sql使用。MySQL同样提供了employees数据库,但并未随数据库一起安装,其下载链接也极其隐蔽,可能导致许多人没注意到。 下载地