#include <iostream> #include <ctime> using namespace std; struct userNode { int key; bool sex; int birthday; struct userNode *next = NULL; }; userNode *HashTable[288]; int Hash(int key) { int res = 0; while (key) { res += key % 100; key /= 100; } return res - 10; } 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 << "| PP号:" << nowTmp->key << " |" << endl << "| 性别:" << (nowTmp->sex ? "男" : "女") << " |" << endl << "| 生日:" << nowTmp->birthday << " |" << endl << "-------------------------------------------" << endl; system("pause"); } else { // 自动注册 srand(time(0)); 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"); } } //散列情况展示部分 // int a[288]; // for (int i = 0; i <= 287; i++) // { // a[i]=0; // } // for (int i = 100000; i <= 999999; i++) // { // a[Hash(i)]++; // } // for (int i = 0; i <= 287; i++) // { // cout<<"a["<<i<<"]="<<a[i]<<endl; // } return 0; }写注释
时间: 2024-03-14 20:43:05 浏览: 117
好的,以下是代码的注释:
```c++
#include <iostream>
#include <ctime> // 用于获取系统时间
using namespace std;
// 定义用户结构体
struct userNode {
int key; // 用户的PP号
bool sex; // 用户性别,true表示男,false表示女
int birthday; // 用户生日,格式为YYYYMMDD
struct userNode *next = NULL; // 用于解决哈希冲突的指针
};
userNode *HashTable[288]; // 定义哈希表,大小为288,即0~287
// 哈希函数,将给定的key映射到哈希表中的位置
int Hash(int key) {
int res = 0;
while (key) {
res += key % 100; // 取key的后两位进行累加
key /= 100; // 将key的后两位去掉
}
return res - 10; // 避免哈希值为负数
}
// 登录函数,根据给定的PP号查找用户信息
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); // 计算哈希值
newUser->next = HashTable[afterHash]; // 将新用户插入到该位置的链表头部
HashTable[afterHash] = newUser;
return 0;
}
int main() {
userNode nowTmp; // 用于保存当前登录的用户信息
int tmp; // 用于保存用户输入的PP号
while (1) { // 无限循环,直到用户主动退出
system("cls"); // 清屏
cout << "请输入你的PP号:" << endl;
tmp=0; // 将tmp初始化为0
while (tmp<100000 || tmp>999999) { // 判断用户输入的PP号是否合法
cin >> tmp;
}
nowTmp = Login(tmp); // 尝试登录
if (nowTmp) { // 登录成功
system("cls");
cout << "-------------------------------------------" << endl << "| 登录成功! |" << endl << "| PP号:" << nowTmp->key << " |" << endl << "| 性别:" << (nowTmp->sex ? "男" : "女") << " |" << endl << "| 生日:" << nowTmp->birthday << " |" << endl << "-------------------------------------------" << endl;
system("pause");
} else { // 登录失败,自动注册
srand(time(0)); // 用系统时间作为随机数的种子
nowTmp = new userNode; // 创建新用户
nowTmp->key=tmp; // 将PP号赋值给新用户
nowTmp->birthday= 2002 + rand() % 21; // 生成随机的出生年份
nowTmp->birthday=100; // 这里应该是nowTmp->birthday +=,而不是nowTmp->birthday=
nowTmp->birthday+= 1 + rand() % 12; // 生成随机的出生月份
nowTmp->birthday=100; // 这里应该是nowTmp->birthday +=,而不是nowTmp->birthday=
nowTmp->birthday+= 1 + rand() % 28; // 生成随机的出生日期
nowTmp->sex= rand() % 2; // 生成随机的性别
Register(nowTmp); // 将新用户加入哈希表
cout << "这个PP号还没注册!帮你注册了!请重新登录!" << endl;
system("pause");
}
}
// 下面是用于展示哈希表散列情况的代码,可以暂时忽略
// int a[288];
// for (int i = 0; i <= 287; i++)
// {
// a[i]=0;
// }
// for (int i = 100000; i <= 999999; i++)
// {
// a[Hash(i)]++;
// }
// for (int i = 0; i <= 287; i++)
// {
// cout<<"a["<<i<<"]="<<a[i]<<endl;
// }
return 0;
}
```
阅读全文
相关推荐

















