本文实例为大家分享了C语言实现学生信息管理程序的具体代码,供大家参考,具体内容如下
目前只有5个功能
1. 增加学生信息
2. 删除学生信息
3. 查询某学生信息
4. 列出所有学生信息
5. 按出生日期先后输出学生信息
代码:
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
|
#include<stdio.h> #include<stdlib.h> #include<string.h> struct StuInfo //学生信息 { char cName[10]; char cSex; char cNumber[6]; char cTel[13]; int iYear; int iMonth; }; typedef struct StuInfo INFO; void AddStu() //增加学生函数 { int iLenth; FILE *fp = NULL; INFO toAdd; //临时存储新学生信息 system ( "CLS" ); fp = fopen ( "SavedStudents.txt" , "a+" ); //a+读写指针在文件尾 if (fp == NULL) { puts ( "Error01" ); getchar (); getchar (); return ; } puts ( "输入信息:\n姓名\t性别\t学号\t出生年月\t手机号" ); scanf ( "%s %c %s %d.%d %s" , toAdd.cName, &toAdd.cSex, toAdd.cNumber, &toAdd.iYear, &toAdd.iMonth, toAdd.cTel); //合法性判断 if (toAdd.cSex != 'F' &&toAdd.cSex != 'M' ) //性别只能为F或者M { puts ( "性别格式错误!请重新输入性别:" ); scanf ( "%c" , &toAdd.cSex); } if (toAdd.iYear <= 1980 || toAdd.iYear >= 2017 || toAdd.iMonth>12) { puts ( "日期错误!请重新输入出生日期:" ); scanf ( "%d.%d" , &toAdd.iYear, &toAdd.iMonth); //日期格式为1990.1格式 } iLenth = strlen (toAdd.cTel); //手机号长度 if (iLenth != 11) { puts ( "手机号码格式错误!请重新输入手机号:" ); scanf ( "%s" , toAdd.cTel); } iLenth = strlen (toAdd.cNumber); if (iLenth != 5) { puts ( "学号格式错误!请重新输入学号:" ); scanf ( "%s" , toAdd.cNumber); } //判断结束 fwrite (&toAdd, sizeof (INFO), 1, fp); fclose (fp); puts ( "写入成功!" ); puts ( "按回车返回" ); getchar (); getchar (); return ; } void DeleteStu() //通过学号删除学生信息 { FILE *fp = NULL; INFO Stu[100]; int i = 0, iCount = 0, iRecord = 0; char cNumber[6]; system ( "CLS" ); fp = fopen ( "SavedStudents.txt" , "a+" ); if (fp == NULL) { puts ( "Error01" ); getchar (); getchar (); return ; } rewind (fp); puts ( "姓名\t性别\t学号\t出生年月\t手机号" ); for (i = 0, iCount = 0; fread (&Stu[i], sizeof (INFO), 1, fp);++i, ++iCount) { printf ( "%s\t%c\t%s\t%d.%d\t\t%s\n" , Stu[i].cName, Stu[i].cSex, Stu[i].cNumber, Stu[i].iYear, Stu[i].iMonth, Stu[i].cTel); } fclose (fp); fp = fopen ( "SavedStudents.txt" , "w+" ); //覆盖重写 rewind (fp); puts ( "输入要删除学生的学号:" ); scanf ( "%s" , cNumber); for (i = 0;i<iCount;i++) { if ( strcmp (cNumber, Stu[i].cNumber) == 0) { iRecord = i; break ; } } for (i = 0;i<iCount;++i) { if (i == iRecord) //删除实质为:不向文件写入该条信息 { continue ; } else { fwrite (&Stu[i], sizeof (INFO), 1, fp); } } fclose (fp); puts ( "删除完成!" ); puts ( "按回车返回" ); getchar (); getchar (); return ; } void GetInfo() //通过学号取得学生信息 { FILE *fp; INFO Stu[100]; char cNumber[6]; int iLenth, iCount = 0, i; system ( "CLS" ); fp = fopen ( "SavedStudents.txt" , "a+" ); //a+读写指针在文件尾 if (fp == NULL) { puts ( "Error01" ); getchar (); getchar (); return ; } rewind (fp); for (; fread (&Stu[iCount], sizeof (INFO), 1, fp);++iCount); //读入并计数 puts ( "输入你要查询的学号:如:Y1001" ); scanf ( "%s" , cNumber); iLenth = strlen (cNumber); if (iLenth != 5) { puts ( "学号格式错误!" ); puts ( "按回车返回" ); getchar (); getchar (); return ; } for (i = 0;i<iCount;++i) { if ( strcmp (Stu[i].cNumber, cNumber) == 0) { puts ( "姓名\t性别\t学号\t出生年月\t手机号" ); printf ( "%s\t%c\t%s\t%d.%d\t\t%s\n" , Stu[i].cName, Stu[i].cSex, Stu[i].cNumber, Stu[i].iYear, Stu[i].iMonth, Stu[i].cTel); break ; } if (i == iCount - 1) { puts ( "没有找到匹配学号!" ); } } puts ( "按回车返回" ); getchar (); getchar (); return ; } void ListAllInfo() //查看所有学生信息 { FILE *fp = NULL; INFO Stu[100], Temp; int iCount = 0, i = 0, j = 0; system ( "CLS" ); fp = fopen ( "SavedStudents.txt" , "a+" ); if (fp == NULL) { puts ( "Error01" ); getchar (); getchar (); return ; } rewind (fp); for (i = 0, iCount = 0; fread (&Stu[i], sizeof (INFO), 1, fp);++i, ++iCount); for (i = 0;i<iCount;i++) { for (j = 0;j<iCount - 1;j++) { if ( strcmp (Stu[j].cNumber, Stu[j + 1].cNumber)>0) { Temp = Stu[j]; Stu[j] = Stu[j + 1]; Stu[j + 1] = Temp; } } } puts ( "姓名\t性别\t学号\t出生年月\t手机号" ); for (i = 0;i<iCount;++i) { printf ( "%s\t%c\t%s\t%d.%d\t\t%s\n" , Stu[i].cName, Stu[i].cSex, Stu[i].cNumber, Stu[i].iYear, Stu[i].iMonth, Stu[i].cTel); } printf ( "\n共找到[%d]条记录" , iCount); puts ( "\n按回车返回" ); getchar (); getchar (); return ; } void SortAndOut() //排序学生 { FILE *fp = NULL; INFO Stu[100], Temp; int i, j, iCount = 0; system ( "CLS" ); fp = fopen ( "SavedStudents.txt" , "a+" ); if (fp == NULL) { puts ( "Error01" ); getchar (); getchar (); return ; } rewind (fp); for (; fread (&Stu[iCount], sizeof (INFO), 1, fp);++iCount); //读入并计数 for (i = 0;i<iCount - 1;++i) { for (j = 0;j<iCount - 1;++j) { if (Stu[j].iYear>Stu[j + 1].iYear) { Temp = Stu[j]; Stu[j] = Stu[j + 1]; Stu[j + 1] = Temp; } if (Stu[j].iYear == Stu[j + 1].iYear&&Stu[j].iMonth>Stu[j + 1].iMonth) { Temp = Stu[j]; Stu[j] = Stu[j + 1]; Stu[j + 1] = Temp; } } } puts ( "姓名\t性别\t学号\t出生年月\t手机号" ); for (i = 0;i<iCount;++i) { printf ( "%s\t%c\t%s\t%d.%d\t\t%s\n" , Stu[i].cName, Stu[i].cSex, Stu[i].cNumber, Stu[i].iYear, Stu[i].iMonth, Stu[i].cTel); } puts ( "按回车返回" ); getchar (); getchar (); return ; } int main() { int iChoice; puts ( "1.增加学生\n2.删除学生\n3.查询学生\n4.显示所有学生信息(默认学号排序)\n5.按出生年月排序输出\n0.退出\n输入选择:" ); scanf ( "%d" , &iChoice); while (iChoice != 0) //主界面 { switch (iChoice) { case 1:AddStu(); break ; case 2:DeleteStu(); break ; case 3:GetInfo(); break ; case 4:ListAllInfo(); break ; case 5:SortAndOut(); break ; default : break ; } system ( "CLS" ); puts ( "1.增加学生\n2.删除学生\n3.查询学生\n4.显示所有学生信息(默认学号排序)\n5.按出生年月排序输出\n0.退出\n输入选择:" ); scanf ( "%d" , &iChoice); } return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/YangZuo_Chester/article/details/77164912