232323

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// 结构体定义:用户信息

typedef struct UserInfo {

    int id;

    char username[50];

    char password[50];

    char phone[20];

    char email[50];

    struct UserInfo* left;

    struct UserInfo* right;

} UserInfo;

// 函数声明

UserInfo* createNode(int id, const char* username, const char* password, const char* phone, const char* email);

UserInfo* insertNode(UserInfo* root, UserInfo* node);

UserInfo* searchNode(UserInfo* root, const char* username);

UserInfo* deleteNode(UserInfo* root, const char* username);

UserInfo* findMinimumNode(UserInfo* root);

void printInorder(UserInfo* root);

void saveToCsv(UserInfo* root, FILE* file);

UserInfo* loadFromCsv(UserInfo* root, FILE* file);

void freeTree(UserInfo* root);

// 创建用户信息节点

UserInfo* createNode(int id, const char* username, const char* password, const char* phone, const char* email) {

    UserInfo* newNode = (UserInfo*)malloc(sizeof(UserInfo));

    newNode->id = id;

    strcpy(newNode->username, username);

    strcpy(newNode->password, password);

    strcpy(newNode->phone, phone);

    strcpy(newNode->email, email);

    newNode->left = NULL;

    newNode->right = NULL;

    return newNode;

}

// 插入节点到二叉排序树

UserInfo* insertNode(UserInfo* root, UserInfo* node) {

    if (root == NULL) {

        return node;

    }

    if (strcmp(node->username, root->username) < 0) {

        root->left = insertNode(root->left, node);

    } else {

        root->right = insertNode(root->right, node);

    }

    return root;

}

// 查找节点

UserInfo* searchNode(UserInfo* root, const char* username) {

    if (root == NULL || strcmp(username, root->username) == 0) {

        return root;

    }

    if (strcmp(username, root->username) < 0) {

        return searchNode(root->left, username);

    } else {

        return searchNode(root->right, username);

    }

}

// 删除节点

UserInfo* deleteNode(UserInfo* root, const char* username) {

    if (root == NULL) {

        return root;

    }

    if (strcmp(username, root->username) < 0) {

        root->left = deleteNode(root->left, username);

    } else if (strcmp(username, root->username) > 0) {

        root->right = deleteNode(root->right, username);

    } else {

        if (root->left == NULL) {

            UserInfo* temp = root->right;

            free(root);

            return temp;

        } else if (root->right == NULL) {

            UserInfo* temp = root->left;

            free(root);

            return temp;

        }

        UserInfo* minRight = findMinimumNode(root->right);

        strcpy(root->username, minRight->username);

        strcpy(root->password, minRight->password);

        strcpy(root->phone, minRight->phone);

        strcpy(root->email, minRight->email);

        root->right = deleteNode(root->right, minRight->username);

    }

    return root;

}

// 查找最小节点

UserInfo* findMinimumNode(UserInfo* root) {

    if (root->left == NULL) {

        return root;

    }

    return findMinimumNode(root->left);

}

// 中序遍历打印节点信息

void printInorder(UserInfo* root) {

    if (root == NULL) {

        return;

    }

    printInorder(root->left);

    printf("ID: %d, Username: %s, Password: %s, Phone: %s, Email: %s\n", root->id, root->username, root->password, root->phone, root->email);

    printInorder(root->right);

}

// 保存节点信息到.csv文件

void saveToCsv(UserInfo* root, FILE* file) {

    if (root == NULL) {

        return;

    }

    saveToCsv(root->left, file);

    fprintf(file, "%d,%s,%s,%s,%s\n", root->id, root->username, root->password, root->phone, root->email);

    saveToCsv(root->right, file);

}

// 从.csv文件加载节点信息

UserInfo* loadFromCsv(UserInfo* root, FILE* file) {

    int id;

    char username[50];

    char password[50];

    char phone[20];

    char email[50];

    while (fscanf(file, "%d,%[^,],%[^,],%[^,],%[^\n]\n", &id, username, password, phone, email) == 5) {

        UserInfo* newNode = createNode(id, username, password, phone, email);

        root = insertNode(root, newNode);

    }

    return root;

}

// 释放二叉排序树节点

void freeTree(UserInfo* root) {

    if (root == NULL) {

        return;

    }

    freeTree(root->left);

    freeTree(root->right);

    free(root);

}

