




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、C+程序設(shè)計(jì) 練習(xí)題學(xué)院: 計(jì)算機(jī)學(xué)院 專業(yè)班級(jí): 物聯(lián)網(wǎng)1002 學(xué)號(hào): 姓名: 徐 遠(yuǎn) 志 第八章1.下面是一個(gè)類的測試程序,試設(shè)計(jì)出能是用如下測試程序的類.Int main() Test x;x.initx(30,20);x.printx();return 0;解:#include using namespace std;class Test /類的開始public:void initx(int i,int j);void printx();private:int x,y; void Test:initx(int i,int j)x=i;y=j;void Test:printx()cou
2、tx*y=x*yendl; /類的結(jié)束int main() /測試函數(shù)Test x;x.initx(30,20);x.printx();return 0;得到的測試結(jié)果:4.定義并實(shí)現(xiàn)一個(gè)矩形類Crectangle。該類包含了下列成員函數(shù)。Crectangle(): 累的構(gòu)造函數(shù).根據(jù)需要可以定義多個(gè)構(gòu)造函數(shù)SetTop(),SetLeft(): 設(shè)置矩形的左上角坐標(biāo)SetLength(),SetWidth(): 設(shè)置矩形的長和寬Perimeter(): 求矩形的周長Area(): 求矩形的面積GetWidth(): 返回矩形的寬度Getlength(): 返回矩形的長度IsSquare():
3、 判斷矩形是否為正方形Move(): 將矩形從一個(gè)位置移動(dòng)到另一個(gè)位置Size(): 改變矩形的大小Where(): 返回矩形的左上角的坐標(biāo)PrintRectangle(): 輸出矩形的四個(gè)頂點(diǎn)的坐標(biāo)數(shù)據(jù)成員int top,left;int length,width;解:#include using namespace std;class Crectangle /類的開始int top,left;int length,width;public:Crectangle(int t=0,int l=0,int len=1,int w=1) top=t;left=l; if (len0) length
4、=len; else length=0;if (w0) width=w; else width=0;void SetTop(int t)top=t;void SetLeft(int l)left=l;void SetLength(int len)length=len;void SetWidth(int w)width=w;int Perimeter()return 2*(width+length);int Area()return width*length;int GetWidth()return width;int GetLength()return length;int IsSquare(
5、) if(width=length) cout該矩形是正方形endl;return 1;elsecout該矩形不是正方形endl;return 0;void Move(int x,int y) left+=x; top+=y;void Size(int l,int w) length=l; width=w; void PrintRectangle()cout左上方的點(diǎn)為:(left,top)endl;cout右上方的點(diǎn)為:(left+length,top)endl;cout左下方的點(diǎn)為:(left,top+width)endl;cout右下方的點(diǎn)為:(left+length,top+width
6、)endl; /類的結(jié)束int main()Crectangle a(1,1,5,5);a.PrintRectangle();a.SetTop(2); a.SetLeft(3);a.SetLength(4);a.SetWidth(5); a.IsSquare();cout周長為:a.Perimeter()endl;cout面積為:a.Area()endl;a.PrintRectangle(); cout用getwidth等函數(shù)得到的值為:endl;cout 長為:a.GetLengthendl;cout 寬為:a.GetWidthendl;return 0;得到的測試結(jié)果:6.某次歌手大賽中.
7、有JudgeNum個(gè)評委給選手打分.參加比賽的選手有PlayerNum名.現(xiàn)為比賽積分編寫一個(gè)CompetitionResult類.類的定義如下: (定義略)(1) 寫出所有的成員函數(shù)和實(shí)現(xiàn)代碼。(2) 編寫main()函數(shù)對該類進(jìn)行測試。在函數(shù)體中.定義了一個(gè)competitionResult類的對象數(shù)組rPlauerNum,他的每個(gè)元素記錄了每個(gè)選手的所有信息.個(gè)評委的打分通過鍵盤的輸入.在屏幕上應(yīng)有提示信息進(jìn)行交互式操作.比賽結(jié)果按選手得分從高到底排列輸出。 解:(1)CompetitionResult:CompetitionResult()num=0; strcpy(name,);fo
8、r(int i=0;iJudgeNum;i+) scorei=0.0;average=0;CompetitionResult:CompetitionResult(short n,char*ps)num=n;strcpy(name,ps);for(int i=0;iJudgeNum;i+) scorei=0.0;average=0;float CompetitionResult:MaxScore() int max=score0;for(int i=0;iJudgeNum;i+)if(maxscorei) max=scorei;return max;float CompetitionResult
9、:MinScore() int min=score0; for(int i=0;iscorei) min=scorei;return min;void CompetitionResult:SetAvg() int i;float sum=0.0; for(i=0;iJudgeNum;i+) sum+=scorei; sum=sum-MaxScore()-MinScore(); average=sum/(JudgeNum-2);float CompetitionResult:GetAvg() return average;short CompetitionResult:GetNo(int i)
10、return num;void CompetitionResult:SetNo(int i) num=i;char CompetitionResult:* GetName() return name ;float CompetitionResult:GetScore(int j) return scorej;void CompetitionResult:Setscore(int k,float av) scorek=av; void Sort(CompetitionResult *pr,int n) int i,j ,k; CompetitionResult temp; for(i=0;in-
11、1;i+) k=i; for(j=i+1;j(prk.average) k=j; if(k!=j) temp=pri; pri=prk; prk=temp; (2) int main()CompetitionResult playerPlayerNum=CompetitionResult(1,one),CompetitionResult(2,two),CompetitionResult(3,there),CompetitionResult(4,four),CompetitionResult(5,five),CompetitionResult(6,six),CompetitionResult(7
12、,seven),CompetitionResult(8,eight),CompetitionResult(9,nine),CompetitionResult(10,ten);float a;for(int i=0;iPlayerNum;i+)cout請?jiān)u委給第i+1個(gè)選手打分:endl;for(int j=0;ja;playeri.Setscore(j,a);playeri.SetAvg();coutendl;Sort(player,PlayerNum);cout 最后的得分情況為: endl;for (int j=0;jPlayerNum;j+)coutj+1 ;coutsetw(6)pla
13、yerj.GetAvg();cout playerj.GetName()endl;return 0;程序運(yùn)行的結(jié)果為:第九章1. 在下面的程序中.派生類Derived 的成員函數(shù)sets()能否訪問基類base中得變量a和b?為什么?如果在基類base中將數(shù)據(jù)成員a和b定義為私有成員.下面的程序能否通過編譯?為什么?#include using namespace std;class Base public:void set(int i,int j) a=i;b=j;void show() couta=a,b=bendl;protected:int a,b;class Derived:publ
14、ic Base public:void sets() s=a*b;void shows() couts=sendl;private:int s;int main() Derived obj;obj.set(2,3);obj.show();obj.sets();obj.shows();return 0;解:可以訪問a和b;因?yàn)閍.b為保護(hù)成員.經(jīng)公有繼承.成為派生類中得保護(hù)成員.所以派生類Derived 的成員函數(shù)sets()能訪問基類base中得變量a和b。如果定義為私有.則不能通過編譯。因?yàn)榕缮愔械某蓡T函數(shù)不能訪問基類中得私有成員2聲明一個(gè)圖形基類Shape.在它的基礎(chǔ)上派生出矩形類Rec
15、tangle和圓形類Circle.它們都有計(jì)算面積的和周長、輸出圖形信息的成員函數(shù).再在Rectangle類的基礎(chǔ)上派生方形類Square。編寫程序和各類的定義和實(shí)現(xiàn).以及類的使用。解: #include using namespace std;class Shape public:double getArea()double getPerimeter() ;class Rectangle:public Shape protected:double height;double width;public:Rectangle() Rectangle(double a,double b)height
16、=a;width=b;double getArea() return height*width;double getPerimeter() return 2*(height+width);class Circle:public Shapepublic:Circle(double x):r(x)double getPerimeter()return 2*3.1416*r;double getArea()return r*r*3.1416;private:double r;class Square:public Rectanglepublic:Square(double x)height=x;wi
17、dth=x;int main()Rectangle a1(2.2,3.2);Circle a2(4.2);Square a3(5.3);coutArea=a1.getArea()endl;coutPerimeter=a1.getPerimeter()endl;coutArea=a2.getArea()endl;coutPerimeter=a2.getPerimeter()endl;coutArea=a3.getArea()endl;coutPerimeter=a3.getPerimeter()endl;return 0;5.某銷售公司有銷售經(jīng)理和銷售員工.月工資的計(jì)算辦法為:銷售激勵(lì)的底薪為4
18、000元.并將銷售額的2/1000做提成.銷售員工無底薪.只提取5/1000為工資。編寫程序:(1) 定義一個(gè)積累Employee.它包含三個(gè)數(shù)據(jù)成員和salary.以及編號(hào)和姓名的構(gòu)造函數(shù)。(2) 有Employee類派生Salesman類。Salesman類包含兩個(gè)新數(shù)據(jù)成員commrate和sales.還包括了用于輸入銷售額并計(jì)算銷售員工工資的成員函數(shù)pay()和用于輸出打印的print()。(3) 由Salesman派生Salemanager,Salesmmanage類包含新數(shù)據(jù)成員monthlypay.以及用于輸入銷售額并計(jì)算銷售經(jīng)理工資的成員函數(shù)pay()和用
19、于輸出的print()。(4) 編寫main()函數(shù)中測試所設(shè)計(jì)的類結(jié)構(gòu)。解:(1)class Employeepublic:Employee(long num,char *n)number=num;strcpy(name,n);protected:long number;char name20;double salary;(2)class Salesman:public Employeepublic:Salesman(long num,char *n,float c=0.005):Employee(num,n) commrate=c;void pay()coutnamesales;salar
20、y=commrate*sales;void print()coutnamesalaryendl;protected:float commrate;double sales;(3) class Salesmanager:public Salesmanpublic:Salesmanager(long num,char *n,float c=0.002,double m=4000):Salesman(num,n,c) monthlypay=m;void pay()coutnamesales;salary=monthlypay+commrate*sales;void print()coutnamesa
21、laryendl;private:double monthlypay;(4) int main()Salesman s1(1234,);Salesmanager s2(1357,);s1.pay();s1.print();s2.pay();s2.print();return 0;第十章1.定義一個(gè)輔助類Complex,重載運(yùn)算符+、-、*、/使之能用于復(fù)數(shù)的加減乘除運(yùn)算。運(yùn)算符重載函數(shù)為Complex類的成員函數(shù)。編寫程序.分別求出兩個(gè)復(fù)數(shù)之和、差、積、商。解:#include using namespace std;class Complexpublic:Complex(double r=0
22、.0,double i=0.0) real=r;imag=i;Complex operator + (Complex);friend Complex operator - (Complex,Complex);Complex operator * (Complex);friend Complex operator / (Complex,Complex);void display();private:double real,imag;Complex Complex:operator + (Complex c)return Complex(real+c.real,imag+c.imag);Compl
23、ex operator - (Complex c1,Complex c2)return Complex(c1.real-c2.real,c1.imag-c2.imag);Complex Complex:operator * (Complex c)return Complex(real*c.real-imag+c.imag,imag*c.imag+real*c.real);Complex operator / (Complex c1,Complex c2)double r,i;r=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag
24、*c2.imag);i=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);return(r,i);void Complex:display()cout(real,imag)endl;int main()Complex c1(7.1,8.4),c2(1.4,5),c3;coutc1=; c1.display();coutc2=; c2.display();c3=c1+c2;coutc1+c2=;c3.display();c3=c1-c2;coutc1-c2=;c3.display();c3=c1*c2;cout
25、c1*c2=;c3.display();c3=c1/c2;coutc1/c2=;c3.display();return 0;2.定義一個(gè)日期類Date.包括年.月.日等私有數(shù)據(jù)成員。要求為所定義的Date類設(shè)計(jì)如下重載運(yùn)算符函數(shù):Date operator+(int days); 返回一日起加天數(shù)days后得到的日期Date operator-(int days); 返回一日起減天數(shù)days后得到的日期Int operator-(Date &b); 返回兩日期相差的天數(shù)解:#include using namespace std;int day_tab12=31,28,31,30,31,30,
26、31,31,30,31,30,31;class Datepublic:Date() Date(int y,int m,int d) year=y;mon=m;day=d;void setday(int d) day=d;void setmon(int m) mon=m;void setyear(int y) year=y;int getday() return day;int getmon() return mon;int getyear() return year;Date operator + (int days)static Date date;int number=dton(*this
27、);number-=days;date=ntod(number);return date;Date operator - (int days) static Date date;int number=dton(*this);number-=days;date=ntod(number);return date; int operator - (Date &b)int days=dton(*this)-dton(b)-1;return days;void disp()coutyear.mon.dayendl;private:int year,mon,day;int leap(int);int dt
28、on(Date &);Date ntod(int);int Date:leap(int year)if(year%4=0&year&100!=0|year%400=0)return 1;else return 0;int Date:dton(Date &d)int y,m,days=0;for(y=1;y=d.year;y+)if(leap(y)days+=366;elsedays+=365;day_tab1=leap(d.year)?29:28;for(m=0;md.mon-1;m+)days+=day_tabm;days+=d.day;return days;Date Date:ntod(
29、int n)int y=1,m=1,d,rest=n,days_year;while(1)days_year=leap(y)?366:365;if(restday_tabm-1)rest-=day_tabm-1;elsebreak;m+;d=rest;return Date(y,m,d);int main()Date now(2011,12,22),Nationday(1949,10,1);coutnow:;now.disp();coutNationday:;Nationday.disp();cout:(now-Nationday)endl;Date d1=now+365,d2=now-365
30、;coutnow+365:;d1.disp();coutnow-365:;d2.disp();return 0;3.編寫一個(gè)程序尖酸正方體圓柱體的表面積和體積。要求抽象出一個(gè)公共的基類Body.吧他當(dāng)做抽象類.在該類中定義表面積和體積的純虛函數(shù)。抽象類中定義一個(gè)數(shù)據(jù)成員data.他可以作為求的半徑.正方形的邊長.或者圓柱體的地面半徑。在三個(gè)類中都有計(jì)算白面積和體積的函數(shù)的具體實(shí)現(xiàn)。解:#include using namespace std;class Bodyprotected:double data;public:Body(double data)Body:data=data;virtua
31、l double s()=0;virtual double v()=0;class Cube:public Bodypublic:Cube(double data):Body(data);double s()return data*data*6;double v()return data*data*data;class Sphere:public Bodypublic:Sphere(double r):Body(r);double s()return data*data*3.1416*4;double v() return 4*data*data*data/3;class Cylinder:p
32、ublic Bodydouble height;public:Cylinder(double r,double height): Body(r)Cylinder:height=height;double s()return (2*3.1416*data*data+2*data*3.1416*height);double v()return data*data*3.1416*height;int main()Body *p;Cube obj1(5);Sphere obj2(5);Cylinder obj3(5,5);p=&obj1;cout正方體的表面積:s()endl;cout正方體的體積:v
33、()endl;p=&obj2;cout球的表面積:s()endl;cout球的體積:v()endl;p=&obj3;cout球的表面積:s()endl;cout球的體積:v()endl;return 0;第十一章1. 已知Void Sort(int a,int size);Void Sort(double a,int size);是一個(gè)函數(shù)模板的兩個(gè)例子.起功能是將數(shù)組a中得前size個(gè)元素按從小到大的順序依次排列。是設(shè)計(jì)函數(shù)模板。解:#include using namespace std;templatevoid Sort(T set,int n)int i,j;T temp;for (i
34、=1;ii;j-)if(setj-1setj)temp=setj-1;setj-1=setj;setj=temp;int main()int a=4,5,8,9,3;double b=3,5,6.7,2,5.2,9.2,10.3;Sort(a,6);Sort(b,6);for(int i=0;i6;i+)coutai ;coutendl;for(i=0;i6;i+)coutbi ;coutendl;return 0;2. 設(shè)有如下聲明class Testpublic: Void SetData1(int val) data1=val;Void SetData2(doule val) data2
35、=val;int GetData1() return data1;double GetData2() return data2;private: int data1;double data2;試將此類聲明為類模板的聲明.使得數(shù)據(jù)成員data1和data2可以使任何類型#include #include using namespace std;template class Testpublic: void SetData1(T1 val) data1=val; void SetData2(T2 val) data2=val; T1 GetData1() return data1; T2 GetD
36、ata2() return data2;private: int data1; double data2;int main()Test t1;t1.SetData1(10);t1.SetData2(5.4); coutt1.GetData1() t1.GetData2()endl;Test t2;t2.SetData1(a);t2.SetData2(China);coutt2.GetData1() t2.GetData2()endl;return 0;3棧是一種重要的數(shù)據(jù)結(jié)構(gòu).它是一種只允許在彪的一端進(jìn)行插入或刪除操作的線性表。表中允許進(jìn)行插入、刪除操作的一端稱為棧頂。表的令一端稱為棧底。下面
37、是一個(gè)整形棧類的定義: Const int MaxSize=100; Class IstackPublic: Istack(); void Push(int &n); void pop();int GetTop();bool Empty();int size();boid ClearStack();Istack();Private:Int elemMaxSize;Int top;編寫一個(gè)棧的類模板.以便為任何類型的對象提供棧結(jié)構(gòu)的數(shù)據(jù)操作。解:#include #include using namespace std;const int MaxSize=100;template class S
38、tackpublic:Stack();void Push(const T &a);void Pop();T GetTop();void ClearStack();bool Empty();int Size();Stack()private:T elemMaxSize;int Top;template Stack :Stack()Top=-1;template void Stack:Push(const T &a)if(Top=MaxSize-1)return;Top+;elemTop=a;template void Stack:Pop()if(Top=-1)return;Top-;templa
39、te void Stack:ClearStack()Top=-1;template T Stack:GetTop()if(Top=-1)return 0;return elemTop;template bool Stack:Empty()if(Top=-1)return true;elsereturn false;template int Stack:Size()return Top+1;int mian()Stack s1;Stack s2;s1.Push(123);s1.Push(456);couts1.GetTop()endl;s1.Pop();couts1.Size() s1.Empt
40、y()endl;s2.Push(beijing);s2.Push(wuhan);couts2.Size() s2.Empty()endl;return 0;第十三章1.編寫程序.重載運(yùn)算符.使用戶能直接輸入和輸出復(fù)數(shù)。解:#include class Complexpublic:Complex(double r=0,double i=0)real=r;imag=i;friend ostream &operator(istream &,Complex &);private:double real,imag;ostream &operator(ostream &output ,Complex &o
41、bj)output0)output+;if(obj.imag!=0)outputobj.imag(istream &input ,Complex &obj)coutInput the real and img of the complex:obj.real;inputobj.imag;return input;int main()Complex c1,c2,c3;cinc1c2c3;coutc1=c1endl;coutc2=c2endl; coutc3=c3endl;return ; 3. 編寫一個(gè)程序.統(tǒng)計(jì)文本文件abc.txt中的字符.并給文件所有行加上行號(hào).然后寫到newabc.txt中去解:#include #include using namespace std;int main()fstream file;file.open(abc.txt,ios:in);if(!file)coutabc.txt cant open.endl;abort();char ch;int i=0;while(!file.eof()file.get(ch);i+;coutCharater:iendl;file.close();return 0;6#include #include using namespace std;struct
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 鳳翔學(xué)校九年級(jí)數(shù)學(xué)試卷
- 低分和高分?jǐn)?shù)學(xué)試卷
- 肝病中醫(yī)康復(fù)課件
- 地調(diào)小升初考試數(shù)學(xué)試卷
- 肖琴中醫(yī)課件
- 公廁管護(hù)培訓(xùn)課件內(nèi)容
- 鄧州市大聯(lián)考數(shù)學(xué)試卷
- 肉牛飼養(yǎng)技術(shù)課件
- 課件制作培訓(xùn)過程
- 2025至2030城市應(yīng)急聯(lián)動(dòng)行業(yè)市場深度研究與戰(zhàn)略咨詢分析報(bào)告
- 2024年高中生物學(xué)業(yè)水平合格考及答案
- DB61∕T 1856-2024 國土調(diào)查成本定額
- 出版業(yè)行業(yè)市場特點(diǎn)分析
- 廣東省四校(華附、省實(shí)、廣雅、深中)2023至2024學(xué)年高二下學(xué)期期末聯(lián)考化學(xué)試題附參考答案(解析)
- 離散裝配行業(yè)MES案例
- 1.3探索三角形全等的條件第7課時(shí) 蘇科版八年級(jí)數(shù)學(xué)上冊
- 南昌市產(chǎn)業(yè)投資集團(tuán)有限公司人才招聘筆試真題2023
- GB/T 4706.11-2024家用和類似用途電器的安全第11部分:快熱式熱水器的特殊要求
- 數(shù)字貨幣概論 課件 第5章 穩(wěn)定幣的原理與實(shí)現(xiàn)
- 專題02《物態(tài)變化》壓軸培優(yōu)題型訓(xùn)練【十三大題型】(原卷版)
- 大學(xué)生科研訓(xùn)練與論文寫作全套教學(xué)課件
評論
0/150
提交評論