-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstudent.c
239 lines (196 loc) · 6.42 KB
/
student.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// 实现学生信息的增删改查功能。
// 读取和保存学生信息到文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "globals.h"
#include "utils.h"
#include "student.h"
void manageStudent() {
freeStudentList();
loadStudentsFromFile("students.txt");
int choice;
do {
printf("学生信息管理系统\n");
printf("1. 增加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 查看学生信息\n");
printf("5. 退出系统\n");
printf("请选择(1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
updateStudent();
break;
case 4:
viewStudents();
break;
case 5:
freeStudentList();
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}while (choice!= 5);
}
void loadStudentsFromFile(const char* filename) {
freeStudentList();
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件:%s\n", filename);
return;
}
char line[1024];
while (fgets(line,sizeof(line),fp)){
Student* student = (Student*)malloc(sizeof(Student));
if (student == NULL) {
printf("内存分配失败!\n");
fclose(fp);
return;
}
sscanf(line, "%[^,],%[^,],%[^,],%[^,],%[^,],%s",student->studentID,student->studentName,student->gender,student->birthDate,student->phone,student->email);
//插入到链表头
student->next = studentHead;
studentHead = student;
}
fclose(fp);
printf("学生信息加载成功!\n");
}
void freeStudentList(){
Student* curr = studentHead;
while (curr != NULL){
Student* temp = curr;
curr = curr->next;
free(temp);
}
studentHead = NULL;
}
void printStudentList() {
Student* curr = studentHead;
while (curr != NULL) {
printf("学号: %s, 姓名: %s, 性别: %s, 出生日期: %s, 电话: %s, 邮箱: %s\n",
curr->studentID, curr->studentName, curr->gender, curr->birthDate, curr->phone, curr->email);
curr = curr->next;
}
}
void addStudent() {
Student* newStudent = (Student*)malloc(sizeof(Student));
if (newStudent == NULL) {
printf("内存分配失败!\n");
return;
}
printf("请输入学号: ");
scanf("%s", newStudent->studentID);
printf("请输入姓名: ");
scanf("%s", newStudent->studentName);
printf("请输入性别: ");
scanf("%s", newStudent->gender);
printf("请输入出生年月: ");
scanf("%s", newStudent->birthDate);
printf("请输入电话: ");
scanf("%s", newStudent->phone);
printf("请输入E-mail: ");
scanf("%s", newStudent->email);
// 保存学生信息到文件
FILE* fp = fopen("students.txt", "a+");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
fprintf(fp, "%s,%s,%s,%s,%s,%s\n", newStudent->studentID, newStudent->studentName, newStudent->gender, newStudent->birthDate, newStudent->phone, newStudent->email);
newStudent -> next = studentHead;
studentHead = newStudent;
printf("学生信息添加成功!\n");
fclose(fp);
}
void deleteStudent() {
char studentID[20];
printf("请输入要删除的学生学号: ");
scanf("%s", studentID);
// 遍历链表,找到要删除的学生信息
Student* prev = NULL;
Student* curr = studentHead;
while (curr!= NULL) {
if (strcmp(curr->studentID, studentID) == 0) {
// 找到了要删除的学生信息
if (prev == NULL) {
// 要删除的学生信息是链表头部
studentHead = curr->next;
} else {
// 要删除的学生信息不是链表头部
prev->next = curr->next;
}
//删除文件中的信息
removeContent("students.txt", studentID);
printf("学生信息删除成功!\n");
// 释放内存
return;
}
prev = curr;
curr = curr->next;
}
printf("没有找到要删除的学生信息!\n");
}
void updateStudent() {
char studentID[20];
printf("请输入要修改的学生学号: ");
scanf("%s", studentID);
// 遍历链表,找到要修改的学生信息
Student* curr = studentHead;
while (curr!= NULL) {
if (strcmp(curr->studentID, studentID) == 0) {
printf("请输入新的学生信息:\n");
printf("姓名: ");
scanf("%s", curr->studentName);
printf("性别: ");
scanf("%s", curr->gender);
printf("出生年月: ");
scanf("%s", curr->birthDate);
printf("电话: ");
scanf("%s", curr->phone);
printf("E-mail: ");
scanf("%s", curr->email);
//生成新内容
char* newContent = createContent(curr,NULL,NULL);
if (newContent == NULL) {
printf("内存分配失败!\n");
return;
}
// 保存修改后的信息到文件
editContent("students.txt",curr->studentID,newContent);
printf("学生信息修改成功!\n");
free(newContent);
return;
}
curr = curr->next;
}
printf("没有找到要修改的学生信息!\n");
}
void viewStudents() {
char studentID[20];
printf("请输入要查看的学生学号: ");
scanf("%s", studentID);
// 遍历链表,找到要查看的学生信息
Student* curr = studentHead;
while (curr != NULL) {
if (strcmp(curr->studentID, studentID) == 0) {
printf("学号: %s\n", curr->studentID);
printf("姓名: %s\n", curr->studentName);
printf("性别: %s\n", curr->gender);
printf("出生年月: %s\n", curr->birthDate);
printf("电话: %s\n", curr->phone);
printf("E-mail: %s\n", curr->email);
return;
}
curr = curr->next;
}
printf("没有找到要查看的学生信息!\n");
}