C++第4次作业.doc_第1页
C++第4次作业.doc_第2页
C++第4次作业.doc_第3页
C++第4次作业.doc_第4页
C++第4次作业.doc_第5页
已阅读5页,还剩4页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左上角与右下角两个点的坐标,根据坐标能计算矩形的面积。(1) 实验目的:根据坐标求矩形的面积。(2) 实验分析:类的基础题,题目的主要难点在于函数的调用。(3) 程序代码:#includeusing namespace std;class Rectangle public:Rectangle(double newL1=0,double newL2=0,double newR1=0,double newR2=0);double GetArea() const;private:double left1,left2,right1,right2;Rectangle:Rectangle(double newL1,double newL2,double newR1,double newR2)left1=newL1;left2=newL2;right1=newR1;right2=newR2;double Rectangle:GetArea() constdouble Width = right1 - left1;double Height = right2 - left2;return (Width * Height);int main()Rectangle MyRectangle (10, 20, 50, 80 );double Area = MyRectangle.GetArea();cout 矩形的面积: Area endl;return 0;(4) 程序结果:2. 定义一个Circle类,有数据成员radius(半径),成员函数getArea(),计算圆的面积,构造一个Circle的对象进行测试。(1) 实验分析:构造一个Circle类来计算圆的面积。(2) 程序代码:#include#define Pi 3.14using namespace std;class Circleprivate:double Radius;public:Circle(double R)Radius=R;double GetArea();double Circle:GetArea()return Pi*Radius*Radius;void main()double i;cout请输入半径:i;Circle C(i);double area=C.GetArea();cout面积为:areaendl;(3) 运行结果:3. 设计一个类sample,实现两个复数的加法和减法运算。(1) 实验分析:题目不难,将复数的实部和虚部分开加减即可(2) 程序代码:#include class Sample private: double real; double imag;public: Sample(double r=0.0,double i=0.0); void print(); Sample Sample:operator+(Sample s); Sample Sample:operator-(Sample m); Sample:Sample(double r,double i) real=r;imag=i; Sample Sample:operator+(Sample s) Sample a; a.real=real+s.real; a.imag=imag+s.imag; return a; Sample Sample:operator-(Sample m) Sample a; a.real=real-m.real; a.imag=imag-m.imag; return a; void Sample:print() cout0)cout+; if(imag!=0)coutimagi; int main() double r1,i1,r2,i2; cout请输入第一个复数的实部和虚部:r1i1;cout请输入第二个复数的实部和虚部:r2i2; Sample s1=Sample(r1,i1),s2=Sample(r2,i2); Sample s3=s1+s2;cout两复数的和为:; s3.print(); coutendl;Sample m1=Sample(r1,i1),m2=Sample(r2,i2); Sample m3=m1-m2; cout两复数的差为:; m3.print(); coutendl;return 0; (3) 运行结果:4. 设计一个字符串类MYSTRING,具有构造函数、析构函数、拷贝构造函数等,尽可能的完善它,使之能满足各种需要。5. 参照下例:“小程序:看看你活了多少天?”编写如下程序:(1)用OOP方法改写上述程序,(编写类Date实现)。(2)编写一个时间类,它包含Date和Clock类对象;(3)求到现在你总共活了多少小时?例:小程序:看看你活了多少天/*filename:mylife.cpp*/#includeint dayspass(int y,int m,int d);/求y年m月d日这年过了多少天int daysofyear(int y); /求y年共有多少天int daysleft(int y,int m,int d);/求y年m月d日这年还剩多少天void main()int y1,m1,d1,y2,m2,d2,days;couty1m1d1;couty2m2d2;days=daysleft(y1,m1,d1);for(int y=y1+1;ydays+=daysofyear(y);days+=dayspass(y2,m2,d2);coutYou have lived int dayspass(int y,int m,int d)int i,leap=0,md212=31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31;if(y%4=0&y%100!=0|y%400=0)leap=1;for(i=0;ireturn(d);int daysofyear(int y)if(y%4=0&y%100!=0|y%400=0)return(366);return(365);int daysleft(int y,int m,int d)return(daysofyear(y)-dayspass(y,m,d);(1) 实验分析:首先需要考虑闰年、平年,然后算出日期之间的年数,闰年乘以366,平年乘以355,再考虑出生那年的天数和现在这年的天数,最后乘以24就是所活的小时数。(2) 程序代码:#include using namespace std;class Datepublic:int year; int month; int day; bool isTrue(int y, int m, int d);Date() year = 1990; month = 1; day = 1; Date(int y, int m, int d) if (isTrue(y, m, d) year = y; month = m;day = d; else year = 1990; month = 1; day = 1; void display()coutyear-month-dayendl;void set(int x,int y,int z)year = x;month = y;day = z;void next()if (year%4 = 0) & (year%100 != 0) | (year%400 = 0)if (month=2)if(day=29)day = 1;month = 3;else day+;else if(month=4 | month=6 | month=9 | month=11)if(day=30)day = 1;month+;else day+;elseif(day=31)day = 1;if(month=12)month = 1;year+;else month+;else day+;elseif (month=2)if(day=28)day = 1;month = 3;else day+;else if(month=4 | month=6 | month=9 | month=11)if(day=30)day = 1;month+;else day+;elseif(day=31)day = 1;if(month=12)month = 1;year+;else month+;else day+;bool Date:isTrue(int y, int m, int d) if (y=0 | m=0 | d=0) return false; if (y%4 = 0) & (y%100 != 0) | (y%400 = 0) if (m = 2 & d = 29) return true; else if (m=4 | m=6 | m=9 | m=11) if (d=30) return true; else return false; if (m=2) if (d=28) return true; else return false; else if (d=31) return true; else return false;int application(Date data1,Date data2)int num = 0;for(int i=0;i+)if(data1.year=data2.year&data1.month=data2.month&data1.day=data2.day) break;data1.next();num+;return num;void main() c

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论