河北工业大学-C++实验报告实验四(共15页).doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《河北工业大学-C++实验报告实验四(共15页).doc》由会员分享,可在线阅读,更多相关《河北工业大学-C++实验报告实验四(共15页).doc(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上1、编写一个程序,要求:(1)生明一个类Complex(复数类),定义类Complex的两个对象c1和c2,对象c1通过构造函数直接指定复数的实部和虚部(类私有数据成员为double类型:real和imag)为2.5及3.7,对象c2通过构造函数直接指定复数的实部和虚部为4.2及6.5;(2)定义友元运算符重载函数,它以c1、c2对象为参数,调用该函数时能返回两个复数对象相加操作;(3)定义成员函数print,调用该函数时,以格式“real+imag i”输出当前对象的实部和虚部,例如:对象的实部和虚部分别是4.2和6.5,则调用print函数输出格式为:4.2+6.
2、5 i;(4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输出。#includeusing namespace std;class Complex public: Complex(double r=0.0,double i=0.0); friend Complex operator+ (Complex& a,Complex& b); void printf(); private: double real; double imag;Complex:Complex(double r,double i)real=r;imag=i;Complex operator+ (Complex& a
3、,Complex& b)Complex temp;temp.real=a.real+b.real;temp.imag=a.imag+b.imag;return temp;void Complex:printf()cout0)cout+;if(imag!=0)coutimagiendl;void main()Complex c1(2.5,3.7),c2(4.2,6.5),c3;c3=c1+c2;c3.printf();2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在059分,秒钟
4、范围限制在059秒。提示:时间类Time的参考框架如下:class Time public: Time(int h=0,int m=0,int s=0);/构造函数 Time operator+(Time &);/运算符重载函数,实现两个时间的相加 void disptime();/显示时间函数 private: int hours,minutes,seconds;#includeusing namespace std;class Timepublic:Time(int h=0,int m=0,int s=0);/构造函数 Time operator+(Time &);/运算符重载函数,实现两
5、个时间的相加 void disptime();/显示时间函数 private:int hours;intminutes;int seconds;Time:Time(int h,int m,int s)hours=h;minutes=m;seconds=s;Time Time:operator+(Time& t)int h,m,s;s=(t.seconds+seconds)%60;m=(minutes+t.minutes+(t.seconds+seconds)/60)%60;h=hours+t.hours+(minutes+t.minutes+(t.seconds+seconds)/60)/60
6、;hours=h;minutes=m;seconds=s;return *this;void Time:disptime()couthours:minutes:seconds.endl;void Input(int &h,int &m,int &s)couth ;cinm ;cins ;while(m59|s59) cout*时间输入错误!请重新输 !*n;couth ;cinm ;cins ;int main()int h1,m1,s1,h2,m2,s2;Input(h1,m1,s1);Input(h2,m2,s2);Time A(h1,m1,s1),B(h2,m2,s2);A=A+B ;A
7、.disptime();return 0;3、用友元运算符函数或成员运算符函数,重载运算符“+”、“-”、“*”,实现对实验二中实现的矩阵类的对象的加、减、乘法。#include#define hang 2#define lie 2class Matrixprivate:int Row;int Column;int MATRIXhanglie;public:Matrix(int r,int c)Row=r;Column=c;Matrix() void TypeMatrix();void Print() const;Matrix& operator = (const Matrix& rhs);M
8、atrix operator + (const Matrix& rhs);Matrix operator - (const Matrix& rhs);void Matrix:TypeMatrix()std:cout请输入矩阵:std:endl;for(int i=0;ihang;i+)for(int j=0;jMATRIXij;void Matrix:Print() conststd:cout矩阵的结果为:std:endl;for(int q=0;qhang;q+)for(int s=0;slie;s+)std:coutMATRIXqst;if(s=lie-1)std:coutstd:endl
9、;Matrix& Matrix:operator = (const Matrix& rhs)if(this!=&rhs)for(int g=0;ghang;g+)for(int h=0;hMATRIXgh=rhs.MATRIXgh;return *this;Matrix Matrix:operator + (const Matrix& rhs)int i,j;for(i=0;ihang;i+)for(j=0;jMATRIXij+rhs.MATRIXij;return *this ;Matrix Matrix:operator - (const Matrix& rhs)int i,j;for(i
10、=0;ihang;i+)for(j=0;jMATRIXij-rhs.MATRIXij;return *this ;int main()Matrix a,b,c,d;a.TypeMatrix();b.TypeMatrix();c=a+b;c.Print();d=a-b;d.Print();4、编写一个程序,用于进行集合的和、并和交运算。例如输入整数集合9,5,4,3,6,7和2,4,6,9,计算出他们进行集合的并、差和交运算后的结果。【提示】(1)可以用一下表达式实现整数集合的基本运算:s1+s2 两个整数集合的并运算s1-s2 两个整数集合的差运算s1*s2 两个整数集合的交运算(2)参考以下
11、Set类的框架,用于完成集合基本运算所需的各项功能。class Set public: Set(); void input(int d);/向集合中添加一个元素 int length();/返回集合中的元素个数 int getd(int i);/返回集合中位置i的元素 void display();/显示集合的所有元素 Set operator+(Set s1);/成员运算符重载函数,实现集合的并运算 Set operator-(Set s1);/成员运算符重载函数,实现集合的差运算 Set operator*(Set s1);/成员运算符重载函数,实现集合的交运算 Set operator=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 河北 工业大学 C+ 实验 报告 15
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内