- 前言
- 问题描述
- 功能要求
- 分析
- 继承关系分析
- 全局函数说明
- 代码
- 测试数据
- 问题总结
问题描述本文章是 【C++笔记】 专栏的文章,主要是C++黑马的笔记、自己的实验与课设
C++笔记 传送门
某高校有四类员工:教师、实验员、行政人员、教师兼行政人员
- 共有的信息:编号、姓名、性别、年龄
- 教师的信息:所在系部、专业、职称
- 实验员的信息:所在实验室、职务
- 行政人员的信息:政治面貌、职称
- 添加功能:程序能够任意添加上述四类人员的记录,可提供选择界面供用户选择所要添加的人员类别,要求员工的编号要唯一,如果添加了重复编号的记录时,则提示数据添加重复并取消
- 添加查询:功能可根据编号、姓名等信息对已添加的记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息
- 显示功能:可显示当前系统中所有记录,每条记录占据一行
- 编辑功能:可根据查询结果对相应的记录进行修改,修改时注意编号的唯一性
- 删除功能:主要实现对已添加的人员记录进行删除。如果当前系统中没有相应的人员记录,则提示“记录为空!”并返回操作;否则,输入要除的人员的编号或姓名,根据所输入的信息删除该人员记录,如果没有找到该人员信息,则提示相应的记录不存
- 统计功能:能根据多种参数进行人员的统计。例如:统计四类人员数量以及总数,或者统计男、女员工的数量,或者统计某年龄段人员的数量等
- 保存功能:可将当前系统中各类人员记录存入文件中,存入方式任意
- 读取功能:可将保存在文件中的人员信息读入到当前系统中,供用户进行使用
可以从仓库地址下一份,或者自己新建项目把代码粘进去。
#include测试数据using namespace std; #include #include #define MAX 200 //每一类人员上限200 //声明全局函数 void Statistics();//统计函数 void Statisticstotal();//统计总人数 void Statisticman();//统计男员工 void Statisticwomen();//统计女员工 void Statisticage();//统计年龄段 //人 类 class Person { public: char id[10]; //编号 char name[5];//姓名 char sex[10];//性别 int age; //年龄 }; //教师类 class Teacher :virtual public Person { friend class Tea_Po; //声明友元,教师兼行政人员类访问教师私有变量 public: void Add();//添加 void Seach();//查询 void Show();//显示 void Edit();//编辑 void Delete();//删除 void Save();//保存 void Read();//读取 void Seachid();//编号查询 void Seachname();//姓名查询 void Showone(); private: char t_dept[20];//所在系部 char t_major[20];//专业 char t_title[10];//职称 }; Teacher Tea[MAX]; int Teatop = 0; void Teacher::Add() { Teacher t_temp;//临时变量 if (Teatop < MAX) { cout << "编号:"; cin >> t_temp.id; for (int i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].id, t_temp.id)) { cout << "id已存在" << endl; return; } } cout << "姓名:"; cin >> t_temp.name; cout << "性别:"; cin >> t_temp.sex; cout << "年龄:"; cin >> t_temp.age; cout << "系部:"; cin >> t_temp.t_dept; cout << "专业:"; cin >> t_temp.t_major; cout << "职称:"; cin >> t_temp.t_title; Tea[Teatop] = t_temp; Teatop++; } else { cout << "当前教师已招满" << endl; } } void Teacher::Seach() { while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 教师管理平台-查询 |" << endl; cout << " | 1.编号查询 |" << endl; cout << " | 2.姓名查询 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: Seachid(); break; case 2: Seachname(); break; case 0: return; break; default: cout << "输入错误请重新输入" << endl; break; } } } void Teacher::Show() { if (Teatop == 0) { cout << "当前教师系统内为空" << endl; } else { for (int i = 0; i < Teatop; i++) { Tea[i].Showone(); } } } void Teacher::Edit() { Teacher t_temp; while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 教师管理平台-编辑 |" << endl; cout << " | 1.根据编号查询并修改 |" << endl; cout << " | 2.根据姓名查询并修改 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; if (c == 1) { char sid[10]; int i; cout << "请输入编号"; cin >> sid; for (i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].id, sid)) break; } if (i == Teatop) cout << "输入编号无效,系统内不存在" << endl; else { cout << "编号:"; cin >> t_temp.id; cout << "姓名:"; cin >> t_temp.name; cout << "性别:"; cin >> t_temp.sex; cout << "年龄:"; cin >> t_temp.age; cout << "系部:"; cin >> t_temp.t_dept; cout << "专业:"; cin >> t_temp.t_major; cout << "职称:"; cin >> t_temp.t_title; Tea[i] = t_temp; } break; } else if (c == 2) { char sname[5]; int i; cout << "请输入姓名"; cin >> sname; for (i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].name, sname)) break; } if (i == Teatop) cout << "输入编号无效,系统内不存在" << endl; else { cout << "编号:"; cin >> t_temp.id; cout << "姓名:"; cin >> t_temp.name; cout << "性别:"; cin >> t_temp.sex; cout << "年龄:"; cin >> t_temp.age; cout << "系部:"; cin >> t_temp.t_dept; cout << "专业:"; cin >> t_temp.t_major; cout << "职称:"; cin >> t_temp.t_title; Tea[i] = t_temp; } break; } else if (c == 0) { break; } else { cout << "输入错误请重新选择" << endl; } } } void Teacher::Delete() { if (Teatop == 0) { cout << "当前记录为空" << endl; return; } while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 教师管理平台-删除 |" << endl; cout << " | 1.根据编号查询并删除 |" << endl; cout << " | 2.根据姓名查询并删除 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; if (c == 1) { char sid[10]; int i; cout << "请输入编号"; cin >> sid; for (i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].id, sid)) break; } if (i == Teatop) cout << "输入编号无效,系统内不存在" << endl; else { for (i; i < Teatop; i++) { Tea[i] = Tea[i + 1]; } cout << "删除成功" << endl; Teatop--; } break; } else if (c == 2) { char sname[5]; int i; cout << "请输入姓名"; cin >> sname; for (i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].name, sname)) break; } if (i == Teatop) cout << "输入姓名无效,系统内不存在" << endl; else { for (i; i < Teatop; i++) { Tea[i] = Tea[i + 1]; } cout << "删除成功" << endl; Teatop--; } break; } else if (c == 0) { break; } else { cout << "输入错误请重新选择" << endl; } } } void Teacher::Save() { int i; ofstream outfile, outfile1; outfile1.open("Teatop.dat", ios::out); //写文件Teatop.dat outfile1 << Teatop; outfile.open("Tea_data.dat", ios::binary); if (!outfile) { cerr << "open error!" << endl; return; } for (i = 0; i < Teatop; i++) outfile.write((char *)&Tea[i], sizeof(Tea[i])); outfile.close(); cout << "保存成功!" << endl; } void Teacher::Read() { int i; ifstream infile, infile1; infile1.open("Teatop.dat", ios::in); infile1 >> Teatop; infile.open("Tea_data.dat", ios::binary); if (!infile) { cerr << "open error!" << endl; return; } for (i = 0; i < Teatop; i++) infile.read((char *)&Tea[i], sizeof(Tea[i])); infile.close(); cout << "读取成功!" << endl; } void Teacher::Seachid() { char sid[10]; cout << "请输入编号" << endl; cin >> sid; int i; for (i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].id, sid)) { Tea[i].Showone(); break; } } if (i == Teatop) { cout << "该编号不存在" << endl; } } void Teacher::Seachname() { char sname[5]; cout << "请输入姓名" << endl; cin >> sname; int i; for (i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].name, sname)) { Tea[i].Showone(); break; } } if (i == Teatop) { cout << "该姓名不存在" << endl; } } void Teacher::Showone() { cout << "编号t" << id << "t姓名t" << name << "t性别t" << sex << "t年龄t" << age << "t系部t" << t_dept << "t专业t" << t_major << "t职称t" << t_title << endl; } //实验人员类 class Experiment :public Person { public: Experiment() {} void Add();//添加 void Seach();//查询 void Show();//显示 void Edit();//编辑 void Delete();//删除 void Save();//保存 void Read();//读取 void Seachid();//编号查询 void Seachname();//姓名查询 void Showone(); private: char e_location[20];//所在实验室 char e_job[10];//职务 }; Experiment Exper[MAX]; int Expertop = 0; void Experiment::Add() { Experiment e_temp;//临时变量 if (Expertop < MAX) { cout << "编号:"; cin >> e_temp.id; for (int i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].id, e_temp.id)) { cout << "id已存在" << endl; return; } } cout << "姓名:"; cin >> e_temp.name; cout << "性别:"; cin >> e_temp.sex; cout << "年龄:"; cin >> e_temp.age; cout << "实验室:"; cin >> e_temp.e_location; cout << "职务:"; cin >> e_temp.e_job; Exper[Expertop] = e_temp; Expertop++; } else { cout << "当前实验人员已招满" << endl; } } void Experiment::Seach() { while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 实验人员管理平台-查询 |" << endl; cout << " | 1.编号查询 |" << endl; cout << " | 2.姓名查询 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: Seachid(); break; case 2: Seachname(); break; case 0: return; break; default: cout << "输入错误请重新输入" << endl; break; } } } void Experiment::Show() { if (Expertop == 0) { cout << "当前实验人员系统内为空" << endl; } else { for (int i = 0; i < Expertop; i++) { Exper[i].Showone(); } } } void Experiment::Edit() { Experiment e_temp; while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 实验人员管理平台-编辑 |" << endl; cout << " | 1.根据编号查询并修改 |" << endl; cout << " | 2.根据姓名查询并修改 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; if (c == 1) { char sid[10]; int i; cout << "请输入编号"; cin >> sid; for (i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].id, sid)) break; } if (i == Expertop) cout << "输入编号无效,系统内不存在" << endl; else { cout << "编号:"; cin >> e_temp.id; cout << "姓名:"; cin >> e_temp.name; cout << "性别:"; cin >> e_temp.sex; cout << "年龄:"; cin >> e_temp.age; cout << "实验室:"; cin >> e_temp.e_location; cout << "职务:"; cin >> e_temp.e_job; Exper[i] = e_temp; } break; } else if (c == 2) { char sname[5]; int i; cout << "请输入姓名"; cin >> sname; for (i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].name, sname)) break; } if (i == Expertop) cout << "输入姓名无效,系统内不存在" << endl; else { cout << "编号:"; cin >> e_temp.id; cout << "姓名:"; cin >> e_temp.name; cout << "性别:"; cin >> e_temp.sex; cout << "年龄:"; cin >> e_temp.age; cout << "实验室:"; cin >> e_temp.e_location; cout << "职务:"; cin >> e_temp.e_job; Exper[i] = e_temp; } break; } else if (c == 0) { break; } else { cout << "输入错误请重新选择" << endl; } } } void Experiment::Delete() { if (Expertop == 0) { cout << "当前记录为空" << endl; return; } while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 实验人员管理平台-删除 |" << endl; cout << " | 1.根据编号查询并删除 |" << endl; cout << " | 2.根据姓名查询并删除 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; if (c == 1) { char sid[10]; int i; cout << "请输入编号"; cin >> sid; for (i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].id, sid)) break; } if (i == Expertop) cout << "输入编号无效,系统内不存在" << endl; else { for (i; i < Expertop; i++) { Exper[i] = Exper[i + 1]; } cout << "删除成功" << endl; Expertop--; } break; } else if (c == 2) { char sname[5]; int i; cout << "请输入姓名"; cin >> sname; for (i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].name, sname)) break; } if (i == Expertop) cout << "输入姓名无效,系统内不存在" << endl; else { for (i; i < Expertop; i++) { Exper[i] = Exper[i + 1]; } cout << "删除成功" << endl; Expertop--; } break; } else if (c == 0) { break; } else { cout << "输入错误请重新选择" << endl; } } } void Experiment::Save() { int i; ofstream outfile, outfile1; outfile1.open("Expertop.dat", ios::out); //写文件Expertop.dat outfile1 << Expertop; outfile.open("Exper_data.dat", ios::binary); if (!outfile) { cerr << "open error!" << endl; return; } for (i = 0; i < Expertop; i++) outfile.write((char *)&Exper[i], sizeof(Exper[i])); outfile.close(); cout << "保存成功!" << endl; } void Experiment::Read() { int i; ifstream infile, infile1; infile1.open("Expertop.dat", ios::in); infile1 >> Expertop; infile.open("Exper_data.dat", ios::binary); if (!infile) { cerr << "open error!" << endl; return; } for (i = 0; i < Expertop; i++) infile.read((char *)&Exper[i], sizeof(Exper[i])); infile.close(); cout << "读取成功!" << endl; } void Experiment::Seachid() { char sid[10]; cout << "请输入编号" << endl; cin >> sid; int i; for (i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].id, sid)) { Exper[i].Showone(); break; } } if (i == Expertop) { cout << "该编号不存在" << endl; } } void Experiment::Seachname() { char sname[5]; cout << "请输入姓名" << endl; cin >> sname; int i; for (i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].name, sname)) { Exper[i].Showone(); break; } } if (i == Expertop) { cout << "该姓名不存在" << endl; } } void Experiment::Showone() { cout << "编号t" << id << "t姓名t" << name << "t性别t" << sex << "t年龄t" << age << "t实验室t" << e_location << "t职务t" << e_job << endl; } //行政人员类 class Politician :virtual public Person { friend class Tea_Po;//声明友元,教师兼行政人员类访问行政人员私有变量 public: void Add();//添加 void Seach();//查询 void Show();//显示 void Edit();//编辑 void Delete();//删除 void Save();//保存 void Read();//读取 void Seachid();//编号查询 void Seachname();//姓名查询 void Showone(); private: char p_look[10];//政治面貌 char p_post[10];//职称 }; Politician Po[MAX]; int Potop = 0; void Politician::Add() { Politician p_temp;//临时变量 if (Potop < MAX) { cout << "编号:"; cin >> p_temp.id; for (int i = 0; i < Potop; i++) { if (!strcmp(Po[i].id, p_temp.id)) { cout << "id已存在" << endl; return; } } cout << "姓名:"; cin >> p_temp.name; cout << "性别:"; cin >> p_temp.sex; cout << "年龄:"; cin >> p_temp.age; cout << "政治面貌:"; cin >> p_temp.p_look; cout << "职称:"; cin >> p_temp.p_post; Po[Potop] = p_temp; Potop++; } else { cout << "当前行政人员已招满" << endl; } } void Politician::Seach() { while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 行政人员管理平台-查询 |" << endl; cout << " | 1.编号查询 |" << endl; cout << " | 2.姓名查询 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: Seachid(); break; case 2: Seachname(); break; case 0: return; break; default: cout << "输入错误请重新输入" << endl; break; } } } void Politician::Show() { if (Potop == 0) { cout << "当前行政人员系统内为空" << endl; } else { for (int i = 0; i < Potop; i++) { Po[i].Showone(); } } } void Politician::Edit() { Politician p_temp; while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 行政人员管理平台-编辑 |" << endl; cout << " | 1.根据编号查询并修改 |" << endl; cout << " | 2.根据姓名查询并修改 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; if (c == 1) { char sid[10]; int i; cout << "请输入编号"; cin >> sid; for (i = 0; i < Potop; i++) { if (!strcmp(Po[i].id, sid)) break; } if (i == Potop) cout << "输入编号无效,系统内不存在" << endl; else { cout << "编号:"; cin >> p_temp.id; cout << "姓名:"; cin >> p_temp.name; cout << "性别:"; cin >> p_temp.sex; cout << "年龄:"; cin >> p_temp.age; cout << "政治面貌:"; cin >> p_temp.p_look; cout << "职称:"; cin >> p_temp.p_post; Po[i] = p_temp; } break; } else if (c == 2) { char sname[5]; int i; cout << "请输入姓名"; cin >> sname; for (i = 0; i < Potop; i++) { if (!strcmp(Po[i].name, sname)) break; } if (i == Potop) cout << "输入编号无效,系统内不存在" << endl; else { cout << "编号:"; cin >> p_temp.id; cout << "姓名:"; cin >> p_temp.name; cout << "性别:"; cin >> p_temp.sex; cout << "年龄:"; cin >> p_temp.age; cout << "政治面貌:"; cin >> p_temp.p_look; cout << "职称:"; cin >> p_temp.p_post; Po[i] = p_temp; } break; } else if (c == 0) { break; } else { cout << "输入错误请重新选择" << endl; } } } void Politician::Delete() { if (Potop == 0) { cout << "当前记录为空" << endl; return; } while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 行政人员管理平台-删除 |" << endl; cout << " | 1.根据编号查询并删除 |" << endl; cout << " | 2.根据姓名查询并删除 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; if (c == 1) { char sid[10]; int i; cout << "请输入编号"; cin >> sid; for (i = 0; i < Potop; i++) { if (!strcmp(Po[i].id, sid)) break; } if (i == Potop) cout << "输入编号无效,系统内不存在" << endl; else { for (i; i < Potop; i++) { Po[i] = Po[i + 1]; } cout << "删除成功" << endl; Potop--; } break; } else if (c == 2) { char sname[5]; int i; cout << "请输入姓名"; cin >> sname; for (i = 0; i < Potop; i++) { if (!strcmp(Po[i].name, sname)) break; } if (i == Potop) cout << "输入姓名无效,系统内不存在" << endl; else { for (i; i < Potop; i++) { Po[i] = Po[i + 1]; } cout << "删除成功" << endl; Potop--; } break; } else if (c == 0) { break; } else { cout << "输入错误请重新选择" << endl; } } } void Politician::Save() { int i; ofstream outfile, outfile1; outfile1.open("Potop.dat", ios::out); //写文件Potop.dat outfile1 << Potop; outfile.open("Po_data.dat", ios::binary); if (!outfile) { cerr << "open error!" << endl; return; } for (i = 0; i < Potop; i++) outfile.write((char *)&Po[i], sizeof(Po[i])); outfile.close(); cout << "保存成功!" << endl; } void Politician::Read() { int i; ifstream infile, infile1; infile1.open("Potop.dat", ios::in); infile1 >> Potop; infile.open("Po_data.dat", ios::binary); if (!infile) { cerr << "open error!" << endl; return; } for (i = 0; i < Potop; i++) infile.read((char *)&Po[i], sizeof(Po[i])); infile.close(); cout << "读取成功!" << endl; } void Politician::Seachid() { char sid[10]; cout << "请输入编号" << endl; cin >> sid; int i; for (i = 0; i < Potop; i++) { if (!strcmp(Po[i].id, sid)) { Po[i].Showone(); break; } } if (i == Potop) { cout << "该编号不存在" << endl; } } void Politician::Seachname() { char sname[5]; cout << "请输入姓名" << endl; cin >> sname; int i; for (i = 0; i < Potop; i++) { if (!strcmp(Po[i].name, sname)) { Po[i].Showone(); break; } } if (i == Potop) { cout << "该姓名不存在" << endl; } } void Politician::Showone() { cout << "编号t" << id << "t姓名t" << name << "t性别t" << sex << "t年龄t" << age << "t政治面貌t" << p_look << "t职称t" << p_post << endl; } //教师兼行政人员类 class Tea_Po :public Teacher, Politician { public: void Add();//添加 void Seach();//查询 void Show();//显示 void Edit();//编辑 void Delete();//删除,调用 void Save();//保存 void Read();//读取 void Seachid();//编号查询 void Seachname();//姓名查询 void Showone(); }; Tea_Po T_P[MAX]; int T_Ptop = 0; void Tea_Po::Add() { Tea_Po tp_temp;//临时变量 if (T_Ptop < MAX) { cout << "编号:"; cin >> tp_temp.id; for (int i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].id, tp_temp.id)) { cout << "id已存在" << endl; return; } } cout << "姓名:"; cin >> tp_temp.name; cout << "性别:"; cin >> tp_temp.sex; cout << "年龄:"; cin >> tp_temp.age; cout << "系部:"; cin >> tp_temp.t_dept; cout << "专业:"; cin >> tp_temp.t_major; cout << "政治面貌:"; cin >> tp_temp.p_look; cout << "职称:"; cin >> tp_temp.p_post; T_P[T_Ptop] = tp_temp; T_Ptop++; } else { cout << "当前教师兼行政人员已招满" << endl; } } void Tea_Po::Seach() { while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 教师兼行政人员管理平台-查询 |" << endl; cout << " | 1.编号查询 |" << endl; cout << " | 2.姓名查询 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: Seachid(); break; case 2: Seachname(); break; case 0: return; break; default: cout << "输入错误请重新输入" << endl; break; } } } void Tea_Po::Show() { if (T_Ptop == 0) { cout << "当前教师兼行政人员系统内为空" << endl; } else { for (int i = 0; i < T_Ptop; i++) { T_P[i].Showone(); } } } void Tea_Po::Edit() { Tea_Po tp_temp; while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 教师兼行政人员管理平台-编辑 |" << endl; cout << " | 1.根据编号查询并修改 |" << endl; cout << " | 2.根据姓名查询并修改 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; if (c == 1) { char sid[10]; int i; cout << "请输入编号"; cin >> sid; for (i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].id, sid)) break; } if (i == T_Ptop) cout << "输入编号无效,系统内不存在" << endl; else { cout << "编号:"; cin >> tp_temp.id; cout << "姓名:"; cin >> tp_temp.name; cout << "性别:"; cin >> tp_temp.sex; cout << "年龄:"; cin >> tp_temp.age; cout << "系部:"; cin >> tp_temp.t_dept; cout << "专业"; cin >> tp_temp.t_major; cout << "政治面貌:"; cin >> tp_temp.p_look; cout << "职称:"; cin >> tp_temp.p_post; T_P[i] = tp_temp; } break; } else if (c == 2) { char sname[5]; int i; cout << "请输入姓名"; cin >> sname; for (i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].name, sname)) break; } if (i == T_Ptop) cout << "输入姓名无效,系统内不存在" << endl; else { cout << "编号:"; cin >> tp_temp.id; cout << "姓名:"; cin >> tp_temp.name; cout << "性别:"; cin >> tp_temp.sex; cout << "年龄:"; cin >> tp_temp.age; cout << "系部:"; cin >> tp_temp.t_dept; cout << "专业"; cin >> tp_temp.t_major; cout << "政治面貌:"; cin >> tp_temp.p_look; cout << "职称:"; cin >> tp_temp.p_post; T_P[i] = tp_temp; } break; } else if (c == 0) { break; } else { cout << "输入错误请重新选择" << endl; } } } void Tea_Po::Delete() { if (T_Ptop == 0) { cout << "当前记录为空" << endl; return; } while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 教师兼行政人员管理平台-删除|" << endl; cout << " | 1.根据编号查询并删除 |" << endl; cout << " | 2.根据姓名查询并删除 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; if (c == 1) { char sid[10]; int i; cout << "请输入编号"; cin >> sid; for (i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].id, sid)) break; } if (i == T_Ptop) cout << "输入编号无效,系统内不存在" << endl; else { for (i; i < T_Ptop; i++) { T_P[i] = T_P[i + 1]; } cout << "删除成功" << endl; T_Ptop--; } break; } else if (c == 2) { char sname[5]; int i; cout << "请输入姓名"; cin >> sname; for (i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].name, sname)) break; } if (i == T_Ptop) cout << "输入姓名无效,系统内不存在" << endl; else { for (i; i < T_Ptop; i++) { T_P[i] = T_P[i + 1]; } cout << "删除成功" << endl; T_Ptop--; } break; } else if (c == 0) { break; } else { cout << "输入错误请重新选择" << endl; } } } void Tea_Po::Save() { int i; ofstream outfile, outfile1; outfile1.open("T_Ptop.dat", ios::out); //写文件T_Ptop.dat outfile1 << T_Ptop; outfile.open("T_P_data.dat", ios::binary); if (!outfile) { cerr << "open error!" << endl; return; } for (i = 0; i < T_Ptop; i++) outfile.write((char *)&T_P[i], sizeof(T_P[i])); outfile.close(); cout << "保存成功!" << endl; } void Tea_Po::Read() { int i; ifstream infile, infile1; infile1.open("T_Ptop.dat", ios::in); infile1 >> T_Ptop; infile.open("T_P_data.dat", ios::binary); if (!infile) { cerr << "open error!" << endl; return; } for (i = 0; i < T_Ptop; i++) infile.read((char *)&T_P[i], sizeof(T_P[i])); infile.close(); cout << "读取成功!" << endl; } void Tea_Po::Seachid() { char sid[10]; cout << "请输入编号" << endl; cin >> sid; int i; for (i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].id, sid)) { T_P[i].Showone(); break; } } if (i == T_Ptop) { cout << "该编号不存在" << endl; } } void Tea_Po::Seachname() { char sname[5]; cout << "请输入姓名" << endl; cin >> sname; int i; for (i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].name, sname)) { T_P[i].Showone(); break; } } if (i == T_Ptop) { cout << "该姓名不存在" << endl; } } void Tea_Po::Showone() { cout << "编号t" << id << "t姓名t" << name << "t性别t" << sex << "t年龄t" << age << "t系部t" << t_dept << "t专业" << t_major << "t政治面貌t" << p_look << "t职称t" << p_look << endl; } //教师菜单 void Tea_menu(Teacher tea) { while (true) { system("pause"); system("cls");//清除主菜单 int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 教师管理平台 |" << endl; cout << " | 1.添加 |" << endl; cout << " | 2.查询 |" << endl; cout << " | 3.显示 |" << endl; cout << " | 4.编辑 |" << endl; cout << " | 5.删除 |" << endl; cout << " | 6.统计 |" << endl; cout << " | 7.保存 |" << endl; cout << " | 8.读取 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: tea.Add(); break; case 2: tea.Seach(); break; case 3: tea.Show(); break; case 4: tea.Edit(); break; case 5: tea.Delete(); break; case 6: Statistics(); break; case 7: tea.Save(); break; case 8: tea.Read(); break; case 0: break; default: cout << "输入错误请重新输入" << endl; break; } if (c == 0) break; } } //实验人员菜单 void Exper_menu(Experiment exper) { while (true) { system("pause"); system("cls");//清除主菜单 int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 实验人员管理平台 |" << endl; cout << " | 1.添加 |" << endl; cout << " | 2.查询 |" << endl; cout << " | 3.显示 |" << endl; cout << " | 4.编辑 |" << endl; cout << " | 5.删除 |" << endl; cout << " | 6.统计 |" << endl; cout << " | 7.保存 |" << endl; cout << " | 8.读取 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: exper.Add(); break; case 2: exper.Seach(); break; case 3: exper.Show(); break; case 4: exper.Edit(); break; case 5: exper.Delete(); break; case 6: Statistics(); break; case 7: exper.Save(); break; case 8: exper.Read(); break; case 0: break; default: cout << "输入错误请重新输入" << endl; break; } if (c == 0) break; } } //行政人员类菜单 void P_menu(Politician po) { while (true) { system("pause"); system("cls");//清除主菜单 int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 行政人员管理平台 |" << endl; cout << " | 1.添加 |" << endl; cout << " | 2.查询 |" << endl; cout << " | 3.显示 |" << endl; cout << " | 4.编辑 |" << endl; cout << " | 5.删除 |" << endl; cout << " | 6.统计 |" << endl; cout << " | 7.保存 |" << endl; cout << " | 8.读取 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: po.Add(); break; case 2: po.Seach(); break; case 3: po.Show(); break; case 4: po.Edit(); break; case 5: po.Delete(); break; case 6: Statistics(); break; case 7: po.Save(); break; case 8: po.Read(); break; case 0: break; default: cout << "输入错误请重新输入" << endl; break; } if (c == 0) break; } } //教师兼行政人员菜单 void TeaPo_menu(Tea_Po t_p) { while (true) { system("pause"); system("cls");//清除主菜单 int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 教师兼行政人员人员管理平台 |" << endl; cout << " | 1.添加 |" << endl; cout << " | 2.查询 |" << endl; cout << " | 3.显示 |" << endl; cout << " | 4.编辑 |" << endl; cout << " | 5.删除 |" << endl; cout << " | 6.统计 |" << endl; cout << " | 7.保存 |" << endl; cout << " | 8.读取 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: t_p.Add(); break; case 2: t_p.Seach(); break; case 3: t_p.Show(); break; case 4: t_p.Edit(); break; case 5: t_p.Delete(); break; case 6: Statistics(); break; case 7: t_p.Save(); break; case 8: t_p.Read(); break; case 0: break; default: cout << "输入错误请重新输入" << endl; break; } if (c == 0) break; } } void Statistics() { while (true) { int c; cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 人数统计 |" << endl; cout << " | 1.四类人员数量 |" << endl; cout << " | 2.男员工数量 |" << endl; cout << " | 3.女员工数量 |" << endl; cout << " | 4.某年龄段数量 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择"; cin >> c; switch (c) { case 1: Statisticstotal(); break; case 2: Statisticman(); break; case 3: Statisticwomen(); break; case 4: Statisticage(); break; case 0: return; break; default: cout << "输入错误请重新输入" << endl; break; } } } //四类员工数量 void Statisticstotal() { cout << "四类员工总数量" << Teatop + Expertop + Potop + T_Ptop << endl; cout << "教师总数量" << Teatop << endl; cout << "实验员总数量" << Expertop << endl; cout << "行政人员总数量" << Potop << endl; cout << "教师兼行政人员总数量" << T_Ptop << endl; } //男员工数量 void Statisticman() { int man = 0; for (int i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].sex, "男")) man++; } for (int i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].sex, "男")) man++; } for (int i = 0; i < Potop; i++) { if (!strcmp(Po[i].sex, "男")) man++; } for (int i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].sex, "男")) man++; } cout << "男员工的数量为" << man << endl; } //女员工数量 void Statisticwomen() { int women = 0; for (int i = 0; i < Teatop; i++) { if (!strcmp(Tea[i].sex, "女")) women++; } for (int i = 0; i < Expertop; i++) { if (!strcmp(Exper[i].sex, "女")) women++; } for (int i = 0; i < Potop; i++) { if (!strcmp(Po[i].sex, "女")) women++; } for (int i = 0; i < T_Ptop; i++) { if (!strcmp(T_P[i].sex, "女")) women++; } cout << "女员工的数量为" << women << endl; } //年龄段统计 void Statisticage() { int age1, age2; cout << "年龄区间:age1="; cin >> age1; cout << "年龄区间:age2="; cin >> age2; int num = 0; for (int i = 0; i < Teatop; i++) { if (Tea[i].age > age1&& Tea[i].age < age2) num++; } for (int i = 0; i < Expertop; i++) { if (Exper[i].age > age1&&Exper[i].age < age2) num++; } for (int i = 0; i < Potop; i++) { if (Po[i].age > age1&&Po[i].age < age2) num++; } for (int i = 0; i < T_Ptop; i++) { if (T_P[i].age > age1&&T_P[i].age < age2) num++; } cout << age1 << "~" << age2 << "的数量为" << num << endl; } int main() { string password = "192062116"; //初始密码 string inputpass; //输入密码 int f = 3; //输入次数 int c; //主菜单选择 //定义四类人员,调用菜单 Teacher mt; Experiment me; Politician mp; Tea_Po mtp; mt.Read(); me.Read(); mp.Read(); mtp.Read(); system("cls"); cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 高校人员信息管理系统 |" << endl; cout << " |______________________________|" << endl << endl; while (true) { cout << "请输入密码(192062116)" << endl; cin >> inputpass; if (inputpass == password) { cout << "密码正确" << endl; break; } else { cout << "密码错误" << endl; if (f > 1) { cout << "你还有" << f - 1 << "次机会" << endl; f--; } else exit(0); } } system("pause"); while (true) { system("cls"); cout << endl; cout << " | ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|" << endl; cout << " | 高校人员信息管理系统 |" << endl; cout << " | 1.教师管理 |" << endl; cout << " | 2.实验员管理 |" << endl; cout << " | 3.行政人员管理 |" << endl; cout << " | 4.教师兼行政人员管理管理 |" << endl; cout << " | 0.退出 |" << endl; cout << " |______________________________|" << endl << endl; cout << "请选择" << endl; cin >> c; switch (c) { case 1: Tea_menu(mt); break; case 2: Exper_menu(me); break; case 3: P_menu(mp); break; case 4: TeaPo_menu(mtp); break; case 0:break; default: cout << "输入错误请重新输入" << endl; system("pause"); break; } if (c == 0) break; } return 0; }
基本功能均已实现,东西挺多的就不附测试结果了。因为编写数据也挺麻烦的,仓库中每类人员各编写了五个,直接把txt中的数据粘贴到运行窗口
问题总结上面代码放的是正确的,在此之前有另外一个版本,在每次读取完文件退出主程序时,跳转xutility发生读写权限冲突,因为string对象维护的是一段内存,而读入二进制文件保存的是内存地址,在下一次读取时这段内存地址发生变化,所以出现错误。解决办法,将四个人员类中的变量定义为char类型的数组。仓库也保存了一份string编写的源码,想看错误可以直接下上跑一下
参考:C++类对象进行文件读取后出现的xutility错误
解决string类型读写问题后,代码没有报错但是一旦运行检索类的功能,出现变量堆栈损坏,首先考虑的是数组中输入的字符超过了定义的长度,所以各个char类型长度要合理定义,查了一下果然是这个问题
参考:有关Run-Time Check Failure #2 - Stack around the variable ‘XXX’ was corrupted.错误的解决方法



