C課程設計報告10_第1頁
C課程設計報告10_第2頁
C課程設計報告10_第3頁
C課程設計報告10_第4頁
C課程設計報告10_第5頁
免費預覽已結束,剩余43頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

課程設計報告課程設計題目:面向對象程序設計學生姓名:吳泓專 業(yè):軟件工程班 級:1621801指導教師:張軍2017年6月16日課程設計目的:綜合運用所學過的知識進行實際程序設計課程設計內容:Parti1:類的組合:定義point類,數據成員包括x,y,成員函數包括構造函數,拷貝構造函數和析構函數,以及setx,getx,sety,gety四個屬性函數。定義line類,端點由兩個point類的對象組成,包括構造函數,析構函數以及計算線段長度的函數getlength。在main函數中,定義line的對象,并輸出其長度c#include<iostream>#include<cmath>usingnamespacestd;classpoint(private:doublex,y;public:point(){}point(doublex,doubley):x(x),y(y){}voidsetx(doublexx){x=xx;}doublegetx(){returnx;}voidsety(doubleyy){y=yy;}doublegety(){returny;}point(point&p){x=p.x;y=p.y;}?point(){}};classline{private:pointa,b;public:line(pointaa,pointbb):a(aa),b(bb)()doublegetlength()(doublelength;length=sqrt(pow((a.getx()-b.getx()),2)+pow((a.gety()-b.gety()),2));returnlength;)~line(){});voidmain(){pointp1(2,3);pointp2(5,6);linel1(p1,p2);cout<<"Thelengthofthelineis"<<l1.getlength()<<endl;}Parti1運行結果與分析:運行結果:輸入x1,y1,x2,y2:2356p1:(2,3)p2:(5,6)'D泡201620180129\課程謾計上相日0<:七,1一類的蛆合\1101?1名\七03一ezeIlhe_lengthofthe_line_is4.24264PressanykeytocontinuePart11設計過程、思路與分析:.定義Point類,設置其成員函數(構造函數,拷貝構造函數和析構函數)以及setx,getx,sety,gety四個屬性函數;.定義line類,設置其成員函數和getlength()函數。Getlength()函數可以輸入和輸出兩點的坐標和兩點之間的距離;.在主函數中定義類line對象myline。調用getlength()函數實現目的。Part1 2:對象數組和函數:定義student類,數據成員包括姓名name和成績score,成員函數包括構造函數,拷貝構造函數和析構函數。定義函數 voidhighestscore(students[]), 輸出分數最高的學生姓名和分數。在main函數中定義students[N],調用highestscore函數,輸出分數最高的學生姓名和分數。#include<iostream>#include<string>constintN=3; //定義要輸入的學生數

usingnamespacestd;student類//student類//定義無參構造函數//定義有參構造函數//拷貝構造函數(private:stringname;doublescore;public:student(){}student(stringn,doubles):name(n),score(s){}student(student&s){name=;score=s.score;}voidhighestscore(students[]){intk=0;doublemax;max=s[0].score;for(inti=1;i<N;i++){if(max<s[i].score){max=s[i].score;k=i;}}cout<<"Thestudent'sname:"<<s[k].name<<endl;cout<<"Highestscoreis:"<<s[k].score<<endl;}~student(){}friendostream&operator<<(ostream&os,students){os<<<<""<<s.score<<endl;returnos;}friendistream&operator>>(istream&is,student&s){is>>>>s.score;returnis;}};voidmain(){students[N];for(inti=0;i<N;i++){cin>>s[i];}s[N].highestscore(s);}Parti2運行結果與分析:運行結果:

