c课程设计职工工资管理系统_第1页
c课程设计职工工资管理系统_第2页
c课程设计职工工资管理系统_第3页
c课程设计职工工资管理系统_第4页
c课程设计职工工资管理系统_第5页
免费预览已结束,剩余12页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、题目C+面向对象程序设计课程设计清单:5 小题+职工工资管理系统(类、链表实现)姓名:学号:专业:计算机科学与技术学院:指导教师:2018年6月17日Part1:小程序练习1类的继承定义一个point类,包含私有数据成员x,y,成员函数包括无参构造函数,带参构造函数,set和get属性函数。定义circle类,从point类公有派生,增加数据成员半径r,成员函数包括无参构造函数,带参构造函数,计算面积函数getarea。在main函数中定义一个circle的对象,并计算其面积。/*1 .定义Point类,设置其成员函数(构造函数,拷贝构造函数和析构函数)以及个属性函数。2 .定义circle类

2、,设置其成员函数(构造函数,拷贝构造函数和析构函数)以及获取半径积并获取面积的函数getarea()。3 .在主函数中定义类的对象c1并初始化r=2。再调用getarea()函数输出面积*/#includeusingnamespacestd;classpoint(public:point()point(intx,inty)voidset_x(intx)this-x=x;intget_x()./定义 point 类returnx;)voidset_y(inty)(this-y=y;)intget_y()(returny;)private:intx;inty;);classcircle:publi

3、cpointpoint/私有对象 xy/circle 类公有派生setx()sety()getx()gety()r的函数get_r()计算面(public:circle()circle(doubler,intx,inty):point(x,y)this-r=r;doubleget_r()returnr;doublegetarea()return(3.14*r*r);运行结果分析:主函数中r=2,输出圆面积12.56private:intr;/circle 私有对象 r;intmain()circlec1(2,3,6);coutr=c1.get_r()endl;cout该圆面积=c1.getar

4、ea()endl;system(pause);return0;/发现问题:定义的 r 好像只显示出 int 类型2运算符重载,友元函数和this指针定义一个计数器类counter,具备自增,自减功能(前后缀)功能。在main函数里测试该类。/*1 .定义 counter 类,私有成员数据 weight,设置其成员函数(构造函数和析构函数)2 .重载自加自减运算符和运算符。3 .在主函数中实现运算符重载。4 .友元函数需要声明。*/#include#includeusingnamespacestd;classcounter;istream&operator(istream&is,

5、counter&a);ostream&operator(istream&is,counter&a);/声明友元,重载输入运算符friendostream&operator(istream&/运算符重载实现ina.P;if(!in)cerr输入错误!endl;returnin;ostream&operator(ostream&out,counter&a)/运算符重载实现(outa.P;returnout;)intmain()(counterc1(236),c2(632);coutc1=c1endlc2=c2endlcout+

6、c1=+c1,doubleP;public:counter()/无参构造函数counter(doublep):P(p)/带参构造函数counteroperator+();/重载前置+counteroperator+(int);/重载后置+counteroperator-();/重载前置-counteroperator-(int);/重载后置-后置+重载前置-重载后置-in,counter&a)/测试coutc1+=c1+endl;coutc2-=c2-endl;cout-c2=-c2endl;system(pause);return0;)运行结果分析:定义c1的值为236,c2的值为6

7、32;此时+c1的数值为237;c1+输出时为237,输出后为238;此时c2-输出时为632;-c2输出时为630,输出后为630;3虚函数和抽象类定义一个抽象类shape,包括公有的计算面积area函数,计算体积volume函数,输出基本信息函数printinfo(三个函数均为纯虚函数)。从shape公有派生point类,增加私有数据成员x,y坐标,以及构造函数,析构函数。从point公有派生circle类,增加私有数据成员半径r,以及构造函数,析构函数。从circle公有派生cylinder类,增加私有数据成员高度h,以及构造函数,析构函数。(在定义三个派生类的过程中,自己考虑需要重定义

8、哪个虚函数)。在main函数中, 定义shape类的指针, 指向派生类的对象, 输出三类对象的基本信息,面积, 体积; (将shape指针改为引用再尝试)。/*1 .先定义基类 shape。设置三个纯虚函数并且声明:声明计算面积纯虚函数 area();声明计算体积纯虚函数 volume();声明输出基本信息纯虚函数 printinfo();2 .定义类 point 共有继承自类 shape。并且在该类中实现三个纯虚函数。3 .定义类 circle 共有继承自类 point。并且在该类中实现三个纯虚函数。4 .定义类 cylinder 共有继承自类 circle。并且在该类中实现三个纯虚函数。5

9、 .在主函数中分别创建类point的对象a,circle的对象b,cylinder的对象 c,并初始化;6 .在主函数中分别定义 shape 类的指针和引用,调用printinfo()函数。*/#include#include#definePi3.141516usingnamespacestd;classshape(public:virtualdoublearea()=0;/三个纯虚函数:面积,体积,基本输出virtualdoublevolume()=0;virtualvoidprintinfo()=0;);classpoint:publicshape/shape 派生的point 类;点并没

