用指针,结构体,文件,链表的知识写一个游戏注册登陆的C语言代码
时间: 2024-04-10 08:34:09 浏览: 27
以下是一个简单的游戏注册登录的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_USERNAME_LENGTH 20
#define MAX_PASSWORD_LENGTH 20
typedef struct {
char username[MAX_USERNAME_LENGTH];
char password[MAX_PASSWORD_LENGTH];
} User;
typedef struct Node {
User user;
struct Node* next;
} Node;
Node* createUserNode(User user) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->user = user;
newNode->next = NULL;
return newNode;
}
void addUser(Node** userList, User user) {
Node* newNode = createUserNode(user);
if (*userList == NULL) {
*userList = newNode;
} else {
Node* current = *userList;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
int checkCredentials(Node* userList, char* username, char* password) {
Node* current = userList;
while (current != NULL) {
if (strcmp(current->user.username, username) == 0 &&
strcmp(current->user.password, password) == 0) {
return 1; // 登录成功
}
current = current->next;
}
return 0; // 登录失败
}
void saveUsersToFile(Node* userList, char* filename) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("无法打开文件 %s\n", filename);
return;
}
Node* current = userList;
while (current != NULL) {
fprintf(file, "%s %s\n", current->user.username, current->user.password);
current = current->next;
}
fclose(file);
}
Node* loadUsersFromFile(char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("无法打开文件 %s\n", filename);
return NULL;
}
Node* userList = NULL;
char username[MAX_USERNAME_LENGTH];
char password[MAX_PASSWORD_LENGTH];
while (fscanf(file, "%s %s", username, password) == 2) {
User user;
strcpy(user.username, username);
strcpy(user.password, password);
addUser(&userList, user);
}
fclose(file);
return userList;
}
void freeUserList(Node* userList) {
Node* current = userList;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
}
int main() {
Node* userList = NULL;
// 从文件加载用户列表
userList = loadUsersFromFile("users.txt");
int choice;
do {
printf("1. 注册\n");
printf("2. 登录\n");
printf("3. 退出\n");
printf("请选择: ");
scanf("%d", &choice);
if (choice == 1) {
User newUser;
printf("请输入用户名: ");
scanf("%s", newUser.username);
printf("请输入密码: ");
scanf("%s", newUser.password);
addUser(&userList, newUser);
saveUsersToFile(userList, "users.txt");
printf("注册成功!\n\n");
} else if (choice == 2) {
char username[MAX_USERNAME_LENGTH];
char password[MAX_PASSWORD_LENGTH];
printf("请输入用户名: ");
scanf("%s", username);
printf("请输入密码: ");
scanf("%s", password);
if (checkCredentials(userList, username, password)) {
printf("登录成功!\n\n");
} else {
printf("用户名或密码错误!\n\n");
}
}
} while (choice != 3);
// 释放内存并保存用户列表到文件
freeUserList(userList);
saveUsersToFile(userList, "users.txt");
return 0;
}
```
这段代码使用了指针、结构体、文件操作和链表等知识,实现了一个简单的游戏注册登录功能。用户可以选择注册新账号或登录已有账号,用户名和密码会保存在一个链表中,并可以将用户列表保存到文件中。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)