-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
290 lines (243 loc) · 6.93 KB
/
main.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* main.c
* This file is part of Chengjiguanli
*
* Copyright (C) 2010 - Kunshan Wang
*
* Chengjiguanli is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Chengjiguanli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chengjiguanli; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedlist.h"
#include "model.h"
struct MainContext main_context;
void print_everything(struct MainContext *context) {
struct StudentInfo *student_info;
struct ListNode *ptr;
int index;
printf("*******列表********\n");
index=0;
list_for_each(ptr, &context->student_infos.node) {
student_info = (struct StudentInfo*) ptr;
printf(
"序号: [%d]\t"
"姓名: [%s]\t"
"性别: [%s]\n"
, index
, student_info->name
, student_info->sex==SEX_MALE ? "男" : "女"
);
index++;
}
printf("*****列表结束******\n");
}
void add_student(struct MainContext *context) {
char buffer[256];
char name[256];
enum Sex sex;
struct StudentInfo *student_info;
do {
printf("姓名:");
fgets(buffer, 256, stdin);
} while (strlen(buffer) == 0);
{
char *nl;
nl = strchr(buffer, '\n');
if(nl != NULL) {
*nl = '\0';
}
}
strcpy(name, buffer);
do {
printf("性别[m/f]:");
fgets(buffer, 256, stdin);
switch(buffer[0]) {
case 'm':
sex = SEX_MALE;
break;
case 'f':
sex = SEX_FEMALE;
break;
default:
continue;
}
} while (strlen(buffer) == 0);
student_info = student_info_new(name, sex);
list_insert(&context->student_infos.node, &student_info->node);
printf("完成\n");
}
void del_student(struct MainContext *context) {
char buffer[256];
struct StudentInfo *student_info;
struct ListNode *ptr;
int index;
do {
printf("序号:");
fgets(buffer, 256, stdin);
} while (sscanf(buffer, "%d", &index) != 1);
ptr = list_delete(&context->student_infos.node, index);
student_info = (struct StudentInfo*) ptr;
student_info_free(student_info);
printf("完成\n");
}
void print_student_info(struct StudentInfo *student_info) {
struct ScoreInfo *score_info;
struct ListNode *ptr;
int index;
printf("*******学生********\n");
printf(
"姓名: [%s]\n"
"性别: [%s]\n"
, student_info->name
, student_info->sex==SEX_MALE ? "男" : "女"
);
index=0;
list_for_each(ptr, &student_info->score_infos.node) {
score_info = (struct ScoreInfo*) ptr;
printf(
"序号: [%d]\t"
"科目: [%s]\t"
"分数: [%lf]\n"
, index
, score_info->course_name
, score_info->score
);
index++;
}
printf("*****学生结束******\n");
}
void add_score(struct StudentInfo *student_info) {
char buffer[256];
char course_name[256];
double score;
struct ScoreInfo *score_info;
do {
printf("科目:");
fgets(buffer, 256, stdin);
} while (strlen(buffer) == 0);
{
char *nl;
nl = strchr(buffer, '\n');
if(nl != NULL) {
*nl = '\0';
}
}
strcpy(course_name, buffer);
do {
printf("分数:");
fgets(buffer, 256, stdin);
} while (sscanf(buffer, "%lf", &score) != 1);
score_info = score_info_new(course_name, score);
list_insert(&student_info->score_infos.node, &score_info->node);
printf("完成\n");
}
void del_score(struct StudentInfo *student_info) {
char buffer[256];
struct ScoreInfo *score_info;
struct ListNode *ptr;
int index;
do {
printf("序号:");
fgets(buffer, 256, stdin);
} while (sscanf(buffer, "%d", &index) != 1);
ptr = list_delete(&student_info->score_infos.node, index);
score_info = (struct ScoreInfo*) ptr;
score_info_free(score_info);
printf("完成\n");
}
void edit_student_detail(struct StudentInfo *student_info) {
while(1) {
char buffer[256];
int selection;
do {
printf(
"学生\n"
"1. 信息\n"
"2. 添加分数\n"
"3. 删除分数\n"
"0. 返回\n"
);
fgets(buffer, 256, stdin);
} while (sscanf(buffer, "%d", &selection) != 1);
switch(selection) {
case 1:
print_student_info(student_info);
break;
case 2:
add_score(student_info);
break;
case 3:
del_score(student_info);
break;
case 0:
return;
}
}
}
void edit_student(struct MainContext *context) {
char buffer[256];
struct StudentInfo *student_info;
struct ListNode *ptr;
int index;
do {
printf("序号:");
fgets(buffer, 256, stdin);
} while (sscanf(buffer, "%d", &index) != 1);
ptr = context->student_infos.node.next;
while(index--) {
ptr = ptr->next;
}
student_info = (struct StudentInfo*) ptr;
edit_student_detail(student_info);
printf("完成\n");
}
int main() {
main_context_init(&main_context);
printf("*** 学生管理系统 ***\n");
while(1) {
char buffer[256];
int selection;
do {
printf(
"主菜单\n"
"1. 列表\n"
"2. 添加学生\n"
"3. 修改学生\n"
"4. 删除学生\n"
"0. 退出\n"
);
fgets(buffer, 256, stdin);
} while (sscanf(buffer, "%d", &selection) != 1);
switch(selection) {
case 1:
print_everything(&main_context);
break;
case 2:
add_student(&main_context);
break;
case 3:
edit_student(&main_context);
break;
case 4:
del_student(&main_context);
break;
case 0:
exit(0);
}
}
return 0;
}