_|n|xc,■。:,20162。180129t課程謾計一對要數組和函數exe_|n|xzhangsan89lisi97uanguu92Iliestudent1sname:lisiHighestscoreis:97Pressanykeytocontinue運行結果分析:輸入的3位同學,成績最高的是lisi97,輸出結果正確。Parti2設計過程、思路與分析:定義student類,設置其成員函數(構造函數,拷貝構造函數和析構函數)和獲取名字的函數getname()、獲取成績的函數getscore();定義highestscore(students[])函數。該函數中用k記錄成績最高的數組。并且輸出其姓名和成績的信息;主函數中定義對象數組s[N],用for循環(huán)輸入3個學生的姓名和學號,再調用higtestscore(students[])函數輸出成績最高的學生的信息。Parti3:靜態(tài)數據成員:設計一個書類,能夠保存書名、定價,所有書的本數和總價。(將書名和定價設計為普通數據成員;將書的本數和總價設計為靜態(tài)數據成員)#include<iostream>#include<string>usingnamespacestd;classBook(private:stringname;doubleprice;staticintnum;staticdoubletotal;public:Book(){}Book(stringname,doubleprice);-Book(){}Book(Book&con_refbook);stringget_name();doubleget_price();staticintget_num();staticdoubleget_total();voidshow();voidput();};Book::Book(stringname,doubleprice):name(name),price(price){num++;total+=price;}intBook::num=0;intBook::get_num()( 一returnnum;)doubleBook::total=0;doubleBook::get_total()(returntotal;)voidBook::show()(cout<<"Bookinformation:"<<endl;cout<<"name:"<<name<<"price:"<<price<<endl;)voidBook::put()(cout<<"Booktotal:"<<num;cout<<"pricetotal:"<<total<<endl;)intmain()(Bookbook1("Helloworld",16.5);Bookbook2("C++prime",20.5);Bookbook3("C++program",30.0);book1.show();book2.show();book3.show();book3.put();system("pause");return0;)Part13運行結果與分析:運行結果:二:「U420162018口1291課程設計\pmjeeiA3.靜春數據成員口口UUm八ezeBookinfomation:name:Helloworldprice:16.5Bookinfomation:name:C++primeprice:20.5Bookinfomation:name:C++programprice:30Booktotal:3pricetotal:67請按任意鍵繼續(xù).…運行結果分析:一共輸入三本書的書名和價格,輸出的Booktotal為3,pricetotal為67,輸出結果正確。Parti 3設計過程、思路與分析:.定義Book類,設置其成員函數(構造函數,拷貝構造函數和析構函數)獲取書名函數get_name()、獲取價格函數get_price(),訪問靜態(tài)數據num的成員函數get_num()、訪問靜態(tài)數據total的成員函數get_total(),顯示書名及價格的函數show()、顯示總價的函數put();.在主函數中定義Book類的三個對象bookl、book2、book3,并且都初始化;.三個對象同時調用show()函數,顯示信息。最后調用put()函數顯示書、的總數和總價格。Part14:動態(tài)內存分配:定義point類,數據成員包括x,y,成員函數包括構造函數,拷貝構造函數和析構函數,以及setx,getx,sety,gety四個屬性函數。在main函數中,用new和delete分配和釋放N個point的數組。(N是const常量,N=10#include<iostream>constintN=10;usingnamespacestd;classPoint{private:floatx,y;public:Point(){}~Point(){}Point(floatx,floaty):x(x),y(y){}Point(Point&con_refpoint){ 一x=con_refpoint.x;y=con_refpoint.y;}voidsetx(floatxx){x=xx;}voidsety(floatyy){y=yy;}floatgetx(){returnx;}floatgety(){returny;}};intmain(){Point*p=newPoint[N];delete[]p;system("pause");return0;}Parti4運行結果與分析:運行結果分析:程序主函數中位Point[N]數組申請了一段內存空間,并定義指針p指向該對象,然后用delete直接作用于指針,刪除new創(chuàng)建的對象,釋放指針p所指向的內存空間。Parti4設計過程、思路與分析:.定義Point類,設置其成員函數(構造函數,拷貝構造函數和析構函數)以及setx()、sety()、getx()、gety()屬性函數;.在主函數中用new來為Point對親數組申請一塊連續(xù)的內存,然后用delete直接作用于指針,刪除new創(chuàng)建的對象,釋放指針p所指向的內存空間。Part15:類的繼承:定義一個point類,包含私有數據成員x,y,成員函數包括無參構造函數,帶參構造函數,set和get屬性函數。定義circle類,從point類公有派生,增加數據成員半徑r,成員函數包括無參構造函數,帶參構造函數,計算面積函數getarea。在main函數中定義一個circle的對象,并計算其面積。#include<iostream>usingnamespacestd;classpoint{public:point(){}point(intx,inty){}voidset_x(intx){this->x=x;}intget_x(){returnx;}voidset_y(inty){this->y=y;}intget_y(){returny;}private:intx;inty;};classcircle:publicpoint(public:circle(){}circle(intr,intx,inty):point(x,y){this->r=r;}intget_r(){returnr;}doublegetarea(){return(3.14*r*r);}private:intr;};intmain(){circlec1(2,3,2);cout<<"r="<<c1.get_r()<<endl;cout<<"thecircle'sarea="<<c1.getarea()<<endl;system("pause");return0;}Part15運行結果與分析:c::''.*C:\FrogrmFiles\licrosoftVisualStudioVlyProjectssVDebngVfes.exer=2thecireleJsapea=12.56請按任意鍵繼續(xù)運行結果分析:在主函數中初始化圓的半徑r=2,輸出結果area=12.56輸出結果正確。Parti5設計過程、思路與分析:.定義Point類,設置其成員函數(構造函數,拷貝構造函數和析構函數)以及setx()sety()getx()gety() 四個屬性函數。.定義circle類,設置其成員函數(構造函數,拷貝構造函數和析構函數)以及獲取半徑r的函數get_r()計算面積并獲取面積的函數getarea()。.在主函數中定義類的對象c1并初始化r=2o再調用getarea()函數輸出面積Part16:虛基類:定義vehicle類,數據成員包括私有的weight,公有的構造函數,析構函數和輸出函數dispaly;從vehicle類公有派生car類,增加數據成員載人數personnum,公有的才造函數,析構函數和輸出display;從vehicle類公有派生truck類,增加數據成員載貨量laod,公有的構造函數,析構函數和輸出函數display;從car類和truck類共同公有派生出pickup類,包括公有的構造函數和輸出函數。在main函數中,定義pickup類對象,并輸出其基本信息。#include<iostream>usingnamespacestd;classvehicle{public:vehicle(){}vehicle(intweight){this->weight=weight;}~vehicle(){cout<<"vehicleconstructoring!"<<endl;}voiddisplay(){cout<<"theweightofvehicleis"<<weight<<endl;}private:intweight;};classcar:virtualpublicvehicle{public:car(){}car(intpersonnum,intweight):vehicle(weight){this->personnum=personnum;}~car(){cout<<"carconstructoring!"<<endl;}virtualvoiddisplay(){cout<<"Thenumberofpersonis"<<personnum<<endl;}private:intpersonnum;};classtruck:virtualpublicvehicle{public:truck(){}truck(intload,intweight):vehicle(weight){this->load=load;

}~truck(){cout<<"truckconstructoring!"<<endl;}virtualvoiddisplay(){cout<<"Theloadofthetruckis:"<<load<<endl;}private:intload;};classpickup:publiccar,publictruck{public:pickup(){}pickup(intweight1,intweight2,intpersonnum,intload,intweight):car(personnum,weight1),truck(load,weight2),vehicle(weight){cout<<"pickupconstructoring!"<<endl;}virtualvoiddisplay(){vehicle::display();vehicle::~vehicle();car::display();car::~car();truck::display();truck::~truck();}};intmain(){pickupp1(10,10,20,30,10);p1.display();system("pause");return0;}Part16運行結果與分析:_|口_|口Xc:*'. :120162018口129\課程設計\.1_°,ect\6.虛基類cle_Project\Debug\test.exepickupconstractoringftheweightofvehicleis10ueJiiclocon@tructOFinff?Thsnumberofpc爐窯on1bSBcarconstrlictoringf!Theloadofthetruckis:30truckconstructoring!請按任意鍵繼續(xù).一運行結果分析:在主函數中定義pickup類的對象p,并對其初始化。在主函數中調用pickup類中的show()函數。輸日結果正確。Parti 6設計過程、思路與分析:.定義vehicle類,私有成員數據weight,設置其成員函數(構造函數,拷貝構造函數和析構函數)以及display()屬性函數。.定義car類繼承自虛基類vehicle,設置其成員函數(構造函數,拷貝構造函數和析構函數)以及display()屬性函數。.定義truck類繼承自虛基類vehicle,設置其成員函數(構造函數,拷貝構造函數和析構函數)以及display()屬性函數。.定義類pickup繼承類car和類truck,設置其成員函數(構造函數,拷貝構造函數和析構函數)以及show()屬性函數,在show()函數中調用類vehicle、類car、類truck中的display()函數。從而輸出其基本信息。.在多重繼承中,若一類聲明為虛基類,則能保證一個派生類間接地多次繼承該類時,派生類中只繼承該基類的一份成員,避免了派生類中訪問公共基類的共有屬性多份拷貝的二義性。Part17:運算符重載,友元函數和this指針:定義一個計數器類counter,具備自增,自減功能(前后綴);輸入輸出衿<<功能。在main函數里測試該類。#include<iostream>#include<cmath>usingnamespacestd;classcounter;istream&operator>>(istream&is,counter&a);ostream&operator<<(ostream&os,counter&a);classcounter // 定義類counter{private:doubleP;public:counter(){} // 無參構造函數counter(doublep):P(p){} // 帶參構造函數counteroperator++(); // 重載前置++counteroperator++(int); // 重載后置++counteroperator--(); // 重載前置--counteroperator--(int); // 重載后置--friendistream&operator>>(istream&is,counter&a); // 重載輸入運算符>>friendostream&operator<<(ostream&os,counter&a); // 重載輸出運算符<<};countercounter::operator++() // 前置++實現{++P;return*this;}countercounter::operator++(int) // 后置++實現{

