




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
課程設(shè)計報告課程設(shè)計題目:面向?qū)ο蟪绦蛟O(shè)計學(xué)生姓名:吳泓專 業(yè):軟件工程班 級:1621801指導(dǎo)教師:張軍2017年6月16日課程設(shè)計目的:綜合運(yùn)用所學(xué)過的知識進(jìn)行實際程序設(shè)計課程設(shè)計內(nèi)容:Parti1:類的組合:定義point類,數(shù)據(jù)成員包括x,y,成員函數(shù)包括構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù),以及setx,getx,sety,gety四個屬性函數(shù)。定義line類,端點由兩個point類的對象組成,包括構(gòu)造函數(shù),析構(gòu)函數(shù)以及計算線段長度的函數(shù)getlength。在main函數(shù)中,定義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運(yùn)行結(jié)果與分析:運(yùn)行結(jié)果:輸入x1,y1,x2,y2:2356p1:(2,3)p2:(5,6)'D泡201620180129\課程謾計上相日0<:七,1一類的蛆合\1101?1名\七03一ezeIlhe_lengthofthe_line_is4.24264PressanykeytocontinuePart11設(shè)計過程、思路與分析:.定義Point類,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及setx,getx,sety,gety四個屬性函數(shù);.定義line類,設(shè)置其成員函數(shù)和getlength()函數(shù)。Getlength()函數(shù)可以輸入和輸出兩點的坐標(biāo)和兩點之間的距離;.在主函數(shù)中定義類line對象myline。調(diào)用getlength()函數(shù)實現(xiàn)目的。Part1 2:對象數(shù)組和函數(shù):定義student類,數(shù)據(jù)成員包括姓名name和成績score,成員函數(shù)包括構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù)。定義函數(shù) voidhighestscore(students[]), 輸出分?jǐn)?shù)最高的學(xué)生姓名和分?jǐn)?shù)。在main函數(shù)中定義students[N],調(diào)用highestscore函數(shù),輸出分?jǐn)?shù)最高的學(xué)生姓名和分?jǐn)?shù)。#include<iostream>#include<string>constintN=3; //定義要輸入的學(xué)生數(shù)
usingnamespacestd;student類//student類//定義無參構(gòu)造函數(shù)//定義有參構(gòu)造函數(shù)//拷貝構(gòu)造函數(shù)(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運(yùn)行結(jié)果與分析:運(yùn)行結(jié)果:
_|n|xc,■。:,20162。180129t課程謾計一對要數(shù)組和函數(shù)exe_|n|xzhangsan89lisi97uanguu92Iliestudent1sname:lisiHighestscoreis:97Pressanykeytocontinue運(yùn)行結(jié)果分析:輸入的3位同學(xué),成績最高的是lisi97,輸出結(jié)果正確。Parti2設(shè)計過程、思路與分析:定義student類,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))和獲取名字的函數(shù)getname()、獲取成績的函數(shù)getscore();定義highestscore(students[])函數(shù)。該函數(shù)中用k記錄成績最高的數(shù)組。并且輸出其姓名和成績的信息;主函數(shù)中定義對象數(shù)組s[N],用for循環(huán)輸入3個學(xué)生的姓名和學(xué)號,再調(diào)用higtestscore(students[])函數(shù)輸出成績最高的學(xué)生的信息。Parti3:靜態(tài)數(shù)據(jù)成員:設(shè)計一個書類,能夠保存書名、定價,所有書的本數(shù)和總價。(將書名和定價設(shè)計為普通數(shù)據(jù)成員;將書的本數(shù)和總價設(shè)計為靜態(tài)數(shù)據(jù)成員)#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運(yùn)行結(jié)果與分析:運(yùn)行結(jié)果:二:「U420162018口1291課程設(shè)計\pmjeeiA3.靜春數(shù)據(jù)成員口口UUm八ezeBookinfomation:name:Helloworldprice:16.5Bookinfomation:name:C++primeprice:20.5Bookinfomation:name:C++programprice:30Booktotal:3pricetotal:67請按任意鍵繼續(xù).…運(yùn)行結(jié)果分析:一共輸入三本書的書名和價格,輸出的Booktotal為3,pricetotal為67,輸出結(jié)果正確。Parti 3設(shè)計過程、思路與分析:.定義Book類,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))獲取書名函數(shù)get_name()、獲取價格函數(shù)get_price(),訪問靜態(tài)數(shù)據(jù)num的成員函數(shù)get_num()、訪問靜態(tài)數(shù)據(jù)total的成員函數(shù)get_total(),顯示書名及價格的函數(shù)show()、顯示總價的函數(shù)put();.在主函數(shù)中定義Book類的三個對象bookl、book2、book3,并且都初始化;.三個對象同時調(diào)用show()函數(shù),顯示信息。最后調(diào)用put()函數(shù)顯示書、的總數(shù)和總價格。Part14:動態(tài)內(nèi)存分配:定義point類,數(shù)據(jù)成員包括x,y,成員函數(shù)包括構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù),以及setx,getx,sety,gety四個屬性函數(shù)。在main函數(shù)中,用new和delete分配和釋放N個point的數(shù)組。(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運(yùn)行結(jié)果與分析:運(yùn)行結(jié)果分析:程序主函數(shù)中位Point[N]數(shù)組申請了一段內(nèi)存空間,并定義指針p指向該對象,然后用delete直接作用于指針,刪除new創(chuàng)建的對象,釋放指針p所指向的內(nèi)存空間。Parti4設(shè)計過程、思路與分析:.定義Point類,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及setx()、sety()、getx()、gety()屬性函數(shù);.在主函數(shù)中用new來為Point對親數(shù)組申請一塊連續(xù)的內(nèi)存,然后用delete直接作用于指針,刪除new創(chuàng)建的對象,釋放指針p所指向的內(nèi)存空間。Part15:類的繼承:定義一個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ù),計算面積函數(shù)getarea。在main函數(shù)中定義一個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運(yùn)行結(jié)果與分析:c::''.*C:\FrogrmFiles\licrosoftVisualStudioVlyProjectssVDebngVfes.exer=2thecireleJsapea=12.56請按任意鍵繼續(xù)運(yùn)行結(jié)果分析:在主函數(shù)中初始化圓的半徑r=2,輸出結(jié)果area=12.56輸出結(jié)果正確。Parti5設(shè)計過程、思路與分析:.定義Point類,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及setx()sety()getx()gety() 四個屬性函數(shù)。.定義circle類,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及獲取半徑r的函數(shù)get_r()計算面積并獲取面積的函數(shù)getarea()。.在主函數(shù)中定義類的對象c1并初始化r=2o再調(diào)用getarea()函數(shù)輸出面積Part16:虛基類:定義vehicle類,數(shù)據(jù)成員包括私有的weight,公有的構(gòu)造函數(shù),析構(gòu)函數(shù)和輸出函數(shù)dispaly;從vehicle類公有派生car類,增加數(shù)據(jù)成員載人數(shù)personnum,公有的才造函數(shù),析構(gòu)函數(shù)和輸出display;從vehicle類公有派生truck類,增加數(shù)據(jù)成員載貨量laod,公有的構(gòu)造函數(shù),析構(gòu)函數(shù)和輸出函數(shù)display;從car類和truck類共同公有派生出pickup類,包括公有的構(gòu)造函數(shù)和輸出函數(shù)。在main函數(shù)中,定義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運(yùn)行結(jié)果與分析:_|口_|口Xc:*'. :120162018口129\課程設(shè)計\.1_°,ect\6.虛基類cle_Project\Debug\test.exepickupconstractoringftheweightofvehicleis10ueJiiclocon@tructOFinff?Thsnumberofpc爐窯on1bSBcarconstrlictoringf!Theloadofthetruckis:30truckconstructoring!請按任意鍵繼續(xù).一運(yùn)行結(jié)果分析:在主函數(shù)中定義pickup類的對象p,并對其初始化。在主函數(shù)中調(diào)用pickup類中的show()函數(shù)。輸日結(jié)果正確。Parti 6設(shè)計過程、思路與分析:.定義vehicle類,私有成員數(shù)據(jù)weight,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及display()屬性函數(shù)。.定義car類繼承自虛基類vehicle,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及display()屬性函數(shù)。.定義truck類繼承自虛基類vehicle,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及display()屬性函數(shù)。.定義類pickup繼承類car和類truck,設(shè)置其成員函數(shù)(構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù))以及show()屬性函數(shù),在show()函數(shù)中調(diào)用類vehicle、類car、類truck中的display()函數(shù)。從而輸出其基本信息。.在多重繼承中,若一類聲明為虛基類,則能保證一個派生類間接地多次繼承該類時,派生類中只繼承該基類的一份成員,避免了派生類中訪問公共基類的共有屬性多份拷貝的二義性。Part17:運(yùn)算符重載,友元函數(shù)和this指針:定義一個計數(shù)器類counter,具備自增,自減功能(前后綴);輸入輸出衿<<功能。在main函數(shù)里測試該類。#include<iostream>#include<cmath>usingnamespacestd;classcounter;istream&operator>>(istream&is,counter&a);ostream&operator<<(ostream&os,counter&a);classcounter // 定義類counter{private:doubleP;public:counter(){} // 無參構(gòu)造函數(shù)counter(doublep):P(p){} // 帶參構(gòu)造函數(shù)counteroperator++(); // 重載前置++counteroperator++(int); // 重載后置++counteroperator--(); // 重載前置--counteroperator--(int); // 重載后置--friendistream&operator>>(istream&is,counter&a); // 重載輸入運(yùn)算符>>friendostream&operator<<(ostream&os,counter&a); // 重載輸出運(yùn)算符<<};countercounter::operator++() // 前置++實現(xiàn){++P;return*this;}countercounter::operator++(int) // 后置++實現(xiàn){
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;)前置--實現(xiàn)后置--實現(xiàn)// 運(yùn)算符>>實現(xiàn)ostream&operator<<(ostream&os,counter&a)//運(yùn)算符<<實現(xiàn)(os<<a.P;ostream&operator<<(ostream&os,counter&a)//運(yùn)算符<<實現(xiàn)(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運(yùn)行結(jié)果與分析:運(yùn)行結(jié)果分析:定義c1的值為5,c2的值為3;此時++c1的數(shù)值為6;c1++輸出時為6,輸出后為7;此時c2一輸出時為3;--c2輸出時為1,輸出后為1;輸出結(jié)果正確。Parti 7設(shè)計過程、思路與分析:.定義counter類,私有成員數(shù)據(jù)weight,設(shè)置其成員函數(shù)(構(gòu)造函數(shù)和析構(gòu)函數(shù)).重載自加自減運(yùn)算符和<<、>>運(yùn)算符。.在主函數(shù)中實現(xiàn)運(yùn)算符重載。.友元函數(shù)需要聲明。Parti8:虛函數(shù)和抽象類:定義一個抽象類shape,包括公有的計算面積 area函數(shù),計算體積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ù)。(在定義三個派生類的過程中,自己考慮需要重定義哪個虛函數(shù))。在main函數(shù)中,定義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運(yùn)行結(jié)果與分析:運(yùn)行結(jié)果:s.'D:,20162018口129上課程設(shè)計短1_0]ect\8,虛函數(shù)和物象類\Eh.pe_pr°ject\Debug\test,exelx=2,y=3卜=2cireleareais12.566h=5cylinderuolumeis62.83cireleareais12.566uoluineis62.83請按任意鍵繼續(xù)….■運(yùn)行結(jié)果分析:在主函數(shù)中分別用了shape類的指針和引用,輸出結(jié)果正確。Parti 8設(shè)計過程、思路與分析:.先定義基類 shape。設(shè)置三個純虛函數(shù)并且聲明: virtualdoublearea()=0;// 聲明計算面積純虛函數(shù) area();virtualvoidvolume()=0;//聲明計算體積純虛函數(shù)volume。;virtualvoidprintinfo()=0;// 聲明輸出基本信息純虛函數(shù)printinfo().定義類point共有繼承自類shape。并且在該類中實現(xiàn)三個純虛函數(shù)。.定義類circle共有繼承自類point。并且在該類中實現(xiàn)三個純虛函數(shù)。7,定義類cylinder共有繼承自類circle。并且在該類中實現(xiàn)三個純虛函數(shù)。.在主函數(shù)中分別創(chuàng)建類point的對象a,circle的對象b,cylinder的對象c,并初始化;.在主函數(shù)中分別定義shape類的指針和引用,調(diào)用printinfo()函數(shù)。Part19:模板:設(shè)計一個堆棧的類模板Stack,在模板中用類型參數(shù)T表示棧中存放的數(shù)據(jù),用非類型參數(shù) 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運(yùn)行結(jié)果與分析:運(yùn)行結(jié)果:運(yùn)行結(jié)果分析:在棧中放入5個數(shù):5,10,2,22,7.輸出棧頂元素7輸出結(jié)果正確。Parti 9設(shè)計過程、思路與分析:.先定義類模板template<typenameT,intMAXSIZE>;.在class類中寫入判斷棧是否已滿的函數(shù)full(),判斷棧是否已空的函數(shù)empty(),寫入數(shù)據(jù)的函數(shù)push(),獲取棧頂?shù)暮瘮?shù)gettop()。.在主函數(shù)中在棧中放入5個數(shù):5,6,7,8,9輸出棧頂元素9Part110:文件讀寫:定義學(xué)生類數(shù)組,有N個人(N=5,包括姓名和語數(shù)外三名課的成績,通過重載<<和>>運(yùn)算符實現(xiàn)學(xué)生數(shù)組的文件讀寫。(姓名用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運(yùn)行結(jié)果與分析:運(yùn)行結(jié)果:c**U:\201620180129,課程設(shè)計\pr”ject\10-文件讀寫\£ile_Frojeut'Ue'bugS.tset-exe,Izhangsan879885lisi959684Languu959692zhaoliu879698chenqi979899zhangsan8798 85TOC\o"1-5"\h\zlisi95 96 84Languu959692zhaoliu8796 98chenqi9798 99請按任意鍵繼續(xù).一Parti 10設(shè)計過程、思路與分析:1.定義student類,私有數(shù)據(jù)成員字符數(shù)組name[20];2,定義運(yùn)算符<<,>>重載;3.在住函數(shù)中定義student類數(shù)組s[N];并以輸出和二進(jìn)制的方式打開文件;Part21:小型軟件的開發(fā)(共占程序檢查分的 70%,學(xué)號的最后一位即選做的題號,最后一位是0則選做第10題,例如16218131%做的題目為第5題,162181310做的題目是第10題)1教職工信息管理基本要求:定義職工(employee)類,其中至少包括姓名、性別、工號、電話、所在系部和職稱。功能要求:、設(shè)計菜單實現(xiàn)功能選擇;、輸入功能:輸入職工信息,并保存到文件中;、查詢功能:)能夠根據(jù)工號精確查詢職工信息;)能夠根據(jù)姓名、科室查詢職工信息)分系部進(jìn)行職稱統(tǒng)計,計算各職稱的人數(shù)、根據(jù)職工的職稱排序輸出、根據(jù)工號修改職工信息、根據(jù)工號刪除職工信息
Part21項目分析:.功能描述:本項目要實現(xiàn)一個簡單信息管理系統(tǒng),該系統(tǒng)具有四大功能: 輸入功能,查詢功能,修改刪除功能,顯示功能:、輸入功能:添加輸入教職工的信息,并在文件中保存下來。、查詢功能:查找已存入的教職工信息。3、修改刪除功能:修改或刪除已存教職工信息。4、顯示功能:顯示存入的教職工信息以及在修改刪除后顯示人員信息。.項目分析:確定項目功能后,依據(jù)功能進(jìn)行類和數(shù)據(jù)結(jié)構(gòu)的設(shè)計。系統(tǒng)中采用單向鏈表結(jié)構(gòu)管理教職工信息。1、教職工信息的輸入、添加項目中使用文件(Esystemsave.dat)保存輸入和添加的教職工信息。2、教職工信息查詢信息查詢功能中提供了按指定條件進(jìn)行信息查詢功能。用戶可以對產(chǎn)品的指定屬性按某個條件進(jìn)行精確查詢或范圍查詢。3、教職工職稱排序通過鏈表對教職工信息依次訪問,按職稱大小依次排序顯示輸出4、修改信息功能管理系統(tǒng)中可以隨時通過姓名和電話號碼精確找到人員信息并且修改。5、刪除信息功能管理系統(tǒng)中可以隨時通過姓名和工號精確找到人員信息并且刪除。6、顯示教職工信息包括姓名、性別、工號、電話、所在系部、職稱等信息。8、信息保存功能運(yùn)行期間可以保存人員信息,將人員信息存入文件(Esystemsave.dat)中c設(shè)計思路:定義Technical_Date系部、職稱等數(shù)據(jù)。而S定義Technical_Date系部、職稱等數(shù)據(jù)。而S義Add_Technical_Information();Query_Information();Technical_Title_Statistics();Technical_Title_Sort();Modify_Technical_Information()// 添加人員信息函數(shù);// 查詢功能;//查詢功能中的職工按系部統(tǒng)計職工職稱〃根據(jù)教職工職稱按排序輸出//根據(jù)工號查找到該工號教職工信息并修改職工信息
Delete_Technical_Information();Display_All_Technical_Date();Display_after_all_person_information();示所有職工信息?!ǜ鶕?jù)工號查找到該工號教職工信息并刪除職工信息〃根據(jù)工號查找到該工號教職工信息并刪除職工信息//6顯示所有教職工信息(息;//在4,5功能執(zhí)行后選擇是否顯項目實現(xiàn):首先在VC6.0中創(chuàng)建一個C+領(lǐng)目并添加相應(yīng)的文件。系統(tǒng)功能通過多個函數(shù)實現(xiàn),類的定義及實現(xiàn)由各個頭文件給出:#include<iostream>#include<string>#include<iomanip>#include<fstream>usingnamespacestd;.定義Technical_Date結(jié)構(gòu)體structTechnical_Date //教職工數(shù)據(jù){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"); //初始化儲存職工信息文件的地址 ,該函數(shù)將第二個字符串參數(shù)復(fù)制給第一個字符串參數(shù)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(); //根據(jù)教職工職稱按排序輸出voidModify_Technical_Information(); //根據(jù)工號查找到該工號教職工信息并修改職工信息voidDelete_Technical_Information(); //根據(jù)工號查找到該工號教職工信息并刪除職工信息voidDisplay_All_Technical_Date(); //顯示所有教職工信息(//讀文件并構(gòu)建職工數(shù)據(jù)結(jié)構(gòu)體鏈表返回該鏈表頭指針 ,若存儲職工的文件里無職工數(shù)據(jù)則返回 NULL(完)structTechnical_Date*read_file_date();//以ios::out 方式(第二個參數(shù)為1)或ios::app方式(第二個參數(shù)為2)打開二進(jìn)制文件并將職工數(shù)據(jù)結(jié)構(gòu)體鏈表數(shù)據(jù)寫入文件(完)voidwrite_file_date(structTechnical_Date*write_chain_head,intopen_mode);voidDisplay_Link_list_Date(structTechnical_Date*display_chain_head);// 顯示鏈表中數(shù)據(jù)voidDelete_Link_List(structTechnical_Date*delete_chain_head);// 釋放單向鏈表所占空間voidnew_date_access(structTechnical_Date*chain_head)// 將鏈表節(jié)點中的date_access全恢復(fù)為未被訪問狀態(tài)falseprivate:structTechnical_Date*head; //職工數(shù)據(jù)結(jié)構(gòu)體鏈表頭指針charfile_address[25]; //儲存文件的地址};.定義主菜單voidTechnical二System_Menu(){charMenu_Choice[10]; //輸入的菜單選項選擇ofstreamcreate_save_file(file_address,ios::app|ios_base::binary);//創(chuàng)建存儲職工信息數(shù)據(jù)的二進(jìn)制文件create_save_file.close();.添加人員信息函數(shù)(使用鏈表)voidTechnical::Add_Technical_Information(){//用于建立鏈表structTechnical_Date*p1=NULLstructTechnical_Date*p2=NULLintn=0;// intemployee_count=0; //記入添加的職工個數(shù)charyes_no[10];//決定是否繼續(xù)輸入職工數(shù)據(jù)信息cout<<"請輸入職工信息"<<endl;// head=NULL//將NULL賦值給鏈表頭指針p1=(structTechnical_Date*)malloc(sizeof(structTechnical_Date));〃給p1開辟一塊Employee_Date結(jié)構(gòu)體大小的空間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;〃構(gòu)建鏈表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) 〃輸入職工個數(shù)為0說明沒有執(zhí)行上面循環(huán)體里的構(gòu)建鏈表 ,那么釋放掉循環(huán)開始前malloc給p1的內(nèi)存空間free(p1);else//輸入職工個數(shù)不為0說明上面構(gòu)建起了鏈表{p2->next=NULL//完整建立了一個頭指針為 head的單向鏈表,p1和p2指向該鏈表尾部結(jié)構(gòu)體的首地址write_file_date(head,2);//以追加ios::app二進(jìn)制方式打開文件,將鏈表數(shù)據(jù)寫入文件,傳入的為空指針則不執(zhí)行操作Delete_Link_List(head);//釋放鏈表所占空間}5.查詢功能voidperson::Inquiry_function()(head=NULL;head=read_file_date();if(head==NULL)return;用于判斷是否存在對應(yīng)人員信息, false用于判斷是否存在對應(yīng)人員信息, false為booljudge=false;//不存在p=head;charinquiry_choice[5];cout <<歡迎使用查詢功能*******************************************************************"<<endl;歡迎使用查詢功能cout<<"charinquiry_person_relation[20];charinquiry_person_relation[20];cout<<"請輸入人員的類別(關(guān)系):"<<endl;*****"<<endl;cout
*******************************************************************H<<cout<<"*****<<cout
*******************************************************************H<<cout<<"*****<<endl;1.根據(jù)姓名、電話精確查詢?nèi)藛T信息*****"<<endl;cout<<"*****2cout<<"*****2.根據(jù)地址進(jìn)行模糊查詢?nèi)藛T信息cout<<"*****3.根據(jù)人員類別查詢?nèi)藛T信息<<<<*****"<<endl;<<<<cout*******************************************************************"<<endl;cout "<<endl;cout<<"請選擇你需要的功能:cin>>inquiry_choice;//1.根據(jù)姓名、電話精確查詢?nèi)藛T信息 (查詢功能)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(關(guān)系):"<<p->relation<<endl;judge=true;)p=p->next;}while(p!=NULL);if(judge==false)cout<<"沒有所輸入姓名和電話的對應(yīng)人員的信息! "<<endl;}//2,根據(jù)地址進(jìn)行模糊查詢?nèi)藛T信息 (查詢功能)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(關(guān)系):"<<p->relation<<endl;judge=true;}p=p->next;}while(p!=NULL);if(judge==false)cout<<"沒有所輸入地址的對應(yīng)人員的信息! "<<endl;}//3, 根據(jù)人員類別查詢?nèi)藛T信息(查詢功能)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(關(guān)系):"<<p->relation<<endl;judge=true;)p=p->next;}while(p!=NULL);if(judge==false)cout<<"沒有所輸入類別的對應(yīng)人員的信息! "<<endl;}elsecout<<"輸入錯誤!請重新輸入:"<<endl;delete_address_listdate(head);system("pause");system("cls");//DOS 命令清屏.根據(jù)姓名對人員信息排序輸出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);// 輸出姓名排序后的鏈表中的人員數(shù)據(jù) 一一一delete_address_list_date(head);// 釋放空間}r - --structaddress_list*person::read_file_date() // 讀取鏈表中的數(shù)據(jù)ifstreamjudge_file("d:\\person.dat",ios::in|ios_base::binary);if(judge_file){intch;ch=judge_file.get();if(judgefile.eof()) // 判斷文件是否到結(jié)尾{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 一.寫入文件//當(dāng)open_file=1時以ios::out 方式寫入二進(jìn)制文件,當(dāng)open_file=2時以ios::app 追加的方式打開二進(jìn)制文件一 一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);//以輸出和二進(jìn)制的方式打開文件elseif(open_file==2)ofs.open("d:\\person.dat", ios::app|ios_base::binary);//以追加和二進(jìn)制的方式打開文件if(!ofs)(cout<<"文件打開失敗,存入數(shù)據(jù)失??! "<<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()// 能根據(jù)姓名、電話修改人員信息{head=NULL;head=read_file_date();if(head==NULL)return;structaddress_list*p=NULL;booljudge=false;// 用于判斷是否存在對應(yīng)工號職工信息, 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(關(guān)系):"<<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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 咨詢-評價復(fù)習(xí)測試卷附答案
- 社交媒體營銷在商業(yè)領(lǐng)域的應(yīng)用與案例分析
- 2025年貴州省安全員-A證考試題庫附答案
- 土地買賣合同合同范本
- 藝人策劃合同范本
- 科技賦能下的電網(wǎng)調(diào)度自動化系統(tǒng)升級
- 2025年湖北省宜昌市單招職業(yè)傾向性測試題庫及參考答案
- 婚慶餐飲訂單合同范本
- 2025-2030年中國滑板行業(yè)運(yùn)行現(xiàn)狀及發(fā)展前景分析報告
- 2025-2030年中國汽車裝飾件行業(yè)發(fā)展趨勢及投資戰(zhàn)略研究報告
- 獸藥GSP質(zhì)量管理制度匯編
- USB-3.1-TYPE-C-培訓(xùn)資料公開課獲獎?wù)n件
- 《機(jī)械制圖(多學(xué)時)》中職全套教學(xué)課件
- 2024-2025學(xué)年小學(xué)信息技術(shù)(信息科技)第二冊電子工業(yè)版(2022)教學(xué)設(shè)計合集
- 課堂教學(xué)質(zhì)量評價表
- 人工智能通識-課件全套 黃君羨 01-12 初識人工智能 -AIGC安全與倫理
- 婚姻家庭咨詢師服務(wù)流程手冊
- 2024-2030年中國納米纖維素技術(shù)行業(yè)市場發(fā)展趨勢與前景展望戰(zhàn)略分析報告
- 《C語言程序設(shè)計(第5版)》全套教學(xué)課件
- 腸內(nèi)營養(yǎng)患者的血糖管理
- 兒科新生兒臍炎培訓(xùn)課件
評論
0/150
提交評論