#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 用户结构体
typedef struct User {
int id;
char username[50];
char password[50];
char phone[20];
char email[50];
struct User* left;
struct User* right;
} User;
// 创建用户节点
User* createNode(int id, const char* username, const char* password, const char* phone, const char* email) {
User* newNode = (User*)malloc(sizeof(User));
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;
}
// 插入用户节点到二叉排序树
User* insertUser(User* root, User* node) {
if (root == NULL) {
return node;
}
// 比较用户名的拼音或英文首字母
int cmp = strcmp(node->username, root->username);
if (cmp < 0 || (cmp == 0 && strcmp(node->username, root->username) < 0)) {
root->left = insertUser(root->left, node);
} else {
root->right = insertUser(root->right, node);
}
return root;
}
// 在二叉排序树中查找用户
User* searchUser(User* root, const char* username) {
if (root == NULL || strcmp(username, root->username) == 0) {
return root;
}
// 比较用户名的拼音或英文首字母
int cmp = strcmp(username, root->username);
if (cmp < 0 || (cmp == 0 && strcmp(username, root->username) < 0)) {
return searchUser(root->left, username);
} else {
return searchUser(root->right, username);
}
}
// 删除二叉排序树中的节点
User* deleteUser(User* root, const char* username) {
if (root == NULL) {
return root;
}
// 比较用户名的拼音或英文首字母
int cmp = strcmp(username, root->username);
if (cmp < 0 || (cmp == 0 && strcmp(username, root->username) < 0)) {
root->left = deleteUser(root->left, username);
} else if (cmp > 0 || (cmp == 0 && strcmp(username, root->username) > 0)) {
root->right = deleteUser(root->right, username);
} else {
if (root->left == NULL) {
User* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
User* temp = root->left;
free(root);
return temp;
}
User* temp = root->right;
while (temp->left != NULL) {
temp = temp->left;
}
strcpy(root->username, temp->username);
strcpy(root->password, temp->password);
strcpy(root->phone, temp->phone);
strcpy(root->email, temp->email);
root->right = deleteUser(root->right, temp->username);
}
return root;
}
// 中序遍历二叉排序树,并保存到文件
void saveUsersToFile(User* root, FILE* file) {
if (root == NULL) {
return;
}
saveUsersToFile(root->left, file);
fprintf(file, "%d,%s,%s,%s,%s\n", root->id, root->username, root->password, root->phone, root->email);
saveUsersToFile(root->right, file);
}
// 中序遍历二叉排序树,并打印用户信息
void printUsers(User* root) {
if (root == NULL) {
return;
}
printUsers(root->left);
printf("ID: %d\n", root->id);
printf("Username: %s\n", root->username);
printf("Password: %s\n", root->password);
printf("Phone: %s\n", root->phone);
printf("Email: %s\n", root->email);
printf("--------------------\n");
printUsers(root->right);
}
int main() {
User* root = NULL;
FILE* file = fopen("users.csv", "r");
if (file != NULL) {
char line[256];
while (fgets(line, sizeof(line), file)) {
// 解析CSV行数据
char* token = strtok(line, ",");
int id = atoi(token);
token = strtok(NULL, ",");
char username[50];
strcpy(username, token);
token = strtok(NULL, ",");
char password[50];
strcpy(password, token);
token = strtok(NULL, ",");
char phone[20];
strcpy(phone, token);
token = strtok(NULL, ",");
char email[50];
strcpy(email, token);
// 创建用户节点
User* newNode = createNode(id, username, password, phone, email);
// 插入用户节点到二叉排序树
root = insertUser(root, newNode);
}
fclose(file);
}
int choice;
while (1) {
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);
if (choice == 0) {
break;
}
switch (choice) {
case 1: {
char username[50];
printf("请输入要查找的用户名: ");
scanf("%s", username);
// 在二叉排序树中查找用户
User* user = searchUser(root, username);
if (user != NULL) {
printf("用户信息:\n");
printf("ID: %d\n", user->id);
printf("Username: %s\n", user->username);
printf("Password: %s\n", user->password);
printf("Phone: %s\n", user->phone);
printf("Email: %s\n", user->email);
} else {
printf("找不到该用户。\n");
}
break;
}
case 2: {
int id;
char username[50];
char password[50];
char phone[20];
char email[50];
printf("请输入用户ID: ");
scanf("%d", &id);
printf("请输入用户名: ");
scanf("%s", username);
printf("请输入密码: ");
scanf("%s", password);
printf("请输入电话号码: ");
scanf("%s", phone);
printf("请输入邮箱: ");
scanf("%s", email);
// 创建用户节点
User* newNode = createNode(id, username, password, phone, email);
// 插入用户节点到二叉排序树
root = insertUser(root, newNode);
printf("用户信息已新增。\n");
break;
}
case 3: {
char username[50];
printf("请输入要删除的用户名: ");
scanf("%s", username);
// 删除二叉排序树中的节点
root = deleteUser(root, username);
printf("用户信息已删除。\n");
break;
}
case 4: {
char username[50];
printf("请输入要修改的用户名: ");
scanf("%s", username);
// 在二叉排序树中查找用户
User* user = searchUser(root, username);
if (user != NULL) {
int id;
char newUsername[50];
char newPassword[50];
char newPhone[20];
char newEmail[50];
printf("请输入新的用户ID: ");
scanf("%d", &id);
printf("请输入新的用户名: ");
scanf("%s", newUsername);
printf("请输入新的密码: ");
scanf("%s", newPassword);
printf("请输入新的电话号码: ");
scanf("%s", newPhone);
printf("请输入新的邮箱: ");
scanf("%s", newEmail);
// 更新用户信息
user->id = id;
strcpy(user->username, newUsername);
strcpy(user->password, newPassword);
strcpy(user->phone, newPhone);
strcpy(user->email, newEmail);
printf("用户信息已修改。\n");
} else {
printf("找不到该用户。\n");
}
break;
}
case 5: {
FILE* file = fopen("users.csv", "w");
if (file != NULL) {
// 中序遍历二叉排序树,并保存到文件
saveUsersToFile(root, file);
fclose(file);
printf("用户信息已保存到文件。\n");
} else {
printf("无法打开文件。\n");
}
break;
}
case 6: {
printf("用户信息:\n");
// 中序遍历二叉排序树,并打印用户信息
printUsers(root);
break;
}
default:
printf("无效的选项。\n");
}
printf("\n");
}
// 释放二叉排序树内存
// 在这个简化的示例中,没有手动释放内存,程序结束后会由操作系统回收分配的内存
return 0;
}