首页 » 技术分享 » 密码库

密码库

 

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

思路:从文本文件中提取密码字符串,保存在链表中,链表有密码字段和密码出现次数的字段,当有新的密码出现就开辟一个新的空间,如果出现的密码在链表字段中,就把相应的密码次数加 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");
}

转载自原文链接, 如需删除请联系管理员。

原文链接:密码库,转载请注明来源!

0