10、有体积面积,所以只写 printinfo 函数(public:point()point(doublex,doubley)this-x=x;this-y=y;voidprintinfo()coutx=x,y=yr=r;doublearea()returnPi*r*r;voidprintinfo()point:printinfo();cout半径为rendl;cout圆的面积是area()h=h;/*doublearea()return2*Pi*this-r+circle:area();/未实现计算圆柱表面积*/doublevolume()returnh*circle:area();voidpri

11、ntinfo()circle:printinfo();cout高为hendl;cout圆柱的体积是volume()endl;cylinder()private:doubleh;intmain()cylindertemp(6,2,3,3);shape*s;/实例化一个圆柱对象s=&temp;(*s).printinfo();printf(n);couttemp 中数据构成的圆面积为area()endl;cout体积为(*s).volume()endl;system(pause);return0;4模板编写一个使用类模板对数组进行查找、求元素和、重载下标口运算符,以及输出的程序。1)设计一

12、个类模板:形式1为templateclassArray;形似2为templateclassArray;用于对T类型的数组进行构造和输出;2)产生模板类Array和Array进行测试;Array和Array进行测试。/先实现第(2)小题#include#includeusingnamespacestd;templateclassArrayprivate:T*list;intsize;public:Array(intsize=10);/构造函数Array();/析构函数T&operator(inti);/重载口”constT&operator(inti)const;Tsum(int

13、n);intsearch(Te,intn);intgetSize()const;voidresize(intsz);templateArray:Array(intsz)/构造函数assert(sz=0);size=sz;list=newTsize;templateArray:Array()/析构函数deletelist;/重载下标运算符口templateT&Array:operator(intn)assert(n=0&nsize);returnlistn;templateconstT&Array:operator(intn)constassert(n=0&nsi

14、ze);returnlistn;/取当前数组的大小templateintArray:getSize()constreturnsize;/将数组大小修改为 sztemplatevoidArray:resize(intsz)assert(sz=0);if(sz=size)return;T*newList=newTsz;intn=(szsize)?sz:size;for(inti=0;in;i+)newListi=listi;deletelist;/删除原数组list=newList;/使 list 指向新数组size=sz;/更新 sizetemplateTArray:sum(intn)Tsum=

15、list0;for(inti=1;in;i+)sum+=listi;returnsum;/查找templateintArray:search(Te,intn)for(inti=0;in;i+)if(listi=e)returni;return-1;intmain()Arraya(5);inti,n,m,count=0;coutn;for(i=1;im;if(count=a.getSize()a.resize(count*2);acount+=m;for(i=0;icount;i+)coutai;coutendl;cout数组和为:a.sum(n)endl;cout数字 4 在数组中的位置是a.

16、search(4,n)endl;/*Array进行测试Arraya(5);inti,n,m,count=0;coutn;for(i=1;im;if(count=a.getSize()a.resize(count*2);acount+=m;)for(i=0;icount;i+)3)产生模板类coutsetw(8)ai;coutendl;cout数组和为:a.sum(n)endl;cout数字 4 在数组中的位置是:a.search(4,n)endl;*/return0;)/然后实现第(3)小题#include#includeusingnamespacestd;templateclassArray

17、private:T*list;public:Array();/构造函数Array();/析构函数T&operator(inti);/重载口”constT&operator(inti)const;Tsum();intsearch(Te););templateArray:Array()/构造函数list=newTn;)templateArray:Array()/析构函数deletelist;)/重载下标运算符口templateT&Array:operator(inti)returnlisti;)templateTArray:sum()Tsum=list0;for(inti=

18、1;in;i+)sum+=listi;returnsum;)/查找templateintArray:search(Te)for(inti=0;in;i+)if(listi=e)returni;return-1;)intmain()Arraya;inti,n,m,count=0;coutn;for(i=1;im;acount+=m;)for(i=0;icount;i+)coutai;coutendl;cout数组和为:a.sum()endl;cout数字 4 在数组中的位置是a.search(4)endl;/*Array进行测试Arraya;inti,n,m,count=0;coutn;for(

19、i=1;im;if(count=a.getSize()a.resize(count*2);acount+=m;)for(i=0;icount;i+)coutsetw(8)ai;coutendl;cout数组和为:a.sum(n)endl;cout数字 4 在数组中的位置是a.search(4,n)endl;*/return0;)运行结果:5文件读写定义学生类数组,有N个人(N=5),包括姓名和语数外三名课的成绩,通过重载运算符实现学生数组的文件读写。/*1.定义 student 类,私有数据成员字符数组 name20;2.定义运算符重载;3.在住函数中定义 student 类数组 sN;并以输

20、出和二进制的方式打开文件*/#include#include#include#defineN5usingnamespacestd;classstudent;ostream&operator(istream&is,student&s);classstudent/student 类:姓名+4 门成绩public:student。student(stringname,intchinese_socre,intmaths_score,intenglish_score)this-name=name;this-chinese_score=chinese_score;this-math