countera=*this;++(*this);returna;)countercounter::operator--() //(--P;)countercounter::operator--() //(--P;return*this;)countercounter::operator--(int) //(countera=*this;--(*this);returna;)istream&operator>>(istream&is,counter&a)(is>>a.P;returnis;)前置--實現后置--實現// 運算符>>實現ostream&operator<<(ostream&os,counter&a)//運算符<<實現(os<<a.P;ostream&operator<<(ostream&os,counter&a)//運算符<<實現(os<<a.P;returnos;)intmain()(counterc1(5),c2(3);cout<<"c1="<<c1<<endl<<"c2="<<c2<<endl;cout<<"++c1="<<++c1<<endl; //cout<<"c1++="<<c1++<<endl;cout<<"c2--="<<c2--<<endl;cout<<"--c2="<<--c2<<endl;system("pause");return0;)測試自增自減功能Part17運行結果與分析:運行結果分析:定義c1的值為5,c2的值為3;此時++c1的數值為6;c1++輸出時為6,輸出后為7;此時c2一輸出時為3;--c2輸出時為1,輸出后為1;輸出結果正確。Parti 7設計過程、思路與分析:.定義counter類,私有成員數據weight,設置其成員函數(構造函數和析構函數).重載自加自減運算符和<<、>>運算符。.在主函數中實現運算符重載。.友元函數需要聲明。Parti8:虛函數和抽象類:定義一個抽象類shape,包括公有的計算面積 area函數,計算體積volume函數,輸出基本信息函數 printinfo (三個函數均為純虛函數)。從shape公有派生point類,增加私有數據成員x,y坐標,以及構造函數,析構函數。從point公有派生circle類,增加私有數據成員半徑r,以及構造函數,析構函數。從circle 公有派生cylinder類,增加私有數據成員高度h,以及構造函數,析構函數。(在定義三個派生類的過程中,自己考慮需要重定義哪個虛函數)。在main函數中,定義shape類的指針,指向派生類的對象,輸出三類對象的基本信息,面積,體積;(將shape指針改為引用再嘗試)。#include<iostream>#include<cmath>#defineP3.1415usingnamespacestd;classshape(public:virtualdoublearea()=0;virtualdoublevolume()=0;virtualvoidprintinfo()=0;);classpoint:publicshape(public:point(){}point(doublex,doubley){this->x=x;this->y=y;}voidprintinfo(){cout<<"x="<<x<<",y="<<y<<endl;}-point(){}private:doublex;doubley;};classcircle:publicpoint{public:circle(){}circle(doubler,doublex,doubley):point(x,y){this->r=r;}doublearea(){returnP*r*r;}voidprintinfo(){point::printinfo();cout<<"r="<<r<<endl;cout<<"circleareais"<<area()<<endl;}~circle(){}private:doubler;};classcylinder:publiccircle{public:cylinder(){}cylinder(doubleh,doublex,doubley,doubler):circle(x,y,r){this->h=h;}doublevolume(){returnh*circle::area();}voidprintinfo(){circle::printinfo();cout<<"h="<<h<<endl;cout<<"cylindervolumeis"<<volume()<<endl;}~cylinder(){}private:doubleh;};intmain(){cylinderc1(5,2,2,3);shape*s; //指針s=&c1;(*s).printinfo();cout<<"circleareais"<<s->area()<<endl;cout<<"volumeis"<<(*s).volume()<<endl;system("pause");return0;}Part18運行結果與分析:運行結果:s.'D:,20162018口129上課程設計短1_0]ect\8,虛函數和物象類\Eh.pe_pr°ject\Debug\test,exelx=2,y=3卜=2cireleareais12.566h=5cylinderuolumeis62.83cireleareais12.566uoluineis62.83請按任意鍵繼續(xù)….■運行結果分析:在主函數中分別用了shape類的指針和引用,輸出結果正確。Parti 8設計過程、思路與分析:.先定義基類 shape。設置三個純虛函數并且聲明: virtualdoublearea()=0;// 聲明計算面積純虛函數 area();virtualvoidvolume()=0;//聲明計算體積純虛函數volume。;virtualvoidprintinfo()=0;// 聲明輸出基本信息純虛函數printinfo().定義類point共有繼承自類shape。并且在該類中實現三個純虛函數。.定義類circle共有繼承自類point。并且在該類中實現三個純虛函數。7,定義類cylinder共有繼承自類circle。并且在該類中實現三個純虛函數。.在主函數中分別創(chuàng)建類point的對象a,circle的對象b,cylinder的對象c,并初始化;.在主函數中分別定義shape類的指針和引用,調用printinfo()函數。Part19:模板:設計一個堆棧的類模板Stack,在模板中用類型參數T表示棧中存放的數據,用非類型參數 MAXSIZE弋表棧的大小。#include<iostream>usingnamespacestd;template<typenameT,intMAXSIZE>classStack{public:Stack();boolfull();boolempty();voidpush(Telement);Tgettop();voidpop();~Stack(){delete[]ptr;}private:intmaxsize;intpos;T*ptr;};template<typenameT,intMAXSIZE>Stack<T,MAXSIZE>::Stack()(maxsize=MAXSIZE;pos=0;ptr=newT[MAXSIZE];}template<typenameT,intMAXSIZE>boolStack<T,MAXSIZE>::full()(returnpos>=MAXSIZE;}template<typenameT,intMAXSIZE>boolStack<T,MAXSIZE>::empty()(returnpos<0;}template<typenameT,intMAXSIZE>voidStack<T,MAXSIZE>::push(Telement)(if(!full())(pos++;ptr[pos]=element;}elsecout<<"thestackisfull"<<endl;}template<typenameT,intMAXSIZE>TStack<T,MAXSIZE>::gettop()(if(empty())cout<<"thestackisempty"<<endl;elsereturnptr[pos];}template<typenameT,intMAXSIZE>voidStack<T,MAXSIZE>::pop()(if(empty())cout<<"thestackisempty"<<endl;elsepos--;}intmain()(Stack<int,5>s1;s1.push(5);s1.push(10);s1.push(2);s1.push(22);s1.push(7);cout<<s1.gettop()<<endl;system("pause");return0;}Part19運行結果與分析:運行結果:運行結果分析:在棧中放入5個數:5,10,2,22,7.輸出棧頂元素7輸出結果正確。Parti 9設計過程、思路與分析:.先定義類模板template<typenameT,intMAXSIZE>;.在class類中寫入判斷棧是否已滿的函數full(),判斷棧是否已空的函數empty(),寫入數據的函數push(),獲取棧頂的函數gettop()。.在主函數中在棧中放入5個數:5,6,7,8,9輸出棧頂元素9Part110:文件讀寫:定義學生類數組,有N個人(N=5,包括姓名和語數外三名課的成績,通過重載<<和>>運算符實現學生數組的文件讀寫。(姓名用stringname;)#include<iostream>#include<fstream>#include<string>#defineN5usingnamespacestd;classstudent;ostream&operator<<(ostream&os,students);istream&operator>>(istream&is,student&s);classstudent{public:student(){}student(stringname,intc_socre,intm_score,inte_score){this->name=name;this->c_score=c_score;this->m_score=m_score;this->e_score=e_score;} 一 一friendostream&operator<<(ostream&os,students){os<<<<""<<s.c_score<<""<<s.m_score<<""<<s.e_score<<endl;returnos;}friendistream&operator>>(istream&is,student&s){is>>>>s.c_score>>s.m_score>>s.e_score;returnis;)private:stringname;intc_score;intm_score;inte_score;); 一intmain()(inti;students[N];for(i=0;i<N;i++)cin>>s[i];ofstreamofs("d:\\test\\test.txt",ios_base::out);if(!ofs)(cerr<<"fileopenfailed"<<endl;exit(1);)for(i=0;i<N;i++)(ofs.write(reinterpret_cast<char*>(&s[i]),sizeof(student));)ifstreamifs("d:\\test\\test.txt",ios_base::out);if(!ifs)(cerr<<"fileopenfailed"<<endl;exit(1);)for(i=0;i<N;i++)(ifs.read(reinterpret_cast<char*>(&s[i]),sizeof(student));)for(i=0;i<N;i++)cout<<s[i];system("pause");return0;)Part110運行結果與分析:運行結果:c**U:\201620180129,課程設計\pr”ject\10-文件讀寫\£ile_Frojeut'Ue'bugS.tset-exe,Izhangsan879885lisi959684Languu959692zhaoliu879698chenqi979899zhangsan8798 85TOC\o"1-5"\h\zlisi95 96 84Languu959692zhaoliu8796 98chenqi9798 99請按任意鍵繼續(xù).一Parti 10設計過程、思路與分析:1.定義student類,私有數據成員字符數組name[20];2,定義運算符<<,>>重載;3.在住函數中定義student類數組s[N];并以輸出和二進制的方式打開文件;Part21:小型軟件的開發(fā)(共占程序檢查分的 70%,學號的最后一位即選做的題號,最后一位是0則選做第10題,例如16218131%做的題目為第5題,162181310做的題目是第10題)1教職工信息管理基本要求:定義職工(employee)類,其中至少包括姓名、性別、工號、電話、所在系部和職稱。功能要求:、設計菜單實現功能選擇;、輸入功能:輸入職工信息,并保存到文件中;、查詢功能:)能夠根據工號精確查詢職工信息;)能夠根據姓名、科室查詢職工信息)分系部進行職稱統(tǒng)計,計算各職稱的人數、根據職工的職稱排序輸出、根據工號修改職工信息、根據工號刪除職工信息

