数据结构基于哈希表的租号玩管理系统

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define HASH_SIZE 100 // 哈希表大小
typedef struct Play {
    int id;
    char account[50];
    char password[50];
    char game[50];
    char rentTime; // 租借时间
    bool rented;
    char username[50];
} Play;


typedef struct Node {
    Play playData;
    struct Node* next;
} Node;
Node* hashTable[HASH_SIZE]; // 哈希表数组
// 哈希函数
int hashFunction(int id) {
    return id % HASH_SIZE;
}
// 初始化哈希表
void initializeHashTable() {
    for (int i = 0; i < HASH_SIZE; i++) {
        hashTable[i] = NULL;
    }
}
void  makeMenu() {
    printf("—————————————————————-----\n");
    printf("—————租号玩管理系统————-\n");
    printf("——————1.添加账号————————\n");
    printf("——————2.查看所有账号————\n");
    printf("——————3.租账号——————————\n");
    printf("——————4.归还账号————————\n");
    printf("——————5.根据id删除账号——\n");
    printf("——————6.根据id修改账号——\n");
    printf("——————7.查找账号————————\n");
    printf("——————8.查看剩余可租————\n");
    printf("——————9.将文件读取哈希表————\n");
    printf("——————10.将数据读取到文件————\n");
    printf("—————————————————————-----\n");
    printf("***请选择你的操作*** \n ");
    printf("   ***1~~~~8***\n");
}
// 创建头节点
Node* createHead() {
    Node* head = (Node*)malloc(sizeof(Node));
    if (head != NULL) {
        head->next = NULL;
    }
    return head;
}
// 将账号插入哈希表
void insertAccount(Play account) {
    int hashIndex = hashFunction(account.id);
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        printf("内存分配失败\n");
        return;
    }
    newNode->playData = account;
    newNode->next = hashTable[hashIndex];
    hashTable[hashIndex] = newNode;
}
// 添加账号函数
void addAccount(Node* head) {
    Play newAccount;
    printf("请输入账号ID:");
    scanf_s("%d", &newAccount.id);
    printf("请输入账号:");
    scanf_s("%49s", newAccount.account, sizeof(newAccount.account));
    printf("请输入密码:");
    scanf_s("%49s", newAccount.password, sizeof(newAccount.password));
    printf("请输入游戏:");
    scanf_s("%49s", newAccount.game, sizeof(newAccount.game));
    newAccount.rentTime = 0;
    newAccount.rented = false;

    // 调用插入账号函数将账号插入哈希表
    int hashIndex = hashFunction(newAccount.id);
    insertAccount(newAccount);

    printf("账号添加成功!\n");
}
// 在主函数中添加显示所有账号的函数
void displayAllAccounts() {
    printf("【】\n");
    for (int i = 0; i < HASH_SIZE; i++) {
        Node* current = hashTable[i];
        while (current != NULL) {
            printf("账号ID:%d\n", current->playData.id);
            printf("账号:%s\n", current->playData.account);
            printf("密码:%s\n", current->playData.password);
            printf("游戏:%s\n", current->playData.game);
            printf("租借状态:%s\n", current->playData.rented ? "已租借" : "未租借");
            printf("租借时间:%ld\n", current->playData.rentTime);
            printf("\n");
            current = current->next;
        }
    }
}
// 释放链表内存
void freeList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        Node* temp = current;
        current = current->next;
        free(temp); // 释放节点内存
    }
}
// 释放哈希表内存
void freeHashTable() {
    for (int i = 0; i < HASH_SIZE; i++) {
        freeList(hashTable[i]); // 释放链表内存
    }
}
//租借账号
void rentAccount(int accountID) {
    int hashIndex = hashFunction(accountID);
    Node* current = hashTable[hashIndex];

    while (current != NULL) {
        if (current->playData.id == accountID) {
            if (!current->playData.rented) { // 只有在账号未被租借时才能租借
                current->playData.rented = true;
                int rentTime;
                printf("请输入租借时间:");
                scanf_s("%d", &rentTime);
                current->playData.rentTime = rentTime;
                printf("账号租借成功!\n");
            }
            else {
                printf("该账号已被租借!\n");
            }
            return; // 找到账号后,退出循环
        }
        current = current->next;
    }
    printf("找不到该账号ID!\n");
}
//归还账号
void returnAccount(int accountID) {
    int hashIndex = hashFunction(accountID);
    Node* current = hashTable[hashIndex];
    while (current != NULL) {
        if (current->playData.id == accountID) {
            if (current->playData.rented) { // 只有在账号已被租借时才能归还
                current->playData.rented = false;
                current->playData.rentTime = 0;
                printf("账号归还成功!\n");
            }
            else {
                printf("该账号未被租借,无法归还!\n");
            }
            return; // 找到账号后,退出循环
        }
        current = current->next;
    }
    printf("找不到该账号ID!\n");
}
//根据id修改账户
void deleteAccount(int accountID) {
    int hashIndex = hashFunction(accountID);
    Node* current = hashTable[hashIndex];
    Node* prev = NULL;
    while (current != NULL) {
        if (current->playData.id == accountID) {
            if (prev == NULL) {
                hashTable[hashIndex] = current->next; // 更新哈希表
            }
            else {
                prev->next = current->next;
            }
            free(current); // 释放内存
            printf("账号删除成功!\n");
            return;
        }
        prev = current;
        current = current->next;
    }
    printf("找不到该账号ID!\n");
}
// 根据ID修改账号信息函数
void modifyAccount(int accountID) {
    int hashIndex = hashFunction(accountID);
    Node* current = hashTable[hashIndex];
    while (current != NULL) {
        if (current->playData.id == accountID) {
            printf("请输入新的账号:");
            scanf("%49s", current->playData.account);

            printf("请输入新的密码:");
            scanf("%49s", current->playData.password);

            printf("请输入新的游戏:");
            scanf("%49s", current->playData.game);

            printf("账号信息修改成功!\n");
            return; // 找到账号后,退出循环
        }
        current = current->next;
    }
    printf("找不到该账号ID,无法修改信息!\n");
}
// 根据账号ID查找账号,返回找到的节点指针
Node* findAccountByID(int accountID) {
    int hashIndex = hashFunction(accountID);
    Node* current = hashTable[hashIndex];

    while (current != NULL) {
        if (current->playData.id == accountID) {
            return current;
        }
        current = current->next;
    }
    return NULL; // 未找到账号
}
// 读取文件并将账号信息插入到哈希表中
void readAccountsFromFile() {
    FILE* file = fopen("E:\\hello.txt", "r");
    if (file == NULL) {
        printf("无法打开文件\n");
        return;
    }
    while (!feof(file)) {
        Play account;
        if (fscanf(file, "账号ID:%d\n", &account.id) != 1) {
            break;
        }
        fscanf(file, "账号:%49s\n", account.account);
        fscanf(file, "密码:%49s\n", account.password);
        fscanf(file, "游戏:%49s\n", account.game);
        char rentedStatus[10];
        fscanf(file, "租借状态:%s\n", rentedStatus);
        account.rented = (strcmp(rentedStatus, "已租借") == 0);
        fscanf(file, "租借时间:%ld\n\n", &account.rentTime);

        // 调用插入账号函数将账号插入哈希表
        int hashIndex = hashFunction(account.id);
        insertAccount(account);
    }
    fclose(file);
    printf("账号信息已从文件读取并插入哈希表\n");
}
// 将账号信息写入文件
void writeAccountsToFile() {
    FILE* file = fopen("E:\\hello.txt", "w");
    if (file == NULL) {
        printf("无法打开文件\n");
        return;
    }
    for (int i = 0; i < HASH_SIZE; i++) {
        Node* current = hashTable[i];
        while (current != NULL) {
            fprintf(file, "账号ID:%d\n", current->playData.id);
            fprintf(file, "账号:%s\n", current->playData.account);
            fprintf(file, "密码:%s\n", current->playData.password);
            fprintf(file, "游戏:%s\n", current->playData.game);
            fprintf(file, "租借状态:%s\n", current->playData.rented ? "已租借" : "未租借");
            fprintf(file, "租借时间:%ld\n\n", current->playData.rentTime);

            current = current->next;
        }
    }
    fclose(file);
    printf("所有账号信息已写入文件\n");
}
//主函数
int main() {
    // 初始化哈希表
    initializeHashTable();
    Node* head = createHead();
    bool flag = true;
    int userput = 0;
    while (flag) {
        printf("请输入你的选择:");
        makeMenu();
        int userkey;
        scanf_s("%d", &userkey);
        Node* head = createHead();
        switch (userkey) {
        case 1:
            printf("【添加】\n");
            addAccount(head); // 插入账号到链表
            break;
        case 2:
            printf_s("【所有】\n");
            displayAllAccounts(); // 显示所有账号信息
            break;
        case 3:
            printf_s("【租账号】\n");
            int accountID;
            printf("请输入要租借的账号ID:");
            scanf_s("%d", &accountID);
            rentAccount(accountID);
            break;
        case 4:
            printf("【归还】\n");
            int returnAccountID;
            printf("请输入要归还的账号ID:");
            scanf_s("%d", &returnAccountID);
            returnAccount(returnAccountID); // 调用归还账号函数
            break;
        case 5:
            printf("【根据id删除账号】\n");
            int deleteAccountID;
            printf("请输入要删除的账号ID:");
            scanf("%d", &deleteAccountID);
            deleteAccount(deleteAccountID); // 调用删除账号函数
            break;
        case 6:
            printf_s("【根据id修改账号】\n");
            int accountID1;
            printf("请输入要修改的账号ID:");
            scanf("%d", &accountID1);
            modifyAccount(accountID1); // 调用修改账号信息函数
            break;
        case 7: {
            printf_s("【查找账号】\n");
            int searchID;
            printf("请输入要查找的账号ID:");
            scanf("%d", &searchID);
            Node* foundAccount = findAccountByID(searchID);
            if (foundAccount != NULL) {
                printf("找到账号信息:\n");
                printf("账号ID:%d\n", foundAccount->playData.id);
                printf("账号:%s\n", foundAccount->playData.account);
                printf("密码:%s\n", foundAccount->playData.password);
                printf("游戏:%s\n", foundAccount->playData.game);
                printf("租借状态:%s\n", foundAccount->playData.rented ? "已租借" : "未租借");
                printf("租借时间:%ld\n", foundAccount->playData.rentTime);
            }
            else {
                printf("找不到账号ID为%d的账号\n", searchID);
            }
            break;
        }
        case 8:
            printf_s("【查看剩余可租账号】\n");
            for (int i = 0; i < HASH_SIZE; i++) {
                Node* current = hashTable[i];
                while (current != NULL) {
                    if (!current->playData.rented) {
                        printf("账号ID:%d\n", current->playData.id);
                        printf("账号:%s\n", current->playData.account);
                        printf("密码:%s\n", current->playData.password);
                        printf("游戏:%s\n", current->playData.game);
                        printf("租借状态:未租借\n");
                        printf("\n");
                    }
                    current = current->next;
                }
            }
            break;
        case 9:
            printf_s("【从文件读取账号信息并插入哈希表】\n");
            readAccountsFromFile();
            break;

        case 10:
            printf_s("【将所有账号信息写入文件】\n");
            writeAccountsToFile();
            break;

        default:
            printf_s("【erorr】\n");
            break;
        }
        printf("是否需要继续“租号” 1/0 (1为继续,0为退出)\n");
        scanf_s("%d", &userput);
        if (userput == 0) {
            flag = false;
        }
        else {
            printf("请继续输入操作:\n");
        }
    }
    freeList(head); // 释放链表内存
    freeHashTable(); // 释放哈希表内存
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值