int main() {

    UserInfo* root = NULL;

    int choice;

    char username[50];

    while (1) {

        printf("\n----- User Management System -----\n");

        printf("1. Search user\n");

        printf("2. Add user\n");

        printf("3. Delete user\n");

        printf("4. Modify user\n");

        printf("5. Save to CSV\n");

        printf("6. Print user list\n");

        printf("7. Load from CSV\n");

        printf("8. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

        switch (choice) {

            case 1:

                printf("Enter username to search: ");

                scanf("%s", username);

                UserInfo* searchResult = searchNode(root, username);

                if (searchResult != NULL) {

                    printf("User found!\n");

                    printf("ID: %d, Username: %s, Password: %s, Phone: %s, Email: %s\n", searchResult->id, searchResult->username, searchResult->password, searchResult->phone, searchResult->email);

                } else {

                    printf("User not found!\n");

                }

                break;

            case 2:

                // Read user information

                int id;

                char password[50];

                char phone[20];

                char email[50];

                printf("Enter user ID: ");

                scanf("%d", &id);

               scanf("%*c"); // Clear the input buffer

                printf("Enter username: ");

                fgets(username, sizeof(username), stdin);

                username[strlen(username) - 1] = '\0'; // Remove newline character

                printf("Enter password: ");

                fgets(password, sizeof(password), stdin);

                password[strlen(password) - 1] = '\0'; // Remove newline character

                printf("Enter phone number: ");

                fgets(phone, sizeof(phone), stdin);

                phone[strlen(phone) - 1] = '\0'; // Remove newline character

                printf("Enter email: ");

                fgets(email, sizeof(email), stdin);

                email[strlen(email) - 1] = '\0'; // Remove newline character

                // Create user node

                UserInfo* newNode = createNode(id, username, password, phone, email);

                // Insert node into the binary search tree

                root = insertNode(root, newNode);

                printf("User added successfully!\n");

                break;

            case 3:

                printf("Enter username to delete: ");

                scanf("%s", username);

                root = deleteNode(root, username);

                printf("User deleted successfully!\n");

                break;

            case 4:

                printf("Enter username to modify: ");

                scanf("%s", username);

                UserInfo* modifyUser = searchNode(root, username);

                if (modifyUser != NULL) {

                    // Read modified user information

                    int newId;

                    char newPassword[50];

                    char newPhone[20];

                    char newEmail[50];

                    printf("Enter new ID: ");

                    scanf("%d", &newId);

                    scanf("%*c"); // Clear the input buffer

                    printf("Enter new password: ");

                    fgets(newPassword, sizeof(newPassword), stdin);

                    newPassword[strlen(newPassword) - 1] = '\0'; // Remove newlicharacter

                    printf("Enter new phone number: ");

                    fgets(newPhone, sizeof(newPhone), stdin);

                    newPhone[strlen(newPhone) - 1] = '\0'; // Remove newline character

                    printf("Enter new email: ");

                    fgets(newEmail, sizeof(newEmail), stdin);

                    newEmail[strlen(newEmail) - 1] = '\0'; // Remove newline character

                    // Update user information

                    modifyUser->id = newId;

                    strcpy(modifyUser->password, newPassword);

                    strcpy(modifyUser->phone, newPhone);

                    strcpy(modifyUser->email, newEmail);

                    printf("User modified successfully!\n");

                } else {

                    printf("User not found!\n");

                }

                break;

            case 5:

                printf("Enter the filename to save (.csv): ");

                scanf("%s", username);

                FILE* saveFile = fopen(username, "w");

                if (saveFile == NULL) {

                    printf("Error opening file!\n");

                } else {

                    saveToCsv(root, saveFile);

                    fclose(saveFile);

                    printf("User information saved to %s successfully!\n", username);

                }

                break;

            case 6:

                printf("User List:\n");

                printInorder(root);

                break;

            case 7:

                printf("Enter the filename to load (.csv): ");

                scanf("%s", username);

                FILE* loadFile = fopen(username, "r");

                if (loadFile == NULL) {

                    printf("Error opening file!\n");

                } else {

                    root = loadFromCsv(root, loadFile);

                    fclose(loadFile);

                    printf("User information loaded from %s successfully!\n", username);

                }

                break;

            case 8:

                freeTree(root);

                printf("Exiting...\n");

                exit(0);

            default:

                printf("Invalid choice! Please try again.\n");

        }

    }

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值