Part21項目分析:.功能描述:本項目要實現一個簡單信息管理系統(tǒng),該系統(tǒng)具有四大功能: 輸入功能,查詢功能,修改刪除功能,顯示功能:、輸入功能:添加輸入教職工的信息,并在文件中保存下來。、查詢功能:查找已存入的教職工信息。3、修改刪除功能:修改或刪除已存教職工信息。4、顯示功能:顯示存入的教職工信息以及在修改刪除后顯示人員信息。.項目分析:確定項目功能后,依據功能進行類和數據結構的設計。系統(tǒng)中采用單向鏈表結構管理教職工信息。1、教職工信息的輸入、添加項目中使用文件(Esystemsave.dat)保存輸入和添加的教職工信息。2、教職工信息查詢信息查詢功能中提供了按指定條件進行信息查詢功能。用戶可以對產品的指定屬性按某個條件進行精確查詢或范圍查詢。3、教職工職稱排序通過鏈表對教職工信息依次訪問,按職稱大小依次排序顯示輸出4、修改信息功能管理系統(tǒng)中可以隨時通過姓名和電話號碼精確找到人員信息并且修改。5、刪除信息功能管理系統(tǒng)中可以隨時通過姓名和工號精確找到人員信息并且刪除。6、顯示教職工信息包括姓名、性別、工號、電話、所在系部、職稱等信息。8、信息保存功能運行期間可以保存人員信息,將人員信息存入文件(Esystemsave.dat)中c設計思路:定義Technical_Date系部、職稱等數據。而S定義Technical_Date系部、職稱等數據。而S義Add_Technical_Information();Query_Information();Technical_Title_Statistics();Technical_Title_Sort();Modify_Technical_Information()// 添加人員信息函數;// 查詢功能;//查詢功能中的職工按系部統(tǒng)計職工職稱〃根據教職工職稱按排序輸出//根據工號查找到該工號教職工信息并修改職工信息

