252525

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// 用户结构体

typedef struct {

    int id;

    char username[50];

    char password[50];

    char phone[20];

    char email[50];

} User;

// 二叉排序树结点结构体

typedef struct Node {

    User user;

    struct Node* left;

    struct Node* right;

} Node;

// 创建新的结点

Node* createNode(User user) {

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

    newNode->user = user;

    newNode->left = NULL;

    newNode->right = NULL;

    return newNode;

}

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

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

    if (root == NULL) {

        root = createNode(user);

    } else {

        // 根据用户名比较大小决定插入位置

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

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

        } else {

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

        }

    }

    return root;

}

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

Node* searchNode(Node* root, char* username) {

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

        return root;

    }

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

        return searchNode(root->left, username);

    }

    return searchNode(root->right, username);

}

// 删除二叉排序树中的结点

Node* deleteNode(Node* root, char* username) {

    if (root == NULL) {

        return root;

    }

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

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

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

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

    } else {

        // 找到要删除的结点

        // 结点没有子节点或只有一个子节点

        if (root->left == NULL) {

            Node* temp = root->right;

            free(root);

            return temp;

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

            Node* temp = root->left;

            free(root);

            return temp;

        }

        // 结点有两个子节点

        Node* minNode = root->right;

        while (minNode->left != NULL) {

            minNode = minNode->left;

        }

        root->user = minNode->user;

        root->right = deleteNode(root->right, minNode->user.username);

    }

    return root;

}

// 修改二叉排序树中的结点

void modifyNode(Node* root, char* username, User user) {

    Node* node = searchNode(root, username);

    if (node != NULL) {

        node->user = user;

    }

}

// 中序遍历二叉排序树,打印用户信息

void inorderTraversal(Node* 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(FILE* file, Node* root) {

    if (root != NULL) {

        saveToFile(file, root->left);

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

        saveToFile(file, root->right);

    }

}

// 从.csv文件中加载用户信息到二叉排序树

Node* loadFromFile(FILE* file, Node* root) {

    char line[256];

    while (fgets(line, sizeof(line), file)) {

        User user;

        sscanf(line, "%d,%[^,],%[^,],%[^,],%[^,\n]", &user.id, user.username, user.password, user.phone, user.email);

        root = insertNode(root, user);

    }

    return root;

}

int main() {

    Node* root = NULL;

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

    if (file != NULL) {

        root = loadFromFile(file, root);

        fclose(file);

    }

    int choice;

    do {

        printf("\n------ 用户信息管理系统 ------\n");

        printf("1. 查找用户信息\n");

        printf("2. 新增用户信息\n");

        printf("3. 删除用户信息\n");

        printf("4. 修改用户信息\n");

        printf("5. 打印用户信息\n");

        printf("6. 保存用户信息到文件\n");

        printf("0. 退出\n");

        printf("请输入您的选择:");

        scanf("%d", &choice);

        switch (choice) {

            case 0:

                printf("感谢使用用户信息管理系统,再见!\n");

                break;

            case 1: {

                char username[50];

                printf("请输入要查找的用户名:");

                scanf("%s", username);

                Node* node = searchNode(root, username);

                if (node != NULL) {

                    printf("用户信息找到:\n");

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

                           node->user.id, node->user.username, node->user.password, node->user.phone, node->user.email);

                } else {

                    printf("未找到该用户名的用户信息。\n");

                }

                break;

            }

            case 2: {

                User user;

                printf("请输入新用户的ID:");

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

                printf("请输入新用户的用户名:");

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

                printf("请输入新用户的密码:");

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

                printf("请输入新用户的电话:");

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

                printf("请输入新用户的邮箱:");

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

                root = insertNode(root, user);

                printf("用户信息已添加。\n");

                break;

            }

            case 3: {

                char username[50];

                printf("请输入要删除的用户名:");

                scanf("%s", username);

                root = deleteNode(root, username);

                printf("用户信息已删除。\n");

                break;

            }

            case 4: {

                char username[50];

                printf("请输入要修改的用户名:");

               scanf("%s", username);

                Node* node = searchNode(root, username);

                if (node != NULL) {

                    User user;

                    printf("请输入新的ID:");

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

                    printf("请输入新的用户名:");

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

                    printf("请输入新的密码:");

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

                    printf("请输入新的电话:");

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

                    printf("请输入新的邮箱:");

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

                    modifyNode(root, username, user);

                    printf("用户信息已修改。\n");

                } else {

                    printf("未找到该用户名的用户信息。\n");

                }

                break;

            }

            case 5:

                printf("所有用户信息:\n");

                inorderTraversal(root);

                break;

            case 6: {

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

                if (file != NULL) {

                    saveToFile(file, root);

                    fclose(file);

                    printf("用户信息已保存到文件。\n");

                } else {

                    printf("保存文件失败。\n");

                }

                break;

            }

            default:

                printf("无效的选择,请重新输入。\n");

        }

    } while (choice != 0);

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
帮我优化以下代码user_list = [ {'user': 'zhangsan', 'password': '123456', 'balance': 1000}, {'user': 'lisi', 'password': '111111', 'balance': 2500}, {'user': 'wangwu', 'password': '252525', 'balance': 100} ] current_user = None # 用于记录当前登陆用户信息的全局变量 def reg(): while True: un = input('请输入您的用户名:【注册】') for item in user_list: if un == item['user']: print('用户已存在,请检查') break # break 出for循环 else: pw = input('请输入您的密码:【注册】') if len(pw) < 6: print('密码长度小于6位,请检查') else: # 用户注册成功 user_list.append({'user': un, 'password': pw, 'balance': 3000}) print('恭喜您注册成功!') return True # 退出整个函数 def login(): while True: un = input('请输入您的用户名:【登陆】') pw = input('请输入您的密码:【登陆】') for user in user_list: if user['user'] == un and user['password'] == pw: # 这就说明用户名和密码输入正确 print('恭喜您登陆成功!') global current_user current_user = user return else: print('用户名或密码错误!') def check_balance(): if current_user: # 代表当前已经登陆 print('当前用户的余额为:', current_user['balance']) else: print('请先登陆后再进行查询余额操作!') def depoisit(): if current_user: money = int(input('请输入您要存款的金额:')) if money[-2:] == '00' and len(money) > 2: # 说明输入的是100的整数倍 current_user['balance'] += int(money) print('恭喜存款成功,当前余额为:', current_user['balance']) else: print('您的存款金额格式不正确,请检查后再操作') else: print('您尚未登陆,请登陆后再进行相关操作!') def withdraw(): if current_user: money = input('请输入您要取款的金额:') if money[-2:] == '00' and len(money) > 2: # 说明输入的是100的整数倍 if current_user['balance'] >= int(money): current_user['balance'] -= int(money) print('恭喜您取款成功,当前余额为:', current_user['balance']) else: print('您的余额不足!') else: print('您的存款金额格式不正确,请检查后再操作') else: print('您尚未登陆,请登陆后再进行相关操作!') def get_menu(): menu = ''' ******欢迎来到WoniuATM******* *********请选择操作菜单********* *****1. 注册 2. 登录 3. 查询余额 4. 存款 5. 取款 6.计算复利 7.取卡 *** ''' while True: print(menu) option = input('请输入您要操作的菜单:') if option == '1': reg() elif option == '2': login() elif option == '3': check_balance() elif option == '4': depoisit() elif option == '5': withdraw() elif option == '6': withdraw() elif option == '7': print('感谢您的使用,欢迎下次再来!') break else: print('选择菜单项错误,请重新选择!') get_menu()
02-07

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值