11-散列3 QQ帐户的申请与登陆

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct ListNode *Position;
typedef struct ListNode{
	char account[11], password[17];
	Position Next;
} *List;

typedef struct HashTbl{
	int TableSize;
	List *TheLists;//建立一个ListNode的指针数组
} *HashTable;

int NextPrime(int x);
HashTable InitializeTable(int TableSize);
void DestroyTable(HashTable H);
Position Find(char Key[], HashTable H);
void Insert(char Key[], char password[], HashTable H);
int Hash(int Key, int TableSize);
int Find_account(char Key[], HashTable H);
int Check_passwrod(Position P, char password[]);

int main(int argc, char const *argv[])
{
//	freopen("test.txt", "r", stdin);
	int N;
	scanf("%d\n", &N);
	HashTable H = InitializeTable(2 * N);
	char c, s1[11] = {}, s2[17] ={};
	for (int i = 0; i < N; i++){
		scanf("%c %s %s\n", &c, s1, s2);
		if (c == 'N'){
			if (Find_account(s1, H) != 1){
				Insert(s1, s2, H);
				printf("New: OK\n");
			}else
				printf("ERROR: Exist\n");
		}
		if (c == 'L'){
			if (Find_account(s1, H) == 1){
				if (Check_passwrod(Find(s1, H), s2))
					printf("Login: OK\n");
				else
					printf("ERROR: Wrong PW\n");
			}else
				printf("ERROR: Not Exist\n");
		}
	}
	DestroyTable(H);
	return 0;
}

int NextPrime(int x)
{
	int i; 
	for (int Next = x; ; Next++){
		for (i = 2; i * i <= Next; i++)
			if (Next % i == 0)
				break;
		if (i * i > Next)
			return Next;
	}
}

HashTable InitializeTable(int TableSize)
{
	HashTable H;
	H = (HashTable)malloc(sizeof(struct HashTbl));
	H->TableSize = NextPrime(TableSize);
	H->TheLists = (List*)malloc(sizeof(List)*H->TableSize);
	for (int i = 0; i != H->TableSize; i++){
		H->TheLists[i] = (List)malloc(sizeof(struct ListNode));
		H->TheLists[i]->Next = NULL;
	}
	return H;
}

void DestroyTable(HashTable H)
{
	for (int i = 0; i < H->TableSize; i++)
		free(H->TheLists[i]);
	free(H->TheLists);
	free(H);
}

Position Find(char Key[], HashTable H)
{
	Position P;
	List L;
	L = H->TheLists[Hash(atoi(Key + 3), H->TableSize)];
	P = L->Next;
	while (P != NULL && strcmp(P->account, Key)){
		P = P->Next;
	}
	return P;
}

int Find_account(char Key[], HashTable H)
{
	Position P;
	List L;
	L = H->TheLists[Hash(atoi(Key + 3), H->TableSize)];
	P = L->Next;
	while (P != NULL){
		if (strcmp(P->account, Key) == 0)
			return 1;
		else
			P = P->Next;
	}
	return 0;
}

void Insert(char Key[], char password[], HashTable H)
{
	Position Pos, Tmp;
	List L = H->TheLists[Hash(atoi(Key + 3), H->TableSize)];
	Pos = Find(Key, H);
	if (Pos == NULL){
		Tmp = (List)malloc(sizeof(struct ListNode));
		strcpy(Tmp->account, Key);
		strcpy(Tmp->password, password);
		Tmp->Next = L->Next;
		L->Next = Tmp;
	}
}

int Hash(int Key, int TableSize)
{
	return (Key % TableSize);
}

int Check_passwrod(Position P, char password[])
{
	if (strcmp(P->password, password) == 0)
		return 1;
	else
		return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要选择一个合适的散列函数。在实际应用中,通常使用哈希函数来将输入映射为固定长度的输出。常见的哈希函数有MD5、SHA-1等。在本例中,我们选择SHA-256算法作为哈希函数。 其次,我们需要考虑冲突处理方法。当两个不同的输入映射到同一个哈希值时,就会发生哈希冲突。这种情况下,我们需要采取一些措施来解决冲突。常见的冲突处理方法有开放地址法、链表法等。在本例中,我们选择链表法作为冲突处理方法。 下面是QQ账户的申请与登录的编程实现(Python代码): ```python import hashlib class User: def __init__(self, username, password): self.username = username self.password = self._hash_password(password) def _hash_password(self, password): sha256 = hashlib.sha256() sha256.update(password.encode('utf-8')) return sha256.hexdigest() class QQAccount: def __init__(self): self.user_dict = {} def register(self, username, password): if username in self.user_dict: print('Username already exists!') return user = User(username, password) self.user_dict[username] = user print('Registration successful!') def login(self, username, password): if username not in self.user_dict: print('Username does not exist!') return False user = self.user_dict[username] if user.password != password: print('Password incorrect!') return False print('Login successful!') return True ``` 在上面的代码中,我们定义了一个`User`类来表示用户,其中`_hash_password`方法使用SHA-256算法对密码进行哈希。然后,我们定义了一个`QQAccount`类来管理用户账户,其中`register`方法用于注册新用户,`login`方法用于用户登录。在`login`方法中,我们首先检查用户名是否存在,然后比较输入的密码和哈希后的密码是否匹配,最后返回登录结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值