21、s_score=maths_score;this-english_score=english_score;friendostream&operator(ostream&os,students)/声明友元,重写s.chinese_scores.maths_scores.english_score(istream&is,student&s)s.chinese_scores.maths_scores.english_score;returnis;private:stringname;运行结果:intchinese_score;intma

22、ths_score;intenglish_score;intmain()inti;studentsN;for(i=0;isi;ofstreamofs(c:testtest.txt,ios_base:out);if(!ofs)cerrfileopenfailedendl;exit(1);for(i=0;iN;i+)/这个我也不太明白-ofs.write(reinterpret_cast(&si),sizeof(student);ifstreamifs(c:testtest.txt,ios_base:out);if(!ifs)cerrfileopenfailedendl;exit(1);f

23、or(i=0;iN;i+)ifs.read(reinterpret_cast(&si),sizeof(student);for(i=0;iN;i+)coutNext用来接收调用浏览函数时所传递过来的实参,用设置好的输出格式(print()、Display()以及while循环,不为空则开始打印信息。4、查询信息1)按工号查询声明链表指针ptr指向Next,cout、cin提示输入工号并赋值给code,匹配ptr中的m_code,显示匹配结果即可;2)按科室查询声明链表指针ptr指向Next,cout、cin提示输入科室并赋值给post,匹配ptr中的m_post,显示匹配结果即可;3)

24、双匹配查询声明链表指针ptr指向Next,cout、cin提示输入工号、科室并赋值给code、post,匹配ptr中的m_codem_post,显示匹配结果5、修改信息Search_Unique_Front()找到需修改的职工信息,再次赋值即可。需要用cout提示要输入的内容,接着用cin输入相应的内容。6、删除信息键盘输入的职工号,通过职工的工号code删除职工信息。链表指针匹配m_code并删除Head-Next7、计算科室平均工资在按科室查找SearchPost函数中定义一个int类型n(为科室人数),初始化为0;每添加一人,则sum+=ptr-m_Wage。用总的工资除以总人数,算出平

25、均工资。六、源代码#include#include#include#include#include#include#include#includeusingnamespacestd;intn=0;classemployeepublic:stringm_Code;/职工工号stringm_Name;stringm_phone;stringm_Sex;stringm_Post;/所在科室unsignedintm_Average;unsignedintm_Cash;unsignedintm_Wage;/链表节点的指针域-employee*Next;public:voidPrint();employe

26、e*Create(employee*Head);/创建一个链表voidRel(employee*Head);employee*Add(employee*Head);boolSearch(employee*Head);intSearchPost(employee*Head,stringpost);employee*Search_Unique_Front(employee*Head)voidDisplay_List(employee*Head);voidDisplay_Node(employee*pNode);employee*Modify(employee*Head);employee*Del(

27、employee*Head);voidSave_ByFile(employee*Head,fstream&ofile);employee*Sort(employee*Head);employee*employee:Create(employee*Head)/创建一个带头节点的空链表。Head=newemployee;if(!Head)cout分配内存失败!m_Code=;Head-m_Name=;Head-m_phone=;Head-m_Sex=;Head-m_Post=;Head-m_Wage=0;Head-Next=NULL;Head-m_Average=0;Head-m_Cash

28、=0;returnHead;voidemployee:Rel(employee*Head)/释放链表。employee*ptr;/声明一个操作用的指针。while(Head!=NULL)(ptr=Head;Head=Head-Next;deleteptr;/释放节点资源。)_employee*employee:Add(employee*Head)/输入职工信息通过职工的工号修改职工信息。声明链表指针ptr,调用查找函数/前插法添加数据。employee*pNew;/声明一个新节点。charagain;stringcode,name,sex,post,phone;unsignedintwage;

29、dopNew=newemployee;/数据域。 coutcode;coutendlname;coutendlphone;coutendlsex;coutendlpost;coutendlwage;while(cin.fail()cout请输入正确的工资数据。wage;coutm_Code=code;pNew-m_Name=name;pNew-m_phone=phone;pNew-m_Sex=sex;pNew-m_Post=post;pNew-m_Wage=wage;/指针域。pNew-Next=Head-Next;Head-Next=pNew;cout数席添加成功! 是否继续添加?(Y/N)

30、again;while(again=Y|again=y);returnHead;boolemployee:Search(employee*Head)/ 查询同时满足“姓名”和“科室”的职工信息。employee*ptr;stringname,post;ptr=Head-Next;coutpost;coutendlname;coutendltt 查询结果m_Name=name)&(ptr-m_Post=post)Display_Node(ptr);/打印满足条件的节点。returntrue;ptr=ptr-Next;/查询下一节点。coutn 无此职工的信息。Next;coutcode;

31、coutendltt 查询结果m_Code=code)Display_Node(ptr);returnptr;ptr=ptr-Next;coutn 无此职工的信息。Next;while(ptr)if(ptr-m_Post=post)sum+=ptr-m_Wage;n+;ptr=ptr-Next;cout科室post里的平均工资为:sum/nendl;return0;voidemployee:Print()coutsetw(10)left工号;coutsetw(10)left姓名;coutsetw(18)left电话;coutsetw(10)left性别;coutsetw(10)left科室;c