Delete_Technical_Information();Display_All_Technical_Date();Display_after_all_person_information();示所有職工信息?!ǜ鶕ぬ柌檎业皆摴ぬ柦搪毠ば畔⒉h除職工信息〃根據工號查找到該工號教職工信息并刪除職工信息//6顯示所有教職工信息(息;//在4,5功能執(zhí)行后選擇是否顯項目實現:首先在VC6.0中創(chuàng)建一個C+領目并添加相應的文件。系統(tǒng)功能通過多個函數實現,類的定義及實現由各個頭文件給出:#include<iostream>#include<string>#include<iomanip>#include<fstream>usingnamespacestd;.定義Technical_Date結構體structTechnical_Date //教職工數據{charTechnical_Name[15];charTechnical_Sex;charTechnical_Job_Number[15];charTechnical_Phone_Number[15];charTechnical_Department[15];charTechnical_Title[20];//教職工姓名//教職工性別//教職工工號//教職工電話//教職工所在系部//教職工職稱//教職工姓名//教職工性別//教職工工號//教職工電話//教職工所在系部//教職工職稱structTechnical_Date*next;//儲存下一個節(jié)點的地址.定義Technical類classTechnical{public:Technical(){strcpy_s(file_address,"d:\\Esystemsave.dat"); //初始化儲存職工信息文件的地址 ,該函數將第二個字符串參數復制給第一個字符串參數System_Menu();}voidSystem_Menu(); //系統(tǒng)主界面菜單voidAdd_Technical_Information(); //添加職工信息并保存到文件voidQuery_Information(); //查詢功能voidTechnical_Title_Statistics(structTechnical_Date*chain_head,intchoice_judge);//查詢功能中的職工按系部統(tǒng)計職工職稱voidTechnical_Title_Sort(); //根據教職工職稱按排序輸出voidModify_Technical_Information(); //根據工號查找到該工號教職工信息并修改職工信息voidDelete_Technical_Information(); //根據工號查找到該工號教職工信息并刪除職工信息voidDisplay_All_Technical_Date(); //顯示所有教職工信息(//讀文件并構建職工數據結構體鏈表返回該鏈表頭指針 ,若存儲職工的文件里無職工數據則返回 NULL(完)structTechnical_Date*read_file_date();//以ios::out 方式(第二個參數為1)或ios::app方式(第二個參數為2)打開二進制文件并將職工數據結構體鏈表數據寫入文件(完)voidwrite_file_date(structTechnical_Date*write_chain_head,intopen_mode);voidDisplay_Link_list_Date(structTechnical_Date*display_chain_head);// 顯示鏈表中數據voidDelete_Link_List(structTechnical_Date*delete_chain_head);// 釋放單向鏈表所占空間voidnew_date_access(structTechnical_Date*chain_head)// 將鏈表節(jié)點中的date_access全恢復為未被訪問狀態(tài)falseprivate:structTechnical_Date*head; //職工數據結構體鏈表頭指針charfile_address[25]; //儲存文件的地址};.定義主菜單voidTechnical二System_Menu(){charMenu_Choice[10]; //輸入的菜單選項選擇ofstreamcreate_save_file(file_address,ios::app|ios_base::binary);//創(chuàng)建存儲職工信息數據的二進制文件create_save_file.close();.添加人員信息函數(使用鏈表)voidTechnical::Add_Technical_Information(){//用于建立鏈表structTechnical_Date*p1=NULLstructTechnical_Date*p2=NULLintn=0;// intemployee_count=0; //記入添加的職工個數charyes_no[10];//決定是否繼續(xù)輸入職工數據信息cout<<"請輸入職工信息"<<endl;// head=NULL//將NULL賦值給鏈表頭指針p1=(structTechnical_Date*)malloc(sizeof(structTechnical_Date));〃給p1開辟一塊Employee_Date結構體大小的空間do(//判斷是否要繼續(xù)輸入職工信息boolchoice_no=false;//若為真則跳出循環(huán)do(cout<<"是否輸入(y/n):";cin>>yes_no;if(!strcmp(yes_no,"y"))break;elseif(!strcmp(yes_no,"n"))(choice_no=true;break;}elsecout<<"無效輸入請重新輸入! "<<endl;}while(true);if(choice_no)break;〃構建鏈表employee_count=employee_count+1;n=n+1;if(n==1)head=p1;else(p1=(structTechnical_Date*)malloc(sizeof(structTechnical_Date));p2->next=p1;}p2=p1;cout<<" " <<endl;cout<<"教職工"<<employee_count<<endl;cout<<"姓名:";cin>>p1->Technical_Name;cout<<"性別:";cin>>p1->Technical_Sex;cout<<"工號:";cin>>p1->Technical_Job_Number;docout<<"電話:";cin>>p1->Technical_Phone_Number;boolpanduan=true;

