设有一条单链表保存了一个班级信息,其中包含:姓名、学号、密码、年龄、请编写一个登入函数,传入这条链表和学号,然后要求输入密码(输入三次错误则程序结束),如果密码正确则登入成功,登入后可以选择修改姓名或年龄或密码,然后进行保存或退出登入。例如: 密码为123456、abcdef,学生姓名为 张三 、李四,年龄为 20,23,学号为1234,1235。登入后可以选择修改姓名或年龄或密码,然后进行保存或退出登入。
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
// 定义链表节点结构体
typedef struct StudentNode {
char name[50];
int id;
char password[20];
int age;
struct StudentNode* next;
} StudentNode;
// 创建新节点
StudentNode* createNode(const char* name, int id, const char* password, int age) {
StudentNode* newNode = (StudentNode*)malloc(sizeof(StudentNode));
strcpy(newNode->name, name);
newNode->id = id;
strcpy(newNode->password, password);
newNode->age = age;
newNode->next = NULL;
return newNode;
}
// 添加节点到链表
void addNodeToList(StudentNode** head, const char* name, int id, const char* password, int age) {
StudentNode* newNode = createNode(name, id, password, age);
if (*head == NULL) {
*head = newNode;
} else {
StudentNode* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
/*******************************查找指定学号的学生节点****************************/
StudentNode* findNodeById(StudentNode* head, int id) {
while (head != NULL) {
if (head->id == id) {
return head;
}
head = head->next;
}
return NULL;
}
/************************************修改学生信息***********************************/
void modifyStudentInfo(StudentNode* student, const char* newName, int newAge, const char* newPassword) {
if (newName != NULL) strcpy(student->name, newName);
if (newAge >= 0) student->age = newAge;
if (newPassword != NULL) strcpy(student->password, newPassword);
}
/*************************************登录验证*************************************/
bool login(StudentNode* head, int id, const char* inputPassword, int maxAttempts) {
StudentNode* student = findNodeById(head, id);
int attempts = 0;
while (attempts < maxAttempts && student != NULL) {
if (strcmp(student->password, inputPassword) == 0) {
printf("Login successful!\n");
return true;
} else {
printf("Incorrect password, try again.\n");
attempts++;
if (attempts < maxAttempts) {
printf("Enter your password: ");
scanf("%s", inputPassword);
getchar(); // Consume newline character
}
}
}
if (attempts == maxAttempts) {
printf("Too many failed attempts, exiting...\n");
}
return false;
}
int main() {
StudentNode* head = NULL;
addNodeToList(&head, "张三", 1234, "123456", 20);
addNodeToList(&head, "李四", 1235, "abcdef", 23);
int id;
char inputPassword[20];
printf("Enter your student ID: ");
scanf("%d", &id);
getchar(); // Consume newline character
if (login(head, id, inputPassword, 3)) {
// 登录成功后,可进行信息修改操作
printf("You have logged in successfully.\n");
int choice;
do {
printf("\nChoose an option to modify:\n");
printf("1. Name\n");
printf("2. Age\n");
printf("3. Password\n");
printf("4. Exit and Save Changes\n");
scanf("%d", &choice);
getchar(); // Consume newline character
switch (choice) {
case 1: {
printf("Enter your new name: ");
char newName[50];
scanf("%s", newName);
getchar();
modifyStudentInfo(findNodeById(head, id), newName, -1, NULL);
break;
}
case 2: {
int newAge;
printf("Enter your new age: ");
scanf("%d", &newAge);
getchar();
modifyStudentInfo(findNodeById(head, id), NULL, newAge, NULL);
break;
}
case 3: {
printf("Enter your new password: ");
char newPassword[20];
scanf("%s", newPassword);
getchar();
modifyStudentInfo(findNodeById(head, id), NULL, -1, newPassword);
break;
}
case 4:
printf("Information saved. Logging out...\n");
goto exit_and_clean;
break;
default:
printf("Invalid choice. Please choose again.\n");
break;
}
} while (choice != 4);
exit_and_clean:
// 清理链表(这里仅为示例,实际应用可能需要更复杂的内存管理)
StudentNode* temp = head;
while (temp) {
StudentNode* toDelete = temp;
temp = temp->next;
free(toDelete);
}
head = NULL;
}
return 0;
}