#include <iostream> #include <ctime> using namespace std; struct userNode { int key; bool sex; int birthday; struct userNode* next = NULL; }; userNode* HashTable[298]; int Hash(int key)//散列函数 { int res = 0; while (key) { res += key % 100; key /= 100; } return res; } userNode* Login(int key)//查找 { int afterHash = Hash(key); userNode* p = HashTable[afterHash]; while (p && p->key != key) { p = p->next; } if (p && (p->key == key)) { return p; } else { return NULL; } return NULL; } int Register(userNode* newUser)//插入数据 { int afterHash = Hash(newUser->key); userNode* p = HashTable[afterHash]; while (p) { p = p->next; } newUser->next = HashTable[afterHash]; HashTable[afterHash] = newUser; return 0; } int main() { userNode* nowTmp; int tmp; while (1) { system("cls"); cout << "请输入你的PP号:" << endl; tmp = 0; while (tmp < 100000 || tmp>999999) { cin >> tmp; } nowTmp = Login(tmp); if (nowTmp) { system("cls"); cout << "-------------------------------------------" << endl << "| 登录成功! |" << endl << "| qq号:" << nowTmp->key << " |" << endl << "| 性别:" << (nowTmp->sex ? "男" : "女") << " |" << endl << "| 生日:" << nowTmp->birthday << " |" << endl << "-------------------------------------------" << endl; system("pause"); } else { // 自动注册 nowTmp = new userNode; nowTmp->key = tmp; nowTmp->birthday = 2002 + rand() % 21; nowTmp->birthday *= 100; nowTmp->birthday += 1 + rand() % 12; nowTmp->birthday *= 100; nowTmp->birthday += 1 + rand() % 28; nowTmp->sex = rand() % 2; Register(nowTmp); cout << "这个PP号还没注册!帮你注册了!请重新登录!" << endl; system("pause"); } } return 0; }给每一行注释
时间: 2024-01-03 08:02:54 浏览: 27
```
#include <iostream>
#include <ctime> // 包含ctime头文件,用于生成随机数种子
using namespace std;
struct userNode { // 定义用户节点结构体
int key; // 用户ID
bool sex; // 用户性别
int birthday; // 用户生日
struct userNode* next = NULL; // 指向下一个用户的指针,初始值为NULL
};
userNode* HashTable[298]; // 定义哈希表,长度为298,存储用户节点指针
int Hash(int key) { // 定义散列函数,用于将用户ID映射到哈希表中的位置
int res = 0; // 初始化散列值为0
while (key) { // 将用户ID按照100位一组进行分组,并将每组数字相加
res += key % 100;
key /= 100;
}
return res; // 返回散列值
}
userNode* Login(int key) { // 查询用户是否已经注册过,若已经注册过则返回节点指针,否则返回NULL
int afterHash = Hash(key); // 将用户ID散列到哈希表中的位置
userNode* p = HashTable[afterHash]; // 定义指针p指向对应位置的用户节点
while (p && p->key != key) { // 遍历链表,查找与输入的ID相等的节点
p = p->next;
}
if (p && (p->key == key)) { // 若找到了与ID相等的节点,则返回该节点指针
return p;
} else { // 否则返回NULL
return NULL;
}
return NULL;
}
int Register(userNode* newUser) { // 将新用户节点插入哈希表中
int afterHash = Hash(newUser->key); // 将用户ID散列到哈希表中的位置
userNode* p = HashTable[afterHash]; // 定义指针p指向对应位置的用户节点
while (p) { // 遍历链表,找到链表的末尾
p = p->next;
}
newUser->next = HashTable[afterHash]; // 将新节点插入到链表的头部
HashTable[afterHash] = newUser; // 将哈希表中对应位置的指针指向新节点
return 0;
}
int main() {
userNode* nowTmp; // 定义当前用户节点指针
int tmp; // 定义临时变量,用于存储用户输入的ID
while (1) { // 循环,直到用户结束程序
system("cls"); // 清屏
cout << "请输入你的PP号:" << endl;
tmp = 0;
while (tmp < 100000 || tmp>999999) { // 输入ID,要求ID为6位数
cin >> tmp;
}
nowTmp = Login(tmp); // 查询用户是否已经注册过,若已经注册过则返回节点指针,否则返回NULL
if (nowTmp) { // 若节点指针不为NULL,则输出用户信息并暂停程序
system("cls"); // 清屏
cout << "-------------------------------------------" << endl << "| 登录成功! |" << endl << "| qq号:" << nowTmp->key << " |" << endl << "| 性别:" << (nowTmp->sex ? "男" : "女") << " |" << endl << "| 生日:" << nowTmp->birthday << " |" << endl << "-------------------------------------------" << endl;
system("pause");
} else { // 若节点指针为NULL,则自动为用户注册,并输出提示信息
nowTmp = new userNode; // 创建新的用户节点
nowTmp->key = tmp; // 设置用户ID
nowTmp->birthday = 2002 + rand() % 21; // 生成用户生日,范围为2002年到2022年
nowTmp->birthday *= 100; // 将年份转换为3位数
nowTmp->birthday += 1 + rand() % 12; // 生成用户生日月份,范围为1月到12月
nowTmp->birthday *= 100; // 将月份转换为2位数
nowTmp->birthday += 1 + rand() % 28; // 生成用户生日日期,范围为1日到28日
nowTmp->sex = rand() % 2; // 生成用户性别,0表示女性,1表示男性
Register(nowTmp); // 将新用户节点插入哈希表中
cout << "这个PP号还没注册!帮你注册了!请重新登录!" << endl;
system("pause");
}
}
return 0;
}
```