此项目为一个小型学生管理系统,仅供初学者学习交流使用,使用者可以在此项目中用已学的C++基础知识进行实战演练,加深对所学知识的了解。
可以从github直接克隆项目(项目地址)
1. 项目要点
1、在每次进入最初登陆界面时,由于要再次加载文件内容,因此需先将list underst 和 list ad 中的内容使用clear()函数清空后再读入。
2、在读取文件时,由于使用!infile.eof()函数会导致最后一行读取两次。因此,在读文件循环内加入infile.get(),目的是在读完一行后立即换行。
2. 功能介绍
从上图可以看出,此项目主要有三个功能模块:开通管理员账户、管理员功能界面(管理员身份登录)、本科生功能界面(本科生省份登录)。第一次运行本项目时,首先要开通管理员账户才能进行其他的操作。
2.1 管理员功能界面
从上图可以看到管理员共有五项功能。
- 查看所有学生信息
- 按姓名查看学生信息
- 按学号查看学生信息录
- 入学生信息按学号
- 删除学生信息
2.2 学生功能界面
从上图可以看到学生共有两项功能。
查看个人信息修改密码
需要注意的是,在登录学生账户之前首先要登进管理员系统创建学生信息之后才可以使用该账户登录学生界面。
3. code(以下代码均在VS2017上编译运行通过)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19/*Administer.h 此文件为Administer类的头文件*/ #pragma once #include<iostream> #include<string> using namespace std; class Administer { private: string name; string ID; string password; public: Administer() {} Administer(string na, string id, string passw); string get_id(){return ID;} string get_name(){return name;} string get_password(){ return password; } void display(); };
复制代码
1
2
3
4
5
6
7
8
9
10
11/*Administer.cpp 此文件为Administer类的实现*/ #include"Administer.h" Administer::Administer(string na, string id, string passw) :name(na), ID(id), password(passw) {} void Administer::display() { cout << endl << "******************" << endl; cout << endl << "* 姓名:" << name; cout << endl << "* 账号:" << ID; cout << endl << "******************" << endl; }
复制代码
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/*UnderStudent.h 此文件为UnderStuent类的头文件*/ #pragma once #include<iostream> #include<string> using namespace std; class Understudent { private: string name; string ID; string password; float grade; string sex; public: Understudent() {} Understudent(string na, string id, string passw, float gra, string s); string get_name(){return name;} string get_id(){return ID;} string get_password(){return password;} float get_grade() { return grade; } string get_sex() { return sex; } void display(); bool operator == (const Understudent &u)const //注意此处参数必须为const类型,才能与STL中list的内置函数匹配 { return ID == u.ID; } void set_password(string passw) { password = passw; } };
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13/*UnderStudent.cpp 此文件为UnderStudent类的实现*/ #include"UnderStudent.h" Understudent::Understudent(string na, string id, string passw, float gra, string s):name(na),ID(id),password(passw),grade(gra), sex(s) {} void Understudent::display() { cout << "******************"<< endl; cout << "* 姓名:" << name << endl; cout << "* 学号:" << ID <<endl ; cout << "* 性别:" << sex <<endl ; cout << "* 绩点:" << grade << endl; cout << "******************"<< endl; }
复制代码
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/*System.h 此文件为System的头文件*/ #pragma once #include<list> #include<fstream> #include"UnderStudent.h" #include"Administer.h" class System { private: list<Understudent> underst; list<Administer> ad; static int underst_count; static int ad_count; public: virtual void load_interface(); //登陆界面 void exit_system(); //退出系统 void understudent_functionshow(); //学生用户功能界面 void administer_functionshow(); //管理员功能界面 void set_ad_account(); //设置管理员账户 void enter_ad_account(); //管理员身份登陆 void enter_underst_account(); //本科生身份登陆 void save_undst(); //保存本科生数据 void save_ad(); //保存管理员数据 void load_undst(); //读取本科生数据 void load_ad(); //读取管理员数据 /*管理员功能*/ void input_underst_info(); //录入本科生信息 void look_all_underst(); //查看所有本科生信息 void look_underst_by_name(string name); //根据姓名查看本科生信息 void look_underst_by_id(string id); //根据ID查看本科生信息 void delete_underst_by_id(string id); //根据ID删除本科生信息 /*本科生功能*/ void change_password(string id); //修改密码 };
复制代码
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515/*System.cpp 此文件为System类的实现*/ #include"System.h" int System::underst_count = 0; int System::ad_count = 0; //登陆界面 void System::load_interface() { int i; do { system("cls"); load_ad(); load_undst(); cout << "********************" << endl; cout << "1)开通管理员账户!" << endl; cout << "2)管理员身份登陆!" << endl; cout << "3)本科生身份登陆!" << endl; cout << "4)退出系统!" << endl; cout << "********************" << endl; cout << "请输入操作:"; cin >> i; while (i < 1 || i>4) { cout << "请输入正确的序号!" << endl; cout << "请重新输入:"; cin >> i; } switch (i) { case 1: set_ad_account(); break; case 2: enter_ad_account(); break; case 3: enter_underst_account(); break; case 4: exit_system(); break; default: break; } //cin.get(); } while (true); } //退出系统 void System::exit_system() { cout << "****************感谢使用!******************" << endl; exit(0); } //本科生功能界面 void System::understudent_functionshow() { cout << "***************************" << endl; cout << "1)查看个人信息" << endl; cout << "2)修改密码" << endl; cout << "3)返回上一级菜单!" << endl; cout << "*****************************" << endl; cout << "请选择你要进行的操作:"; } //管理员功能界面 void System::administer_functionshow() { cout << "***************************" << endl; cout << "1)查看所有学生信息!" << endl; cout << "2)按姓名查找学生信息!" << endl; cout << "3)按学号查找学生信息!" << endl; cout << "4)录入学生信息" << endl; cout << "5)按学号删除学生信息" << endl; cout << "6)返回上一级菜单!" << endl; cout << "*****************************" << endl; cout << "请选择你要进行的操作:"; } //设置管理员账户 void System::set_ad_account() { string name; string id; string password; string password2; cout << endl<<"请输入姓名:"; cin >> name; cout << endl << "请输入ID:"; cin >> id; cout << endl << "请输入密码:"; cin >> password; cout << endl << "请再次输入密码:"; cin >> password2; while (password != password2) { cout << "两次密码不一致,请再次确认:"; cin >> password2; } Administer adm(name, id, password); ad.push_back(adm); cout << "开户成功!" << endl; cin.get(); ad_count++; save_ad(); } //管理员身份登陆 void System::enter_ad_account() { string udst_name; //要查询的学生的名字 string udst_id; //要查询学生的ID string id; string passw; list<Administer>::iterator iter; cout << "请输入账户:"; cin >> id; int flag = 1; for (iter = ad.begin(); iter != ad.end(); iter++) { if (id == iter->get_id()) { flag = 0; break; } } if (flag) { cout << endl<<"账户不存在!" << endl; return; } cout << endl << "请输入密码:"; cin >> passw; while (passw != iter->get_password()) { cout << endl<<"密码错误,请重新输入:"; cin >> passw; } cin.get(); int n; do { system("cls"); administer_functionshow(); cin >> n; while (n < 1 || n>6) { cout << "请输入正确的选项:"; cin >> n; } switch (n) { case 1: look_all_underst(); break; case 2: cout << "请输入要查询学生的名字:"; cin >> udst_name; look_underst_by_name(udst_name); break; case 3: cout << "请输入要查询学生的ID:"; cin >> udst_id; look_underst_by_id(udst_id); break; case 4: input_underst_info(); break; case 5: cout << "请输入要删除学生的ID:"; cin >> udst_id; delete_underst_by_id(udst_id); break; case 6: return; break; default: break; } } while (1); } //本科生身份登陆 void System::enter_underst_account() { list<Understudent>::iterator iter; string id; string passw; cout << "请输入账户:"; cin >> id; int flag = 1; for (iter = underst.begin(); iter != underst.end(); iter++) { if (id == iter->get_id()) { flag = 0; break; } } if (flag) { cout << endl << "账户不存在!" << endl; return; } cout << endl << "请输入密码:"; cin >> passw; while (passw != iter->get_password()) { cout << endl << "密码错误,请重新输入:"; cin >> passw; } int n; do { system("cls"); understudent_functionshow(); cin >> n; while (n < 1 || n>3) { cout << endl << "请输入正确的操作:"; cin >> n; } system("cls"); switch (n) { case 1: iter->display(); break; case 2: change_password(id); break; case 3: return; break; default: break; } system("pause"); } while (true); } //保存管理员数据 void System::save_ad() { ofstream outfile("administer.dat",ios::out); list<Administer>::iterator iter; outfile << ad_count << endl; for (iter = ad.begin(); iter != ad.end(); iter++) { outfile << iter->get_name() << "t" << iter->get_id() << "t" << iter->get_password() << endl; } outfile.close(); } //保存本科生数据 void System::save_undst() { ofstream outfile("understudent.dat",ios::out); list<Understudent>::iterator iter; outfile << underst_count << endl; for (iter = underst.begin(); iter != underst.end(); iter++) { outfile << iter->get_name() << "t" << iter->get_id() << "t" << iter->get_password() << "t" << iter->get_grade() << "t" << iter->get_sex() << endl; } outfile.close(); } //读取本科生数据 void System::load_undst() { ifstream infile("understudent.dat"); if (!infile) { cout << "无本科生资料!" << endl; return; } string name; string ID; string password; float grade; string sex; infile >> underst_count;//读取本科生总人数 infile.get(); if (!underst.empty()) underst.clear(); while (!infile.eof() && infile.peek() != EOF) { infile >> name >> ID >> password >> grade >> sex; Understudent undst(name, ID, password, grade, sex); underst.push_back(undst); infile.get(); } infile.close(); cout << "读取本科生资料正常。" << endl; } //读取管理员数据 void System::load_ad() { ifstream infile("administer.dat"); if (!infile) { cout << "无管理员资料!" << endl; return; } string name; string ID; string password; infile >> ad_count;//读取管理员总人数 infile.get(); if (!ad.empty()) ad.clear(); while (!infile.eof()||infile.peek()!=EOF) { infile >> name >> ID >> password; Administer adm(name, ID, password); ad.push_back(adm); infile.get(); } infile.close(); cout << "读取管理员资料正常。" << endl; } /* 管理员权限: */ //1)查看所有本科生信息 void System::look_all_underst() { system("cls"); if (underst.empty()) { cout << "无本科生数据!" << endl; system("pause"); return; } list<Understudent>::iterator iter; cout << "姓名" << "t" << "ID" << "t" << "t" <<"性别" << "t" << "绩点" << endl; for (iter = underst.begin(); iter != underst.end(); iter++) cout << iter->get_name() << "t" << iter->get_id() << "t" << iter->get_sex() << "t" << iter->get_grade() << endl; cout << endl << "学生总人数:" << underst_count << endl; system("pause"); } //2)按姓名查看本科生数据 void System::look_underst_by_name(string udst_name) { system("cls"); if (underst.empty()) { cout << "无本科生数据!" << endl; system("pause"); return; } list<Understudent>::iterator iter; for (iter = underst.begin(); iter != underst.end(); iter++) { if (iter->get_name() == udst_name) { cout << "姓名" << "t" << "ID" << "t" << "t" << "性别" << "t" << "绩点" << endl; cout << iter->get_name() << "t" << iter->get_id() << "t" << iter->get_sex() << "t" << iter->get_grade() << endl; //姓名可以重复,因此遍历所有 } if (iter == --underst.end()) { system("pause"); return; } } cout << "无该生数据!" << endl; system("pause"); return; } //3)按ID查看本科生数据 void System::look_underst_by_id(string udst_id) { system("cls"); if (underst.empty()) { cout << "无本科生数据!" << endl; system("pause"); return; } list<Understudent>::iterator iter; for (iter = underst.begin(); iter != underst.end(); iter++) { if (iter->get_id()==udst_id) { cout << "姓名" << "t" << "ID" << "t" << "t" << "性别" << "t" << "绩点" << endl; cout << iter->get_name() << "t" << iter->get_id() << "t" << iter->get_sex() << "t" << iter->get_grade() << endl; system("pause"); return; //ID不能有重复 } } cout << "无该生数据!" << endl; system("pause"); return; } //4)录入本科生信息 void System::input_underst_info() { string name; string ID; string password; float grade; string sex; char s; //是否继续录入flag do { system("cls"); cout << endl << "请输入学生姓名:"; cin >> name; cout << endl << "请输入学生ID:"; cin >> ID; cout << endl << "请输入学生初始密码:"; cin >> password; cout << endl << "请输入学生绩点:"; cin >> grade; cout <<endl<< "请输入学生性别:"; cin >> sex; Understudent undst(name, ID, password, grade, sex); underst.push_back(undst); underst_count++; cout << endl << "是否继续录入?(Y/N)"; cin >> s; while (s != 'Y'&&s != 'N'&&s != 'y'&&s != 'n') { cout << endl << "请输入正确操作(Y/N):"; cin >> s; } } while (s == 'Y'||s=='y'); save_undst(); } //5)按ID删除学生信息 void System::delete_underst_by_id(string udst_id) { system("cls"); if (underst.empty()) { cout << "无本科生数据!" << endl; system("pause"); return; } list<Understudent>::iterator iter; string name; string ID; string password; float grade; string sex; for (iter = underst.begin(); iter != underst.end(); iter++) { if (iter->get_id() == udst_id) { name = iter->get_name(); ID = iter->get_id(); password = iter->get_password(); grade = iter->get_grade(); sex = iter->get_sex(); Understudent undst(name, ID, password, grade, sex); underst.remove(undst); underst_count--; cout << "删除成功!" << endl; system("pause"); save_undst(); return; //ID不能有重复 } } cout << "无该生数据!" << endl; system("pause"); return; } /* 本科生权限 */ //2)修改密码 void System::change_password(string id) { string password, passw; cout << "请输入新密码:"; cin >> password; cout <<endl<<"请再次输入:"; cin >> passw; while (password != passw) { cout << endl<<"两次密码不一致,请重新输入:"; cin >> passw; } list<Understudent>::iterator iter; for (iter = underst.begin(); iter != underst.end(); iter++) { if (iter->get_id() == id) break; } iter->set_password(password); cout << "修改密码成功!" << endl; save_undst(); }
复制代码
1
2
3
4
5
6
7
8
9/*mai.cpp 此文件为主函数所在文件*/ #include"System.h" int main() { System s; s.load_interface(); system("pause"); return 0; }
代码目录结构
到此这篇关于C++实现学生管理系统示例解析的文章就介绍到这了,更多相关C++学生管理系统内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是俭朴大门最近收集整理的关于C++实现学生管理系统示例解析的全部内容,更多相关C++实现学生管理系统示例解析内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复