;i++);i++)if(!('0'<=p1->Technical_Phone_Number[i]&&p1->Technical_Phone_Number[i]<= '9')){panduan=false;cout<<"有非法字符請重新輸入!"<<endl;break;}if(panduan)break;}while(true);cout<<"系部:";cin>>p1->Technical_Department;do{charwork_m_name[10];cout<<"職稱(1正高級,2高級,3一級,4二級,5三級):cin>>work_m_name;if(!strcmp(work_m_name,"1")){strcpy_s(p1->Technical_Title,"正高級");break;}elseif(!strcmp(work_m_name,"2")){strcpy_s(p1->Technical_Title,"高級"}elseif(!strcmp(work_m_name,"2")){strcpy_s(p1->Technical_Title,"高級");}elseif(!strcmp(work_m_name,"3")){strcpy_s(p1->Technical_Title,"一級");}elseif(!strcmp(work_m_name,"4")){strcpy_s(p1->Technical_Title, "二級");}elseif(!strcmp(work_m_name,"5")){strcpy_s(p1->Technical_Title,"三級");}elsebreak;break;break;break;cout<<"無效輸入,請重新輸入! "cout<<"無效輸入,請重新輸入! "<<endl;}while(true);cout<<<<

<<endl;}while(true);if(employee_count==0) 〃輸入職工個數為0說明沒有執(zhí)行上面循環(huán)體里的構建鏈表 ,那么釋放掉循環(huán)開始前malloc給p1的內存空間free(p1);else//輸入職工個數不為0說明上面構建起了鏈表{p2->next=NULL//完整建立了一個頭指針為 head的單向鏈表,p1和p2指向該鏈表尾部結構體的首地址write_file_date(head,2);//以追加ios::app二進制方式打開文件,將鏈表數據寫入文件,傳入的為空指針則不執(zhí)行操作Delete_Link_List(head);//釋放鏈表所占空間}5.查詢功能voidperson::Inquiry_function()(head=NULL;head=read_file_date();if(head==NULL)return;用于判斷是否存在對應人員信息, false用于判斷是否存在對應人員信息, false為booljudge=false;//不存在p=head;charinquiry_choice[5];cout <<歡迎使用查詢功能*******************************************************************"<<endl;歡迎使用查詢功能cout<<"charinquiry_person_relation[20];charinquiry_person_relation[20];cout<<"請輸入人員的類別(關系):"<<endl;*****"<<endl;cout

