




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、題目C+面向?qū)ο蟪绦蛟O(shè)計(jì)課程設(shè)計(jì)清單:5 小題+職工工資管理系統(tǒng)(類、鏈表實(shí)現(xiàn))姓名:學(xué)號:專業(yè):計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院:指導(dǎo)教師:2018年6月17日Part1:小程序練習(xí)1類的繼承定義一個point類,包含私有數(shù)據(jù)成員x,y,成員函數(shù)包括無參構(gòu)造函數(shù),帶參構(gòu)造函數(shù),set和get屬性函數(shù)。定義circle類,從point類公有派生,增加數(shù)據(jù)成員半徑r,成員函數(shù)包括無參構(gòu)造函數(shù),帶參構(gòu)造函數(shù),計(jì)算面積函數(shù)getarea。在main函數(shù)中定義一個circle的對象,并計(jì)算其面積。/*1 .定義Point類,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及個屬性函數(shù)。2 .定義circle類
2、,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及獲取半徑積并獲取面積的函數(shù)getarea()。3 .在主函數(shù)中定義類的對象c1并初始化r=2。再調(diào)用getarea()函數(shù)輸出面積*/#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的函數(shù)get_r()計(jì)算面(public:circle()circle(doubler,intx,inty):point(x,y)this-r=r;doubleget_r()returnr;doublegetarea()return(3.14*r*r);運(yùn)行結(jié)果分析:主函數(shù)中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;/發(fā)現(xiàn)問題:定義的 r 好像只顯示出 int 類型2運(yùn)算符重載,友元函數(shù)和this指針定義一個計(jì)數(shù)器類counter,具備自增,自減功能(前后綴)功能。在main函數(shù)里測試該類。/*1 .定義 counter 類,私有成員數(shù)據(jù) weight,設(shè)置其成員函數(shù)(構(gòu)造函數(shù)和析構(gòu)函數(shù))2 .重載自加自減運(yùn)算符和運(yùn)算符。3 .在主函數(shù)中實(shí)現(xiàn)運(yùn)算符重載。4 .友元函數(shù)需要聲明。*/#include#includeusingnamespacestd;classcounter;istream&operator(istream&is,
5、counter&a);ostream&operator(istream&is,counter&a);/聲明友元,重載輸入運(yùn)算符friendostream&operator(istream&/運(yùn)算符重載實(shí)現(xiàn)ina.P;if(!in)cerr輸入錯誤!endl;returnin;ostream&operator(ostream&out,counter&a)/運(yùn)算符重載實(shí)現(xiàn)(outa.P;returnout;)intmain()(counterc1(236),c2(632);coutc1=c1endlc2=c2endlcout+
6、c1=+c1,doubleP;public:counter()/無參構(gòu)造函數(shù)counter(doublep):P(p)/帶參構(gòu)造函數(shù)counteroperator+();/重載前置+counteroperator+(int);/重載后置+counteroperator-();/重載前置-counteroperator-(int);/重載后置-后置+重載前置-重載后置-in,counter&a)/測試coutc1+=c1+endl;coutc2-=c2-endl;cout-c2=-c2endl;system(pause);return0;)運(yùn)行結(jié)果分析:定義c1的值為236,c2的值為6
7、32;此時(shí)+c1的數(shù)值為237;c1+輸出時(shí)為237,輸出后為238;此時(shí)c2-輸出時(shí)為632;-c2輸出時(shí)為630,輸出后為630;3虛函數(shù)和抽象類定義一個抽象類shape,包括公有的計(jì)算面積area函數(shù),計(jì)算體積volume函數(shù),輸出基本信息函數(shù)printinfo(三個函數(shù)均為純虛函數(shù))。從shape公有派生point類,增加私有數(shù)據(jù)成員x,y坐標(biāo),以及構(gòu)造函數(shù),析構(gòu)函數(shù)。從point公有派生circle類,增加私有數(shù)據(jù)成員半徑r,以及構(gòu)造函數(shù),析構(gòu)函數(shù)。從circle公有派生cylinder類,增加私有數(shù)據(jù)成員高度h,以及構(gòu)造函數(shù),析構(gòu)函數(shù)。(在定義三個派生類的過程中,自己考慮需要重定義
8、哪個虛函數(shù))。在main函數(shù)中, 定義shape類的指針, 指向派生類的對象, 輸出三類對象的基本信息,面積, 體積; (將shape指針改為引用再嘗試)。/*1 .先定義基類 shape。設(shè)置三個純虛函數(shù)并且聲明:聲明計(jì)算面積純虛函數(shù) area();聲明計(jì)算體積純虛函數(shù) volume();聲明輸出基本信息純虛函數(shù) printinfo();2 .定義類 point 共有繼承自類 shape。并且在該類中實(shí)現(xiàn)三個純虛函數(shù)。3 .定義類 circle 共有繼承自類 point。并且在該類中實(shí)現(xiàn)三個純虛函數(shù)。4 .定義類 cylinder 共有繼承自類 circle。并且在該類中實(shí)現(xiàn)三個純虛函數(shù)。5
9、 .在主函數(shù)中分別創(chuàng)建類point的對象a,circle的對象b,cylinder的對象 c,并初始化;6 .在主函數(shù)中分別定義 shape 類的指針和引用,調(diào)用printinfo()函數(shù)。*/#include#include#definePi3.141516usingnamespacestd;classshape(public:virtualdoublearea()=0;/三個純虛函數(shù):面積,體積,基本輸出virtualdoublevolume()=0;virtualvoidprintinfo()=0;);classpoint:publicshape/shape 派生的point 類;點(diǎn)并沒
10、有體積面積,所以只寫 printinfo 函數(shù)(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();/未實(shí)現(xiàn)計(jì)算圓柱表面積*/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;/實(shí)例化一個圓柱對象s=&temp;(*s).printinfo();printf(n);couttemp 中數(shù)據(jù)構(gòu)成的圓面積為area()endl;cout體積為(*s).volume()endl;system(pause);return0;4模板編寫一個使用類模板對數(shù)組進(jìn)行查找、求元素和、重載下標(biāo)口運(yùn)算符,以及輸出的程序。1)設(shè)計(jì)一
12、個類模板:形式1為templateclassArray;形似2為templateclassArray;用于對T類型的數(shù)組進(jìn)行構(gòu)造和輸出;2)產(chǎn)生模板類Array和Array進(jìn)行測試;Array和Array進(jìn)行測試。/先實(shí)現(xiàn)第(2)小題#include#includeusingnamespacestd;templateclassArrayprivate:T*list;intsize;public:Array(intsize=10);/構(gòu)造函數(shù)Array();/析構(gòu)函數(shù)T&operator(inti);/重載口”constT&operator(inti)const;Tsum(int
13、n);intsearch(Te,intn);intgetSize()const;voidresize(intsz);templateArray:Array(intsz)/構(gòu)造函數(shù)assert(sz=0);size=sz;list=newTsize;templateArray:Array()/析構(gòu)函數(shù)deletelist;/重載下標(biāo)運(yùn)算符口templateT&Array:operator(intn)assert(n=0&nsize);returnlistn;templateconstT&Array:operator(intn)constassert(n=0&nsi
14、ze);returnlistn;/取當(dāng)前數(shù)組的大小templateintArray:getSize()constreturnsize;/將數(shù)組大小修改為 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;/刪除原數(shù)組list=newList;/使 list 指向新數(shù)組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數(shù)組和為:a.sum(n)endl;cout數(shù)字 4 在數(shù)組中的位置是a.
16、search(4,n)endl;/*Array進(jìn)行測試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)產(chǎn)生模板類coutsetw(8)ai;coutendl;cout數(shù)組和為:a.sum(n)endl;cout數(shù)字 4 在數(shù)組中的位置是:a.search(4,n)endl;*/return0;)/然后實(shí)現(xiàn)第(3)小題#include#includeusingnamespacestd;templateclassArray
17、private:T*list;public:Array();/構(gòu)造函數(shù)Array();/析構(gòu)函數(shù)T&operator(inti);/重載口”constT&operator(inti)const;Tsum();intsearch(Te););templateArray:Array()/構(gòu)造函數(shù)list=newTn;)templateArray:Array()/析構(gòu)函數(shù)deletelist;)/重載下標(biāo)運(yùn)算符口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數(shù)組和為:a.sum()endl;cout數(shù)字 4 在數(shù)組中的位置是a.search(4)endl;/*Array進(jìn)行測試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數(shù)組和為:a.sum(n)endl;cout數(shù)字 4 在數(shù)組中的位置是a.search(4,n)endl;*/return0;)運(yùn)行結(jié)果:5文件讀寫定義學(xué)生類數(shù)組,有N個人(N=5),包括姓名和語數(shù)外三名課的成績,通過重載運(yùn)算符實(shí)現(xiàn)學(xué)生數(shù)組的文件讀寫。/*1.定義 student 類,私有數(shù)據(jù)成員字符數(shù)組 name20;2.定義運(yùn)算符重載;3.在住函數(shù)中定義 student 類數(shù)組 sN;并以輸
20、出和二進(jìn)制的方式打開文件*/#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;運(yùn)行結(jié)果: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用來接收調(diào)用瀏覽函數(shù)時(shí)所傳遞過來的實(shí)參,用設(shè)置好的輸出格式(print()、Display()以及while循環(huán),不為空則開始打印信息。4、查詢信息1)按工號查詢聲明鏈表指針ptr指向Next,cout、cin提示輸入工號并賦值給code,匹配ptr中的m_code,顯示匹配結(jié)果即可;2)按科室查詢聲明鏈表指針ptr指向Next,cout、cin提示輸入科室并賦值給post,匹配ptr中的m_post,顯示匹配結(jié)果即可;3)
24、雙匹配查詢聲明鏈表指針ptr指向Next,cout、cin提示輸入工號、科室并賦值給code、post,匹配ptr中的m_codem_post,顯示匹配結(jié)果5、修改信息Search_Unique_Front()找到需修改的職工信息,再次賦值即可。需要用cout提示要輸入的內(nèi)容,接著用cin輸入相應(yīng)的內(nèi)容。6、刪除信息鍵盤輸入的職工號,通過職工的工號code刪除職工信息。鏈表指針匹配m_code并刪除Head-Next7、計(jì)算科室平均工資在按科室查找SearchPost函數(shù)中定義一個int類型n(為科室人數(shù)),初始化為0;每添加一人,則sum+=ptr-m_Wage。用總的工資除以總?cè)藬?shù),算出平
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;/鏈表節(jié)點(diǎn)的指針域-employee*Next;public:voidPrint();employe
26、e*Create(employee*Head);/創(chuàng)建一個鏈表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)/創(chuàng)建一個帶頭節(jié)點(diǎn)的空鏈表。Head=newemployee;if(!Head)cout分配內(nèi)存失??!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;/釋放節(jié)點(diǎn)資源。)_employee*employee:Add(employee*Head)/輸入職工信息通過職工的工號修改職工信息。聲明鏈表指針ptr,調(diào)用查找函數(shù)/前插法添加數(shù)據(jù)。employee*pNew;/聲明一個新節(jié)點(diǎn)。charagain;stringcode,name,sex,post,phone;unsignedintwage;
29、dopNew=newemployee;/數(shù)據(jù)域。 coutcode;coutendlname;coutendlphone;coutendlsex;coutendlpost;coutendlwage;while(cin.fail()cout請輸入正確的工資數(shù)據(jù)。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數(shù)席添加成功! 是否繼續(xù)添加?(Y/N)
30、again;while(again=Y|again=y);returnHead;boolemployee:Search(employee*Head)/ 查詢同時(shí)滿足“姓名”和“科室”的職工信息。employee*ptr;stringname,post;ptr=Head-Next;coutpost;coutendlname;coutendltt 查詢結(jié)果m_Name=name)&(ptr-m_Post=post)Display_Node(ptr);/打印滿足條件的節(jié)點(diǎn)。returntrue;ptr=ptr-Next;/查詢下一節(jié)點(diǎn)。coutn 無此職工的信息。Next;coutcode;
31、coutendltt 查詢結(jié)果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)/在標(biāo)準(zhǔn)輸出設(shè)備上輸出。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)/修改職工信息/修改單一個節(jié)點(diǎn)。employee*
33、ptr;ptr=Search_Unique_Front(Head);stringcode,name,sex,post,phone;unsignedintwage;if(ptr)couttt 你現(xiàn)在可以修改此職工的信息了endl;/數(shù)據(jù)域。coutcode;coutendlname;coutendlphone;coutendlsex;coutendlpost;coutendlwage;while(cin.fail()cout請輸入正確的工資數(shù)據(jù)。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)/根據(jù)工號刪除職工信息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();/清除文件結(jié)束狀態(tài)。while(pNode)ofilesetw(10)leftm_Codesetw(10)leftm_Namesetw(10)leftm_phonesetw(10)leftm_Sexsetw(10)leftm_Postsetw(10)leftm_WageNext;
36、cout數(shù)據(jù)文件保存成功!Next=NULL)|(Head-Next-Next=NULL)/此步條件判斷非常有價(jià)值。cout數(shù)據(jù)節(jié)點(diǎn)數(shù)少于 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;/歸位,準(zhǔn)備下一次排序。cout從高到低,排序成功!Create(st);fstreamiofile;/定義一個具有輸入.輸出的文件流對象 iofile。iofile.open(d:iofile.txt,ios_base:in|ios_base:out|ios_base:app);/文件以三種方式打開。if(!iofile)cout打開文件失?。ndl;return-1;intmenu;while(1)*couttt*=*endl;couttt*endl;couttt*1.注冊職工 2.修改信息 3.刪除信息 4.信息查詢*endl;couttt*5.保存文件 6.工資排行 7.信息顯示 0.退出系統(tǒng)*endl;couttt*menu;while(cin.fail()cout
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 深入理解教育領(lǐng)域中的大數(shù)據(jù)庫解析
- 從心理角度理解學(xué)生學(xué)習(xí)行為的驅(qū)動力
- 教育心理學(xué)與在線課程學(xué)習(xí)成效的關(guān)系
- 小學(xué)班班通培訓(xùn)課件
- 智慧城市背景下綠色智能辦公樓的發(fā)展
- 教育政策在高校文化傳承中的作用
- 從新政策看未來學(xué)校教育模式的創(chuàng)新
- 大數(shù)據(jù)在學(xué)生個性化教學(xué)計(jì)劃制定中的作用
- 抖音商戶數(shù)據(jù)分析師直播數(shù)據(jù)看板制度
- 抖音商戶直播時(shí)段選擇依據(jù)制度
- 2025年四川省高考生物試卷真題(含答案解析)
- 2024年遼寧省高校畢業(yè)生“三支一扶”計(jì)劃考試真題
- 北京市大興區(qū)2025年初中學(xué)業(yè)水平考試地理真題(含答案)
- 第三代社??ㄅ嘤?xùn)
- 辦公室應(yīng)聘題庫及答案
- 2025年河北中考地理真題含答案
- 鐵礦尾礦清運(yùn)方案(3篇)
- 國開機(jī)考答案 管理學(xué)基礎(chǔ)2025-06-27
- 2025年浙江省中考數(shù)學(xué)試卷真題(含官方標(biāo)準(zhǔn)答案)
- 國家開放大學(xué)《思想道德與法治》社會實(shí)踐報(bào)告范文一
- 【9語安徽中考卷】2025年安徽省中考招生考試真題語文試卷(真題+答案)
評論
0/150
提交評論