1、高校人员管理系统c+课程设计报告 C+面向对象程序综合设计实验报告 2014年11月4日要求:设计一个程序,用于管理某高校教师、一般员工、学生的数据。学生的数据包括姓名、年龄、身份证号、年级和学费;教师的数据包括姓名、年龄、身份证号、课时和课时费;一般员工的数据包括姓名、年龄、身份证号和月薪。管理系统分析:各类的分析:本程序包含五个类,分别是:基本信息类,学生类,教师类,职工类,系统管理类。对象模型图如下: 基本信息类 系统管理类输入人员信息查询人员信息界面显示人员类别编号类别名姓名年龄身份证号数据输入数据输出取人员类别编号 教 师 类 课 时 课时费 数据输入 数据输出 学 生 类 年 级
2、学 费 数据输入 数据输出 一般员工类月 薪 数据输入 数据输出基本信息类:数据输入:输入各人员信息:姓名,年龄,身份证号;数据输出:输出各人员信息:人员类别编号,姓名,年龄,身份证号;学生类:数据输入:除了继承基本信息类的数据输入功能外,增加了输入“年级,学费”信息的功能。数据输出:除了继承基本信息类的数据输出功能外,增加了输出“年级,学费”信息的功能。教师类:数据输入:除了继承基本信息类的数据输入功能外,增加了输入“课时,课时费”信息的功能。数据输出:除了继承基本信息类的数据输出功能外,增加了输出“课时,课时费”信息的功能。一般员工:数据输入:除了继承基本信息类的数据输入功能外,增加了输入
3、“月薪”信息的功能。数据输出:除了继承基本信息类的数据输出功能外,增加了输出“月薪”信息的功能。基类和派生类设计: 基类(Record) 数据成员 Int num; char name; int age; int ID_number; 成员函数 Int Get_num; char Get_nme; int Get_age; int Get_ID_number; Void Input; void Output;3个人员类除了继承基类Record的数据外,类Student还增加了数据成员年级Grade,学费Spent;类Teacher增加了数据成员课时Hour,课时费Hourly_wages;类S
4、taff增加了数据成员月薪Salary。系统管理类System的主要操作是:成员函数In_information负责输入人员信息,成员函数Search负责查询人员信息,成员函数Interface负责界面的输出。系统管理类的设计:1、当系统启动完成后,自动将人员信息从数据文件中读入内存各对象数组中。这个功能由系统管理类System的构造函数自动调用函数save完成。如 System:system() save(); 所有人员的信息都存在一个数据文件中,由于不同种类人员信息的长度不同,如何从数据文件里将这些数据读出并存放到各自相对应的对象数组中是一个问题,解决的方法是:每次从数据文件中读取基类大小
5、的一条信息存入基类对象中,并获得这条信息的种类编号,通过人员类别编号可以判定人员的类别,然后将指针回指到这条信息的开头,从人员文件中重新读取完整的人员信息,存入对应的对象数组中。如:基本信息类Record的对象是A学术类student的对象是Bj1从数据文件里将相关的学生数据读出并存放到对象Bj1中,有关程序段如下:fstream datafile(fileName,ios:in|ios:out|ios:binary); datafile.read(char*)&A,sizeof(Record); while(!datafile.eof() a=A.Get_num(); switch(a) c
6、ase 1: datafile.seekp(-1*sizeof(class Record),ios:cur); datafile.read(char*)&Bj1,sizeof(Student); 2、信息的输入信息的输入功能由成员函数In_information 来完成,他根据要输入人员的类别分别调用对应的人员信息输入功能函数完成本类人员的输入。有3个类别的人员信息输入函数: Void infor1; /输入学生类对象数据 Void infor2; /输入教师类对象数据 Void infor3 /输入一般员工对象数据下面以infor1为例说明一条人员信息输入的实现过程:void System:
7、infor1() /输入学生对象数据 Student A; fstream datafile(fileName,ios:in|ios:out|ios:binary); datafile.seekp(0,ios:end); /写指针指到文件尾部 A.Input(); datafile.write(char*)&A,sizeof(class Student); Bj1=A; datafile.close(); 函数infor1完成一条学生对象信息的输入。新增加的学生信息添加在文件的尾部,基本信息类Record对象A的属性值的录入是调用学生类Student提供的数据输入成员Input完成,学生类对象
8、信息输入文件的同时,也保存一份在内存学生类对象数组B中。3、信息的查询信息查询功能由成员函数Search 来完成。它接收从键盘输入的人员类别编号和类别名,在对应的对象数组中查找,找到后通过调用对象的数据输出成员函数Output来显示人员信息。例如查询人员信息的程序段如下:If(strcmp(ch,Bs.Getname()=0) Bs.Output(); .4、界面的设计和实现程序运行主要由System类中的成员函数Interface来完成,通过选择不同的菜单选项调用不同的成员函数完成各自的功能。例如程序运行的主界面如下: *Welcome* *Personal Information Mana
9、gement System* 1.Input the information of personal 2.Found information and showing 3.Exit Please selecting(1-4):程序运行后结果:程序调试问题:在姓名输入的时候,如果输入字符中有“ ”,程序会被终止,所以个人觉得姓名的输入应该用gets()函数,这样就可以把中英文的名字都输入了。在询问用户“是否继续输入(y/n)?”以后的程序没有做异常处理。如果输入的不是y/n,程序将自动终止。另外,根据姓名来查找人员信息相当的不科学,因为可能有重名的情况。改成用身份证号来查询人员信息更靠谱,而且身份
10、证号也不应该定义为int型,有的身份证尾号是X,所以定义为char型更适合。实验心得:写如此长的程序还是第一次,即便是有书上的内容作为参考,感觉也相当的困难。而且该程序里还含有多个类,基类派生类,又是一个多文件程序,这些对我这种写代码少的人来说简直就是:蜀道难,难于上青天!值得高兴的是虽然手速慢,英语差,但还是把程序敲完了,而且错误也检查改正了。忽略掉那些拼写错误的单词,这也是一件挺有成就感的事。不过调试的时候又出问题了,程序编译过了,但什么东西都不显示。当时跳楼的心都有了,不过还好有度娘在,解决了问题。原来是“super.dat”这个文件没有建立,菜鸟就是菜鸟,如此低级的错误都犯了。幸苦的同
11、时也收获了不少,以前写代码总是照着书上的格式写,好多东西都似是而非,似懂非懂。这次领悟到的东西还是不少的,比如说弄清了cout最后加endl的作用和不加的作用,如何实现一个系统,如何将数据文件中的信息读入内存对象数组最后感谢老师两月的幸苦,感谢旁边教我如何建立和运行多文件程序的那位同学,感谢无所不能的度娘源代码如下(改进版):/Record.hclass Recordprotected: int num; char name30; int age; int ID_number;public: Record(char* R_name= ,int age1=0,int ID_number1=0);
12、 Record() int Get_num(); int Get_age(); int Get_ID_number(); char *Getname(); void Input(); void Output();class Student:public Record int grade; float spent;public: Student(char* R_name= ,int age1=0,int ID_number1=0,int grade1=0,int spent1=0); Student() void Input(); void Output();class Teacher:publ
13、ic Record float hour; float hourly_wages;public: Teacher(char* R_name= ,int age1=0,int ID_number1=0,float hour1=0,float hourly_wages1=0); Teacher() void Input(); void Output(); /* data */;class Staff:public Record float salary;public: Staff(char* R_name= ,int age1=0,int ID_number1=0,float salary=0);
14、 Staff() void Input(); void Output(); /* data */;class System Record A; Student B20; Teacher C20; Staff D20; static int j1,j2,j3; void infor1(); void infor2(); void infor3(); void save(); void Search1(int h,char ch20); void Interface1();public: System(); void In_information(); void Search(); void In
15、terface(); /* data */;/Record.cpp#include #include #include #include #include Record.husing namespace std;#define N 30char fileName=super.dat;Record:Record(char* R_name,int age1,int ID_number1) strcpy(name,R_name); age=age1; ID_number=ID_number1;int Record:Get_num() return num;int Record:Get_age() r
16、eturn age;int Record:Get_ID_number() return ID_number;char* Record:Getname() return name;void Record:Input() coutname; coutage; coutID_number;void Record:Output() coutendl; couttt Information Of Personal:endl; couttt Classfied number:numendl; couttt Name:nameendl; couttt age:ageendl; couttt ID_numbe
17、r:ID_numberendl;Student:Student(char* R_name,int age1,int ID_number1,int grade1,int spent1):Record(R_name,age1,ID_number1) num=1; grade=grade1; spent=spent1;void Student:Input() Record:Input(); coutgrade; coutspent;void Student:Output() Record:Output(); couttt Grade:gradeendl; couttt Spent:spentendl
18、;Teacher:Teacher(char* R_name,int age1,int ID_number1,float hour1,float hourly_wages1):Record(R_name,age1,ID_number1) num=2; hour=hour1; hourly_wages=hourly_wages1;void Teacher:Input() Record:Input(); couthour; couthourly_wages;void Teacher:Output() Record:Output(); couttt Hour:hourendl; couttt Hour
19、ly_wages:hourly_wagesendl;Staff:Staff(char* R_name,int age1,int ID_number1,float salary1):Record(R_name,age1,ID_number1) num=3; salary=salary1;void Staff:Input() Record:Input(); coutsalary;void Staff:Output() Record:Output(); couttt Salary:salaryendl;int System:j1=0;int System:j2=0;int System:j3=0;S
20、ystem:System() save();void System:Interface1() coutnnn; couttt *According to the class information*endl; couttt *Selecting the information*endl; couttt 1.Student endl; couttt 2.Teacher endl; couttt 3.Staff endl; couttt 4.Exit endl; coutrev1; switch(rev1) case 1: infor1(); break; case 2: infor2(); br
21、eak; case 3: infor3(); break; case 4: Interface(); break; default: couttt There is no this personal!endl; continue; coutttt Information stored success!endl; coutt; coutendl; if(!(t=Y|t=y) again=0; Interface();void System:infor1() Student A; fstream datafile(fileName,ios:in|ios:out|ios:binary); dataf
22、ile.seekp(0,ios:end); A.Input(); datafile.write(char*)&A,sizeof(class Student); Bj1=A; datafile.close();void System:infor2() Teacher A; fstream datafile(fileName,ios:in|ios:out|ios:binary); datafile.seekp(0,ios:end); A.Input(); datafile.write(char*)&A,sizeof(class Teacher); Cj2=A; datafile.close();v
23、oid System:infor3() Staff A; fstream datafile(fileName,ios:in|ios:out|ios:binary); datafile.seekp(0,ios:end); A.Input(); datafile.write(char*)&A,sizeof(class Staff); Dj3=A; datafile.close();void System:save() int a; fstream datafile(fileName,ios:in|ios:out|ios:binary); datafile.read(char*)&A,sizeof(
24、Record); while(!datafile.eof() a=A.Get_num(); switch(a) case 1: datafile.seekp(-1*sizeof(class Record),ios:cur); datafile.read(char*)&Bj1,sizeof(Student); j1+; break; case 2: datafile.seekp(-1*sizeof(class Record),ios:cur); datafile.read(char*)&Cj2,sizeof(Teacher); j2+; break; case 3: datafile.seekp
25、(-1*sizeof(class Record),ios:cur); datafile.read(char*)&Dj3,sizeof(Staff); j3+; break; default: break; datafile.read(char*)&A,sizeof(Record); datafile.close();void System:Search1(int h,char ch20) int s=0,found=0; switch(h) case 1: while(sN) if(strcmp(ch,Bs.Getname()=0) Bs.Output(); coutttt*endl; found=1; s+; break; case 2: while(sN) if(strcmp(ch,Cs.Getname()=0) Cs.Output(); coutttt*endl; found=1; s+; break; case 3: while(sN) if(strcmp(ch,Ds.Getname()=0) Ds.Output(); coutttt*endl; found=1; s+; break; if(found=0) coutnntt Sorry,dont seek out thi