類的作業(yè)及答案_第1頁
類的作業(yè)及答案_第2頁
類的作業(yè)及答案_第3頁
類的作業(yè)及答案_第4頁
類的作業(yè)及答案_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上/編程:建立一個(gè)分?jǐn)?shù)類。分?jǐn)?shù)類的數(shù)據(jù)成員包括分子和分母,操作包括顯示、輸入、約分、通分、比較、加、減、乘、除、求相反數(shù)。#include<iostream>#include<cmath>using namespace std;class fractionint above; /分子int below; /分母void reduction(); /約分void makeCommond(fraction&); /通分public:fraction(int a=0,int b=1) /構(gòu)造函數(shù)above=a;below=b;fraction

2、add(fraction); /兩分?jǐn)?shù)相加fraction sub(fraction); /本分?jǐn)?shù)減去實(shí)參分?jǐn)?shù)fraction mul(fraction); /兩分?jǐn)?shù)相乘fraction div(fraction); /本分?jǐn)?shù)除以實(shí)參分?jǐn)?shù)fraction reciprocal(); /求倒數(shù)bool equal(fraction); /等于運(yùn)算bool greaterThan(fraction); /大于運(yùn)算bool lessThan(fraction); /小于運(yùn)算void display(); /顯示分?jǐn)?shù)void input(); /輸入分?jǐn)?shù);void fraction:reduction

3、() /約分先求最大公約數(shù)int a,b,temp;if(below<0)above=-above;below=-below;a=abs(above);b=abs(below);while(a%b) /歐幾里德法求最大公約數(shù)temp=a;a=b;b=temp%b;above/=b;below/=b;void fraction:makeCommond(fraction& b)int temp;reduction();b.reduction();above*=b.below;b.above*=below;temp=below*b.below;below=b.below=temp;fr

4、action fraction:add(fraction b)fraction temp;makeCommond(b); /通分temp.above=above+b.above;temp.below=below;temp.reduction(); /約分return temp;fraction fraction:sub(fraction b)fraction temp;makeCommond(b); /通分temp.above=above-b.above;temp.below=below;temp.reduction(); /約分return temp;fraction fraction:mu

5、l(fraction b)fraction temp;temp.above=above*b.above;temp.below=below*b.below;temp.reduction(); /約分return temp;fraction fraction:div(fraction b)fraction temp;if(b.above=0)cout<<"零不能作除數(shù)!"<<endl;exit(1);temp.above=above*b.below;temp.below=below*b.above;temp.reduction(); /約分return

6、temp;fraction fraction:reciprocal()fraction temp;temp.above=below;temp.below=above;temp.reduction(); /約分return temp;bool fraction:equal(fraction b)makeCommond(b); /通分return(above=b.above);bool fraction:greaterThan(fraction b)makeCommond(b); /通分return(above>b.above);bool fraction:lessThan(fraction

7、 b)makeCommond(b); /通分return(above<b.above);void fraction:display()reduction(); /約分cout<<"為:"<<above<<"/"<<below<<endl;void fraction:input()while(1)cout<<"請(qǐng)順序輸入分子和分母(整數(shù)):"<<endl;cin>>above>>below;if(below=0) cout&

8、lt;<"分母不可為零!"<<endl;elsereduction();return;int main()fraction f1(-3,-5),f2(-3,5),f3(3,-7),f4,f5(8);cout<<"f1" f1.display();cout<<"f2" f2.display();cout<<"f3" f3.display();cout<<"f4" f4.display();cout<<"f5&q