*******************************************************************H<<cout<<"*****<<cout

*******************************************************************H<<cout<<"*****<<endl;1.根據姓名、電話精確查詢人員信息*****"<<endl;cout<<"*****2cout<<"*****2.根據地址進行模糊查詢人員信息cout<<"*****3.根據人員類別查詢人員信息<<<<*****"<<endl;<<<<cout*******************************************************************"<<endl;cout "<<endl;cout<<"請選擇你需要的功能:cin>>inquiry_choice;//1.根據姓名、電話精確查詢人員信息 (查詢功能)if(!strcmp(inquirychoice,"1")) {charinquiry_person_name[20];charinquirypersontelephone[20];cout<<"請輸入人員的姓名和電話:"<<endl;cout<<"姓名:";cin>>inquiry_person_name;cout<<"電話號碼:";cin>>inquiry_person_telephone;do{if (!strcmp(inquiry_person_name, p->name)&&!strcmp(inquiry_person_telephone,p->telephone))一一{cout<<"該人員的信息:"<<endl;cout<<"姓名:"<<p->name<<" 性另U:"<<p->sex<<"電話號碼:"<<p->telephone;cout<<"地址:"<<p->address<<"郵政編碼:"<<p->postalcode<<"郵箱email:"<<p->email;cout<<"QQ:"<<p->QQ<<"類另U(關系):"<<p->relation<<endl;judge=true;)p=p->next;}while(p!=NULL);if(judge==false)cout<<"沒有所輸入姓名和電話的對應人員的信息! "<<endl;}//2,根據地址進行模糊查詢人員信息 (查詢功能)elseif(!strcmp(inquiry_choice,"2")) {charinquiry_person_address[20];cout<<"請輸入人員的地址:"<<endl;cout<<"地址:";cin>>inquiry_person_address;do{if(!strcmp(inquiry_person_address,p->address)){ 一一cout<<"該人員的信息:"<<endl;cout<<"姓名:"<<p->name<<" 性另U:"<<p->sex<<"電話號碼:"<<p->telephone;cout<<"地址:"<<p->address<<"郵政編碼:"<<p->postalcode<<"郵箱email:"<<p->email;cout<<"QQ:"<<p->QQ<<"類另U(關系):"<<p->relation<<endl;judge=true;}p=p->next;}while(p!=NULL);if(judge==false)cout<<"沒有所輸入地址的對應人員的信息! "<<endl;}//3, 根據人員類別查詢人員信息(查詢功能)elseif(!strcmp(inquiry_choice,"3"))cout<<"類別:";cin>>inquiry_person_relation;do(if(!strcmp(inquiry_person_relation,p->relation))(cout<<"該人員的信息:"<<endl;cout<<"姓名:"<<p->name<<"性另U:"<<p->sex<<"電話號碼:"<<p->telephone;cout<<"地址:"<<p->address<<"郵政編碼:"<<p->postalcode<<"郵箱email:"<<p->email;cout<<"QQ:"<<p->QQ<<"類另U(關系):"<<p->relation<<endl;judge=true;)p=p->next;}while(p!=NULL);if(judge==false)cout<<"沒有所輸入類別的對應人員的信息! "<<endl;}elsecout<<"輸入錯誤!請重新輸入:"<<endl;delete_address_listdate(head);system("pause");system("cls");//DOS 命令清屏.根據姓名對人員信息排序輸出voidperson::Name_order_sorting(){head=NULL;head=read_file_date();if(head==NULL)Ireturn;structaddress_list*p=NULL;structaddress_list*pp;pp=(structaddress_list*)malloc(sizeof(structaddress_list));inti;|intflag;if(head->next!=NULL){for(i=0;;i++) |{flag=0;for(p=head;p->next->next!=NULL;p=p->next){if(p->next->name>p->next->next->name){flag=1;pp->next=p->next;p->next=p->next->next;| pp->next->next=p->next->next;p->next->next=pp->next;}}if(flag==0)break;}if(head->name>head->next->name)structaddress_list*p4;p4=(structaddress_list*)malloc(sizeof(structaddress_list));pp=head;head=head->next;p=head;do{if(pp->name<p->name){pp->next=p4->next;p4->next=pp;break;}p4=p;p=p->next;}while(p!=NULL);if(p==NULL){p4->next=pp;pp->next=NULL;}}}display_address_list_date(head);// 輸出姓名排序后的鏈表中的人員數據 一一一delete_address_list_date(head);// 釋放空間}r - --structaddress_list*person::read_file_date() // 讀取鏈表中的數據ifstreamjudge_file("d:\\person.dat",ios::in|ios_base::binary);if(judge_file){intch;ch=judge_file.get();if(judgefile.eof()) // 判斷文件是否到結尾{cout<<"已無人員信息!"<<endl;judge_file.close();returnNULL;}judge_file.close();}一else{cout<<"已無人員信息!(或文件不存在! )"<<endl;returnNULL;}structaddress_list*chain_head=NULL;ifstreamifs("d:\\person.dat",ios::in|ios_base::binary);structaddress_list*p1=NULL;structaddress_list*p2=NULL;structaddress_list*p3=NULL;intn=0;p1=(structaddress_list*)malloc(sizeof(structaddress_list));do{n=n+1;if(n==1)chain_head=p1;else{Lpl=(structaddress_list*)malloc(sizeof(structaddress_list));p2->next=p1;}p3=p2;p2=p1;ifs.read(reinterpret_cast<char*>(p1),sizeof(structaddress_list));}while(!ifs.eof());free(p1);p3->next=NULL;ifs.close();returnchain_head;}r 一.寫入文件//當open_file=1時以ios::out 方式寫入二進制文件,當open_file=2時以ios::app 追加的方式打開二進制文件一 一voidperson::write_file_date(structaddress_list*write_chain_head,intopen_file)structaddress_list*point=NULL;point=write_chain_head;if(point!=NULL)(ofstreamofs;if(open_file==1)ofs.open("d:\\person.dat", ios::out|ios_base::binary);//以輸出和二進制的方式打開文件elseif(open_file==2)ofs.open("d:\\person.dat", ios::app|ios_base::binary);//以追加和二進制的方式打開文件if(!ofs)(cout<<"文件打開失敗,存入數據失敗! "<<endl;return;}do(ofs.write(reinterpret_cast<char *>(point), sizeof(structaddress_list));point=point->next;}while(point!=NULL);ofs.close();}else(if(open_file==1)(ofstreamofs("d:\\person.dat",ios::out|ios_base::binary);ofs.close();.顯示所有成員信息voidperson::Display_all_person_information(){head=NULL;head=read_file_date();if(head!=NULL){display_address_list_date(head);delete_address_list_date(head);}elsereturn;Ivoidperson::Change_person_information()// 能根據姓名、電話修改人員信息{head=NULL;head=read_file_date();if(head==NULL)return;structaddress_list*p=NULL;booljudge=false;// 用于判斷是否存在對應工號職工信息, false為不存在charinput_person_name[20];charinput_person_telephone[20];cout<<"請輸入底想修改信息的人員姓名和電話: ";cout<<"姓名:";cin>>input_person_name;cout<<"電話號碼:";cin>>input_person_telephone;p=head;if(p!=NULL){do{if (!strcmp(input_person_name, p->name)&&!strcmp(input_person_telephone,p->telephone)){cout<<"該人員的原信息:"<<endl;cout<<"姓名:"<<p->name<<" 性另U:"<<p->sex<<"電話號碼:"<<p->telephone;cout<<"地址:"<<p->address<<"郵政編碼:"<<p->postalcode<<"郵箱email:"<<p->email;cout<<"QQ:"<<p->QQ<<"類另U(關系):"<<p->relation<<endl;cout<<"請輸入修改后的信息:"<<endl;cout<<"姓名:";cin>>p->name;cout<<"性另U:";cin>>p->sex;cout<<"電話號碼:";cin>>p->telephone;cout<<"地址:";cin>>p->address;cout<<"郵政編碼:";cin>>p->postalcod

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論