242424

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// 定义用户结构体

typedef struct {

    int id;

    char username[50];

    char password[50];

    char phone[50];

    char email[50];

} User;

// 定义二叉排序树节点结构体

typedef struct TreeNode {

    User user;

    struct TreeNode* left;

    struct TreeNode* right;

} TreeNode;

// 创建二叉排序树节点

TreeNode* createNode(User user) {

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

    newNode->user = user;

    newNode->left = NULL;

    newNode->right = NULL;

    return newNode;

}

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

TreeNode* insertNode(TreeNode* root, User user) {

    if (root == NULL) {

        return createNode(user);

    }

    int compare = strcmp(user.username, root->user.username);

    if (compare < 0) {

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

    } else if (compare > 0) {

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

    }

    return root;

}

// 在二叉排序树中查找节点

TreeNode* findNode(TreeNode* root, const char* username) {

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

        return root;

    }

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

        return findNode(root->left, username);

    } else {

        return findNode(root->right, username);

    }

}

// 中序遍历二叉排序树,并按照用户名排序打印输出

void inorderTraversal(TreeNode* root) {

    if (root != NULL) {

        inorderTraversal(root->left);

        printf("ID: %d, Username: %s, Password: %s, Phone: %s, Email: %s\n",

               root->user.id, root->user.username, root->user.password,

               root->user.phone, root->user.email);

        inorderTraversal(root->right);

    }

}

// 保存用户信息到.csv文件

void saveToFile(TreeNode* root, FILE* file) {

    if (root != NULL) {

        saveToFile(root->left, file);

        fprintf(file, "%d,%s,%s,%s,%s\n", root->user.id, root->user.username,

                root->user.password, root->user.phone, root->user.email);

        saveToFile(root->right, file);

    }

}

// 模拟从.csv文件读取用户信息

TreeNode* readFromFile() {

    // 这里使用一个简单的示例用户数据

    User users[] = {

        {1, "user1", "pass1", "123456789", "user1@example.com"},

        {2, "user2", "pass2", "987654321", "user2@example.com"},

        {3, "user3", "pass3", "111222333", "user3@example.com"},

        {4, "user4", "pass4", "444555666", "user4@example.com"},

        {5, "user5", "pass5", "777888999", "user5@example.com"}

    };

    int numUsers = sizeof(users) / sizeof(users[0]);

    TreeNode* root = NULL;

    for (int i = 0; i < numUsers; i++) {

        root = insertNode(root, users[i]);

    }

    return root;

}

// 释放二叉排序树内存

void freeTree(TreeNode* root) {

    if (root != NULL) {

        freeTree(root->left);

        freeTree(root->right);

        free(root);

    }

}

int main() {

    TreeNode* root = readFromFile();

    int choice;

    char username[50];

    User user;

    do {

        printf("\nUser Management System\n");

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

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

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

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

        printf("5. Print all users\n");

        printf("6. Save to file\n");

        printf("7. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

        switch (choice) {

            case 1:

                printf("Enter username: ");

                scanf("%s", username);

                TreeNode* foundNode = findNode(root, username);

                if (foundNode != NULL) {

                    printf("User found:\n");

                    printf("ID: %d, Username: %s, Password: %s, Phone: %s, Email: %s\n",

                           foundNode->user.id, foundNode->user.username,

                           foundNode->user.password, foundNode->user.phone,

                           foundNode->user.email);

                } else {

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

                }

                break;

            case 2:

                printf("Enter user ID: ");

                scanf("%d", &user.id);

                printf("Enter username: ");

                scanf("%s", user.username);

                printf("Enter password: ");

                scanf("%s", user.password);

                printf("Enter phone: ");

                scanf("%s", user.phone);

                printf("Enter email: ");

                scanf("%s", user.email);

                root = insertNode(root, user);

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

                break;

            case 3:

                printf("Enter username: ");

                scanf("%s", username);

                TreeNode* deleteNode = findNode(root, username);

                if (deleteNode != NULL) {

                    root = deleteNode;

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

                } else {

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

                }

                break;

            case 4:

                printf("Enter username: ");

                scanf("%s", username);

                TreeNode* modifyNode = findNode(root, username);

                if (modifyNode != NULL) {

                    printf("Enter new password: ");

                    scanf("%s", modifyNode->user.password);

                    printf("Enter new phone: ");

                    scanf("%s", modifyNode->user.phone);

                    printf("Enter new email: ");

                    scanf("%s", modifyNode->user.email);

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

                } else {

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

                }

                break;

            case 5:

                printf("All users:\n");

                inorderTraversal(root);

                break;

            case 6:

                // 模拟保存到.csv文件

                printf("Saving to file...\n");

                FILE* file = fopen("users.csv", "w");

               if (file != NULL) {

                    saveToFile(root, file);

                    fclose(file);

                    printf("Saved to file successfully.\n");

                } else {

                    printf("Failed to save to file.\n");

                }

                break;

            case 7:

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

                break;

            default:

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

        }

    } while (choice != 7);

    freeTree(root);

    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值