32、outsetw(10)left工资Next;couttt=所有职工信 Jl、=Next;voidemployee:Display_Node(employee*pNode)/在标准输出设备上输出。coutsetw(10)leftm_Codesetw(10)leftm_Namesetw(18)leftm_phonesetw(10)leftm_Sexsetw(10)leftm_Postsetw(10)leftm_Wageendl;/setw(10)表示占 10 个字符位置。employee*employee:Modify(employee*Head)/修改职工信息/修改单一个节点。employee*

33、ptr;ptr=Search_Unique_Front(Head);stringcode,name,sex,post,phone;unsignedintwage;if(ptr)couttt 你现在可以修改此职工的信息了endl;/数据域。coutcode;coutendlname;coutendlphone;coutendlsex;coutendlpost;coutendlwage;while(cin.fail()cout请输入正确的工资数据。wage;coutm_Code=code;ptr-m_Name=name;ptr-m_phone=phone;ptr-m_Sex=sex;ptr-m_P

34、ost=post;ptr-m_Wage=wage;elsecout没找到此职工的记录,无法修改。endl;returnHead;employee*employee:Del(employee*Head)/根据工号删除职工信息stringcode;employee*parentptr;employee*ptr_front;coutcode;parentptr=Head;ptr_front=Head-Next;while(ptr_front)if(ptr_front-m_Code=code)parentptr-Next=ptr_front-Next;deleteptr_front;returnHea

35、d;parentptr=ptr_front;ptr_front=ptr_front-Next;returnHead;voidemployee:Save_ByFile(employee*Head,fstream&ofile)employee*pNode;pNode=Head-Next;ofile.clear();/清除文件结束状态。while(pNode)ofilesetw(10)leftm_Codesetw(10)leftm_Namesetw(10)leftm_phonesetw(10)leftm_Sexsetw(10)leftm_Postsetw(10)leftm_WageNext;

36、cout数据文件保存成功!Next=NULL)|(Head-Next-Next=NULL)/此步条件判断非常有价值。cout数据节点数少于 2 个,不用排序!Next-Next;ptr_F=Head;Head-Next-Next=NULL;/到 I 此,分成了两个链表。/第三步。while(ptr)ptr_N=ptr-Next;ptr_F=Head;/ptr_F 的归位。while(ptr_F-Next)if(ptr-m_Wageptr_F-Next-m_Wage)ptr-Next=ptr_F-Next;ptr_F-Next=ptr;break;elseptr_F=ptr_F-Next;if(

37、ptr_F-Next=NULL)ptr-Next=ptr_F-Next;ptr_F-Next=ptr;/表示插到有序链表的最后面了。ptr=ptr_N;/归位,准备下一次排序。cout从高到低,排序成功!Create(st);fstreamiofile;/定义一个具有输入.输出的文件流对象 iofile。iofile.open(d:iofile.txt,ios_base:in|ios_base:out|ios_base:app);/文件以三种方式打开。if(!iofile)cout打开文件失败!endl;return-1;intmenu;while(1)*couttt*=*endl;couttt*endl;couttt*1.注册职工 2.修改信息 3.删除信息 4.信息查询*endl;couttt*5.保存文件 6.工资排行 7.信息显示 0.退出系统*endl;couttt*menu;while(cin.fail()cout

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论