本文实例为大家分享了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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#include<stdio.h> #include<windows.h> #include<stdlib.h> #include<conio.h> typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; #define CLASS_CLS system("cls") #define CLASS_NAME 80 typedef struct class { char name[CLASS_NAME]; /* 课程名称 -- 唯一性 */ uint32_t nature; /* 课程性质(必修或者选修) */ uint32_t total_period; /* 课程总学时 */ uint32_t teach_period; /* 授课学时 */ uint32_t exper_period; /* 上机学时 */ uint32_t start_time; /* 课程开始时间 */ uint8_t score; /* 课程学分 */ uint8_t is_exsit; /* 课程是否存在 */ struct class *next; } class_t; // 课程结构体 class_t *head = NULL; static uint32_t count = 1; void play( char *text, int display, int time , int nu) //动画打印 { CLASS_CLS; int i, len; for (i = 0; i <= nu; i++) { printf ( "\n" ); } for (i = 0; i < 25; i++) { printf ( " " ); } len = strlen (text); for (i = 0; i < len; i++) { printf ( "%c" , text[i]); Sleep(display); } Sleep( time ); } void titile( char *text, char *str) { CLASS_CLS; uint8_t i; for (i = 0; i < 25; i++) { printf ( " " ); } printf ( "%s\n" , text); for (i = 0; i <= 60; i++) { printf ( "%s" , str); } printf ( "\n" ); } void menu( void ) { titile( "【学生选课系统】" , "-" ); printf ( "\n\t|-----------------------------------|" ); printf ( "\n\t| [1]--增加课程 |" ); printf ( "\n\t| [2]--浏览课程 |" ); printf ( "\n\t| [3]--查询课程 |" ); printf ( "\n\t| [4]--删除课程 |" ); printf ( "\n\t| [5]--修改课程 |" ); printf ( "\n\t| [Q]--退出系统 |" ); printf ( "\n\t|-----------------------------------|" ); } void get_bat_data( void ) { class_t *point, *q; uint32_t count = 0; FILE *fp = fopen ( "c:\\student_elective.dat" , "rb" ); rewind (fp); point = (class_t *) malloc ( sizeof (class_t)); head = point; while (! feof (fp)) { count++; fread (point, sizeof (class_t), 1, fp); point->next = (class_t *) malloc ( sizeof (class_t)); q = point; point = point->next; } q->next = NULL; fclose (fp); } void save_bat_data( void ) { class_t *point = head; FILE *fp = fopen ( "c:\\student_elective.dat" , "w+" ); while (NULL != point) { count++; fwrite (point, sizeof (class_t), 1, fp); point = point->next; } fclose (fp); } uint32_t num_check( void ) { char ch; uint32_t sum = 0; while (1) { ch = getch(); if ( '\n' == ch || '\r' == ch) { return sum; } else if ( '\b' == ch) { sum /= 10; printf ( "\b \b" ); } else if (( '0' <= ch) && ( '9' >= ch)) { sum *= 10; sum += ch - '0' ; printf ( "%d" , ch - '0' ); } } } void create( void ) { class_t *point, *q; char tmp[CLASS_NAME], ch; uint8_t flag = 0; while (1) { if (1 != count) { printf ( "是否继续增加课程(y/n):" ); gets (tmp); if ( strcmp (tmp, "n" ) == 0) { break ; } } point = (class_t *) malloc ( sizeof (class_t)); point->is_exsit = 0; printf ( "\n====请输入第%d个选修课程信息====\n" , count); printf ( "选择课程名称:" ); gets (point->name); q = head; while (NULL != q) { if ( strcmp (q->name, point->name) == 0) { flag = 1; printf ( "课程名称重复或者不合格,请重新输入...\n" ); break ; } q = q->next; } if (1 == flag) { continue ; } printf ( "课程性质:" ); printf ( "\n[B]--【必修】 [X]--【选修】" ); while (1) { ch = getch(); if (ch == 'b' || ch == 'B' ) { point->nature = 1; break ; } if (ch == 'x' || ch == 'X' ) { point->nature = 2; break ; } } printf ( "\n输入总学时:(只接受数字!)" ); point->total_period = num_check(); printf ( "\n输入授课学时:(只接受数字!)" ); point->teach_period = num_check(); printf ( "\n输入上机学时:(只接受数字!)" ); point->exper_period = num_check(); printf ( "\n输入本课程学分:(只接受数字!)" ); point->score = num_check(); printf ( "\n输入开课学期:(只接受数字!)" ); point->start_time = num_check(); point->is_exsit = 1; point->next = head; head = point; count++; } printf ( "信息录入完毕,按任意键继续……" ); getch(); } void display( void ) { class_t *point = head; CLASS_CLS; titile( "【查看课程】" , "-" ); printf ( "\n名称 \t性质\t总学时\t授课学时\t上机学时\t学分\t开课学期" ); while (NULL != point) { if (1 == point->is_exsit) { printf ( "\n%-14s " , point->name); if (1 == point->nature) { printf ( "必修课" ); } else { printf ( "选修课" ); } printf ( " %d时 %d时 %d时 %d分 %d时" , point->total_period, point->teach_period, point->exper_period, point->score, point->start_time); } point = point->next; } getch(); } // 对照学生管理系统自行拓展 void search( void ) { } void modify( void ) { } void delete ( void ) { } int main( void ) { uint8_t value; uint8_t movie = 1; char choice[3]; FILE *fp = fopen ( "c:\\student_elective.dat" , "a" ); fclose (fp); system ( "color 30" ); system ( "mode con:cols=100 lines=35" ); system ( "title 【选修课系统】" ); if (1 == movie) { play( "欢迎使用【选修课系统】" , 80, 1500, 10); } while (1) { CLASS_CLS; menu(); do { gets (choice); value = atoi (choice); } while ((value > 12) || (value < 0)); switch (value) { case 1: create(); break ; case 2: display(); break ; case 3: search(); break ; case 4: modify(); break ; case 5: delete (); break ; case 6: save_bat_data(); break ; case 7: get_bat_data(); break ; case 8: exit (1); break ; default : break ; } } return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wqx521/article/details/58605698