9、uot; f5.display();if(f1.greaterThan(f2) cout<<"f1>f2"<<endl;if(f2.lessThan(f3) cout<<"f2<f3"<<endl;if(f1.equal(f1) cout<<"f1=f1"<<endl;f4=f1.add(f3);cout<<"f4=f1+f3" f4.display();f4=f1.sub(f2);cout<<"f

10、4=f1-f2" f4.display();f4=f1.mul(f3);cout<<"f4=f1*f3" f4.display();f4=f2.div(f3);cout<<"f4=f1/f3" f4.display();f4=f2.reciprocal();cout<<"f4=1/f2" f4.display();f4.input(); cout<<"f4" f4.display();return 0;4.3 構(gòu)造一個(gè)日期時(shí)間類(Timedate),數(shù)據(jù)成員

11、包括年、月、日和時(shí)、分、秒,函數(shù)成員包括設(shè)置日期時(shí)間和輸出時(shí)間,其中年、月請(qǐng)用枚舉類型,并完成測試。(包括用成員函數(shù)和用普通函數(shù))解:本題要求僅是定義類的練習(xí),并非實(shí)用的提供日期時(shí)間的程序。實(shí)用的日期時(shí)間程序見附錄二的日期時(shí)間函數(shù)。#include <iostream>#include <iomanip>using namespace std;enum YRY2000,Y2001,Y2002,Y2003,Y2004,Y2005;/enum MTJan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec;class Timedatepr

12、ivate:YR year;MT month;int date;int hh;int mm;int ss;public:Timedate()year=Y2000;month=Jan;date=1;hh=0;mm=0;ss=0;Timedate(YR a,MT b,int c)year=a;month=b;date=c;hh=12;mm=30;ss=0;void getdate(YR &,MT &,int &);/使用引用一次取得3個(gè)數(shù)值void gettime(int &,int &,int &);void putdate(YR ,MT ,int

13、 );void puttime(int ,int ,int );void list();void Timedate:getdate(YR &y,MT &m,int &d)y=year;m=month;d=date;void Timedate:gettime(int &a,int &b,int &c)a=hh;b=mm;c=ss;void Timedate:putdate(YR a,MT b,int c)year=a;month=b;date=c;void Timedate:puttime(int a,int b,int c)hh=a;mm=b;s

14、s=c;void Timedate:list()/成員函數(shù),直接訪問私有的數(shù)據(jù)成員cout<<"year/month/date :"switch(year)case Y2000:cout<<"2000"break;case Y2001:cout<<"2001"break;case Y2002:cout<<"2002"break;case Y2003:cout<<"2003"break;case Y2004:cout<<&qu

15、ot;2004"break;case Y2005:cout<<"2005"break;switch(month)case Jan:cout<<'/'<<"Jan"break;case Feb:cout<<'/'<<"Feb"break;case Mar:cout<<'/'<<"Mar"break;case Apr:cout<<'/'<<

16、"Apr"break;case May:cout<<'/'<<"May"break;case Jun:cout<<'/'<<"Jun"break;case Jul:cout<<'/'<<"Jul"break;case Aug:cout<<'/'<<"Aug"break;case Sep:cout<<'/'<

17、;<"Sep"break;case Oct:cout<<'/'<<"Oct"break;case Nov:cout<<'/'<<"Nov"break;case Dec:cout<<'/'<<"Dec"break;cout<<'/'<<date<<endl;cout<<"hour:minite:second :"

18、;cout<<hh<<':'<<mm<<':'<<ss<<endl;int main(int argc, char* argv)Timedate A(Y2004,Mar,3),B;A.list();B.list();B.putdate(Y2005,Oct,18);B.puttime(17,30,00);B.list();return 0;4.4 設(shè)計(jì)并測試一個(gè)矩形類(Rectangle),屬性為矩形的左下與右上角的坐標(biāo),矩形水平放置。操作為計(jì)算矩形周長與面積。測試包括用成員函數(shù)和普通函數(shù)。解

19、:這里的矩形的4邊分別與x軸y軸平行,為最簡單的情況。注意參數(shù)有缺省值的函數(shù)的聲明和定義格式。#include <iostream>#include <cmath>using namespace std;class Rectangle double left, top ;double right, bottom;public:Rectangle(double l=0, double t=0, double r=0, double b=0); Rectangle(); /析構(gòu)函數(shù),在此函數(shù)體為空void Assign(double l,double t,double r,

20、double b);double getLeft() return left; / 以下四個(gè)函數(shù)皆為內(nèi)聯(lián)成員函數(shù)double getRight() return right;double getTop()return top;double getBottom()return bottom;void Show();double Area();double Perimeter();/ 構(gòu)造函數(shù),帶缺省參數(shù),缺省值為全0,在聲明中指定Rectangle:Rectangle(double l , double t, double r, double b) left = l; top = t;right

21、 = r; bottom = b; void Rectangle:Assign(double l, double t, double r, double b)/賦值left = l; top = t;right = r; bottom = b;void Rectangle:Show()/成員函數(shù)直接使用私有的數(shù)據(jù)成員cout<<"left-top point is ("<<left<<","<<top<<")"<<'n'cout<<&q

22、uot;right-bottom point is ("<<right<<","<<bottom<<")"<<'n'double Rectangle:Area()return fabs(right-left)*(bottom-top);double Rectangle:Perimeter()return 2*(fabs(right-left)+fabs(bottom-top);int main()Rectangle rect; rect.Show();rect.Assi

23、gn(100,200,300,400);rect.Show();Rectangle rect1(0,0,200,200);rect1.Show();Rectangle rect2(rect1);rect2.Show();cout<<"面積"<<rect.Area()<<'t'<<"周長"<<rect.Perimeter()<<endl;return 0;4.5 定義一個(gè)圓類(Circle),屬性為半徑(radius)、圓周長和面積,操作為輸入半徑并計(jì)算周長、面積,輸出半徑、周長和面積。要求定義構(gòu)造函數(shù)(以半徑為參數(shù),缺省值為0,周長和面積在構(gòu)造函數(shù)中生成)和拷貝構(gòu)造函數(shù)。解:通常所有數(shù)據(jù)成員都在構(gòu)造函數(shù)中賦初值??截悩?gòu)造函數(shù)以本類的引用為參數(shù)。#include<iostream>#include<cmath>using namespace std;class Circledouble r,Area,Circumference;public:Circle(double a=0);Circle(Circle &);vo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論