付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、学生实验报告学院:软件与通信工程学院课程名称:C+课程设计专业班级:10软件1班姓名:黄道炜学号:0103815学生实验报告(5)学生姓名黄道炜学号0103815同组人:无实验项目公司员工管理信息系统必修R选修口演示性实验口验证性实验口操作性实验。综合性实验实验地点W102实验仪器台号指导教师赵晓平实验日期及节次2011.11.9(三)567节2011.11.16(三)567节2011.11.23(三)567节一、实验综述1、实验目的及要求利用文件处理方式,并综合运用C+面向对象编程实现一个公司员工管理信息系统。实验题:设计一个虚基类Staff(员工),包含编号、姓名和年龄保护数据成员以及相关
2、的成员函数;由Staff派生出工程师类Engineer,包含专业和职称保护数据成员和相关的成员函数;再由Staff派生出领导类Leader,包括职务和部门保护数据成员和相关的成员函数;然后由Engineer和Leader类派生出主任工程师类Chairman。设计一个利用文件处理方式实现对公司员工(包括工程师、领导和主任工程师)进行管理,具备增加数据、更新数据、查询数据、删除数据以及重组文件的功能。要求将公司人员信息存储在数据文件staff.dat中,为存储方便,可以定义索引文件,在索引文件中,每一项包括删除标志、编号,记录在数据文件staff.dat中的相对位置和人员类型,使用索引对数据文件进
3、行定位操作。实验要求:认真完成实验题,能正确运行,提交实验报告并上传程序,实验报告要求写出操作步骤、结果、问题、解决方法、体会等。2、实验仪器、设备或软件计算机、VC+6Qoffice、相关的操作系统等。二、实验过程(实验步骤、记录、数据、分析)#include<iostream>#include<fstream>#include<iomanip>usingnamespacestd;#defineMAX_ERROR_MESSAGE_LEN100classErrorprivate:charmessageMAX_ERROR_MESSAGE_LEN;public:
4、Error(charmes="一般性异常!")strcpy(message,mes);voidShow()constcout<<message<<endl;charGetChar(istream&in=cin)charch;while(ch=in.peek()!=EOF&&(ch=in.get()=''|ch='/t');returnch;boolUserSaysYes()charch;boolinitialResponse=true;doif(initialResponse)cout<&
5、lt;"(y,n)?"elsecout<<"用y或n回答:"while(ch=GetChar()='/n');initialResponse=false;while(ch!='y'&&ch!='Y'&&ch!='n'&&ch!='N');while(GetChar()!='/n')if(ch='y'|ch='Y')returntrue;elsereturnfalse;c
6、lassStaffprotected:charnum8;charname12;intage;public:Staff()virtualStaff()constchar*GetNum()constreturnnum;virtualvoidInput()=0;virtualvoidShow()const=0;virtualvoidRead(fstream&f)const=0;virtualvoidWrite(fstream&f)const=0;classEngineer:virtualpublicStaffprotected:charmajor12;charprof12;publi
7、c:Engineer()virtualEngineer()voidInput()cout<<"编号:"cin>>num;cout<<"姓名:"cin>>name;cout<<"年龄:"cin>>age;cout<<"专业:"cin>>major;cout<<"职称:"cin>>prof;voidShow()constcout<<setw(8)<<num&
8、lt;<setw(12)<<name<<setw(5)<<age<<setw(12)<<major<<setw(12)<<prof<<endl;voidRead(fstream&f)constf.read(char*)this,sizeof(Engineer);voidWrite(fstream&f)constf.write(char*)this,sizeof(Engineer);classLeader:virtualpublicStaffprotected:charjob12
9、;chardep12;public:Leader()virtualLeader()voidInput()cout<<"编号:";cin>>num;cout<<"姓名:";cin>>name;cout<<"年龄:";cin>>age;cout<<"职务:";cin>>job;cout<<"部门:";cin>>dep;)voidShow()const(cout<<set
10、w(8)<<num<<setw(12)<<name<<setw(5)<<age<<setw(12)<<job<<setw(12)<<dep<<endl;)voidRead(fstream&f)constf.read(char*)this,sizeof(Leader);voidWrite(fstream&f)constf.write(char*)this,sizeof(Leader);classChairman:publicEngineer,publicLead
11、erpublic:Chairman()virtualChairman()voidInput()cout<<"编号:"cin>>num;cout<<"姓名:";cin>>name;cout<<"年龄:";cin>>age;cout<<"专业:";cin>>major;cout<<"职称:";cin>>prof;cout<<"职务:";cin>
12、>job;cout<<"部门:";cin>>dep;voidShow()constcout<<setw(8)<<num<<setw(12)<<name<<setw(5)<<age<<setw(12)<<major<<setw(12)<<prof<<setw(12)<<job<<setw(12)<<dep<<endl;voidRead(fstream&f)con
13、stf.read(char*)this,sizeof(Chairman);voidWrite(fstream&f)constf.write(char*)this,sizeof(Chairman);structIndexTypebooldelTag;charnum8;intposition;charstaffType;#defineNUM_OF_INCREMENT1000classStaffManageprivate:fstreamfile;IndexType*indexTable;intmaxSize;intcount;voidAddIndexItem(constIndexType&a
14、mp;e);voidAddData();voidUpdataData();voidSearchData();voidDeleteData();voidPack();public:StaffManage();virtualStaffManage();voidRun();StaffManage:StaffManage()ifstreamindexFile("staff.idx",ios:binary);if(!indexFile.fail()indexFile.seekg(0,ios:end);count=indexFile.tellg()/sizeof(IndexType);
15、maxSize=count+NUM_OF_INCREMENT;indexTable=newIndexTypemaxSize;indexFile.seekg(0,ios:beg);inti=0;indexFile.read(char*)&indexTablei+,sizeof(IndexType);while(!indexFile.eof()indexFile.read(char*)&indexTablei+,sizeof(IndexType);indexFile.close();elsecount=0;maxSize=count+NUM_OF_INCREMENT;indexTa
16、ble=newIndexTypemaxSize;ifstreamiFile("staff.dat");if(iFile.fail()ofstreamoFile("staff.dat");if(oFile.fail()throw("打开文件失败!");oFile.close();elseiFile.close();file.open("staff.dat",ios:in|ios:out|ios:binary);if(file.fail()throw("打开文件失败!");StaffManage:S
17、taffManage()ofstreamindexFile("staff.idx",ios:binary);for(inti=0;i<count;i+)indexFile.write(char*)&indexTablei,sizeof(IndexType);indexFile.close();file.close();voidStaffManage:AddIndexItem(constIndexType&e)if(count>=maxSize)maxSize+=NUM_OF_INCREMENT;IndexType*tmpIndexTable=ne
18、wIndexTypemaxSize;for(inti=0;i<count;i+)tmpIndexTablei=indexTablei;deleteindexTable;indexTable=tmpIndexTable;indexTablecount+=e;voidStaffManage:AddData()Staff*pStaff;IndexTypeitem;item.delTag=false;file.seekg(0,ios:end);dodocout<<"人员类型(e:工程师,l:领导,c:主任工程师);"cin>>item.staffTyp
19、e;item.staffType=tolower(item.staffType);while(item.staffType!='e'&&item.staffType!='l'&&item.staffType!='c');if(item.staffType='e')pStaff=newEngineer;elseif(item.staffType='l')pStaff=newLeader;elsepStaff=newChairman;pStaff->Input();strcpy(i
20、tem.num,pStaff->GetNum();item.position=file.tellg();AddIndexItem(item);pStaff->Write(file);deletepStaff;cout<<"继续添加吗"while(UserSaysYes();voidStaffManage:UpdataData()Staff*pStaff;IndexTypeitem;item.delTag=false;charnum12;cout<<"人员编号:"cin>>num;intpos;for(pos
21、=0;pos<count;pos+)if(strcmp(num,indexTablepos.num)=0&&!indexTablepos.delTag)break;if(pos<count)file.seekg(indexTablepos.position,ios:beg);if(indexTablepos.staffType='e')pStaff=newEngineer;elseif(indexTablepos.staffType='l')pStaff=newLeader;elsepStaff=newChairman;pStaff-
22、>Read(file);pStaff->Show();docout<<"人员类型(e:工程师,l:领导,c:主任工程师);"cin>>item.staffType;item.staffType=tolower(item.staffType);while(item.staffType!='e'&&item.staffType!='l'&&item.staffType!='c');if(item.staffType='e')pStaff=newEn
23、gineer;elseif(item.staffType='l')pStaff=newLeader;elsepStaff=newChairman;pStaff->Input();if(item.staffType=indexTablepos.staffType)strcpy(indexTablepos.num,pStaff->GetNum();file.seekg(indexTablepos.position,ios:beg);elseindexTablepos.delTag=true;strcpy(item.num,pStaff->GetNum();file
24、.seekg(0,ios:end);item.position=file.tellg();AddIndexItem(item);pStaff->Write(file);deletepStaff;elsecout<<"查无此人!"<<endl;file.clear();voidStaffManage:SearchData()Staff*pStaff;charnum12;cout<<"人员编号:"cin>>num;intpos;for(pos=0;pos<count;pos+)if(strcmp(n
25、um,indexTablepos.num)=0&&!indexTablepos.delTag)break;if(pos<count)file.seekg(indexTablepos.position,ios:beg);if(indexTablepos.staffType='e')pStaff=newEngineer;elseif(indexTablepos.staffType='l')pStaff=newLeader;elsepStaff=newChairman;pStaff->Read(file);pStaff->Show()
26、;deletepStaff;elsecout<<"查无此人!"<<endl;file.clear();voidStaffManage:DeleteData()Staff*pStaff;IndexTypeitem;item.delTag=false;charnum12;cout<<"人员编号:"cin>>num;intpos;for(pos=0;pos<count;pos+)if(strcmp(num,indexTablepos.num)=0&&!indexTablepos.delTag
27、)break;if(pos<count)file.seekg(indexTablepos.position,ios:beg);if(indexTablepos.staffType='e')pStaff=newEngineer;elseif(indexTablepos.staffType='l')pStaff=newLeader;elsepStaff=newChairman;pStaff->Read(file);cout<<"被删除记录为:"<<endl;pStaff->Show();indexTabl
28、epos.delTag=true;deletepStaff;cout<<"删除成功!"<<endl;elsecout<<"删除失败!"<<endl;file.clear();voidStaffManage:Pack()ofstreamoFile("tem.dat");oFile.close();fstreamoutFile("tem.dat",ios:app|ios:binary);Staff*pStaff;intcur_count=0;for(intpos=0;po
29、s<count;pos+)if(!indexTablepos.delTag)file.seekg(indexTablepos.position,ios:beg);indexTablepos.position=file.tellg();if(indexTablepos.staffType='e')pStaff=newEngineer;elseif(indexTablepos.staffType='l')pStaff=newLeader;elsepStaff=newChairman;pStaff->Read(file);pStaff->Write(
30、outFile);deletepStaff;indexTablecur_count+=indexTablepos;count=cur_count;file.close();outFile.close();remove("staff.dat");rename("tem.dat","staff.dat");file.open("staff.dat",ios:in|ios:out|ios:binary);voidStaffManage:Run()intselect;docout<<"请选择:&quo
31、t;<<endl;cout<<"1.增加数据"<<endl;cout<<"2.更新数据"<<endl;cout<<"3.查询数据"<<endl;cout<<"4.删除数据"<<endl;cout<<"5.重组文件"<<endl;cout<<"6.退出"<<endl;cin>>select;while(cin.get()!='n');switch(select)case 1:AddData();break;case 2:UpdataData();break;case 3:SearchData();break;case 4:DeleteData();break;case 5:Pack();break;while(select!=6);intmain()tryStaffManageobjStaffManage;objStaffManage.Run();catch(Errorerr)err.Show();system("PAUSE");return0;三、结论
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年一本院校理科平行志愿投档分数线
- 年产10万吨沥青混凝土建设项目环境影响报告表
- 第4章 外汇市场
- 2025-2026学年追溯历史的教学设计
- 八年级下册自由平等的真谛教案
- Unit 2 Vacation Plans教学设计小学英语五年级下册广东版(开心英语)
- 2025年雪地徒步杖雪托设计
- 第13课 香港和澳门回归祖国八年级下册历史同步教学设计
- 多媒体作品的开发教学设计中职专业课-多媒体技术及应用-计算机类-电子与信息大类
- Lesson 75 SOS教学设计初中英语第二册新概念英语
- 初三道德与法治中考复习:开放性设问之倡议书、标语与活动方案专项突破教案
- 2025年合肥高新区社区工作者招聘考试试卷真题
- 2026年安徽省检察机关招聘书记员考试真题
- 2025年荣耀AI隐私安全白皮书
- 2026届山东省聊城市临清市重点达标名校中考押题生物预测卷含解析
- 2026年高考(湖南卷)化学试题及答案
- 2025 地中海气候的特点和成因课件
- 围手术期营养支持指南
- 2026年山东高考英语真题试卷(新课标卷)(+答案)
- DB4101∕T 145-2025 城市道路管线综合规划规范
- 麦凯66表格(完全版)
评论
0/150
提交评论