您的位置:首页 > 编程语言 > C语言/C++

C02-程序设计基础提高班(C++)第11周上机任务-运算符重载

2012-11-06 23:14 555 查看
第11周:阅读教材第10章(p314-346),掌握用运算符重载解决问题,完成第11周上机任务;

(回到C02-程序设计基础提高班(C++)学习安排)

【任务1】实现复数类中的运算符重载
定义一个复数类重载运算符+、-、*、/,使之能用于复数的加减乘除。
(1)方案一:请用类的成员函数完成运算符的重载;

class Complex
{
public:
	Complex(){real=0;imag=0;}
	Complex(double r,double i){real=r;imag=i;}
	Complex operator+(Complex &c2);
	Complex operator-(Complex &c2);
	Complex operator*(Complex &c2);
	Complex operator/(Complex &c2);
	void display();
private:
	double real;
	double imag;
};
//下面定义成员函数

int main()
{
	Complex c1(3,4),c2(5,-10),c3;
	cout<<"c1=";
	c1.display();
	cout<<"c2=";
	c2.display();
	c3=c1+c2;
	cout<<"c1+c2=";
	c3.display();
	c3=c1-c2;
	cout<<"c1-c2=";
	c3.display();
	c3=c1*c2;
	cout<<"c1*c2=";
	c3.display();
	c3=c1/c2;
	cout<<"c1/c2=";
	c3.display();
	system("pause");
	return 0;
}

(2)方案二:请用类的友元函数,而不是成员函数,完成上面提及的运算符的重载;
(3)方案三:在方案二的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; double d; c?d和d?c的结果为“将d视为实部为d的复数同c运算”的结果(其中?为+、-、*、/之一)。另外,再定义一目运算符 -,-c相当于0-c。
[ 参考:复数类中运算符重载(成员函数实现)-/article/1353533.html ]
【任务1进阶】接任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
[ 参考:Complex类中运算符的重载 - /article/1353527.html ]

【任务2】实现Time类中的运算符重载

#include <iostream>
using namespace std;
class CTime
{
private:
	unsigned short int hour;    // 时
	unsigned short int minute;  // 分
	unsigned short int second;  // 秒
public:
	CTime(int h=0,int m=0,int s=0);
	void setTime(int h,int m,int s);
	void display();
	//比较运算符(二目)的重载
	bool operator > (CTime &t);
	bool operator < (CTime &t);
	bool operator >= (CTime &t);
	bool operator <= (CTime &t);
	bool operator == (CTime &t);
	bool operator != (CTime &t);
	//二目运算符的重载
	CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
	CTime operator-(CTime &c);//对照+理解
	CTime operator+(int s);//返回s秒后的时间
	CTime operator-(int s);//返回s秒前的时间
	//一目运算符的重载
	CTime operator++(int);//后置++,下一秒
	CTime operator++();//前置++,下一秒
	CTime operator--(int);//后置--,前一秒
	CTime operator--();//前置--,前一秒
	//赋值运算符的重载     
	CTime operator+=(CTime &c);
	CTime operator-=(CTime &c);
	CTime operator+=(int s);//返回s秒后的时间
	CTime operator-=(int s);//返回s秒前的时间
};
//下面实现所有的运算符重载代码。
//为简化编程,请注意通过调用已有函数,利用好各函数之间的关系

void main()
{
	CTime t1(8,20,25),t2(11,20,50),t;
	cout<<"t1为:";
	t1.display();
	cout<<"t2为:";
	t2.display();
	cout<<"下面比较两个时间大小:\n";
	if (t1>t2) cout<<"t1>t2"<<endl;
	if (t1<t2) cout<<"t1<t2"<<endl;
	if (t1==t2) cout<<"t1=t2"<<endl; 
	if (t1!=t2) cout<<"t1≠t2"<<endl;
	if (t1>=t2) cout<<"t1≥t2"<<endl;
	if (t1<=t2) cout<<"t1≤t2"<<endl;
	cout<<endl;
	//下面自行设计对其他运算符的重载的测试
    ……

}
[ 参考:实现Time类中的运算符重载 - /article/1353530.html ]
【任务2进阶】接任务2,定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
[ 参考:Time类中运算符重载 - /article/1353526.html ]

【任务3】实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。

class CFraction
{
private:
	int nume;  // 分子
	int deno;  // 分母
 public:
	//构造函数及运算符重载的函数声明
};
//重载函数的实现及用于测试的main()函数

[ 参考:实现分数类中的运算符重载 - /article/1353529.html ]

【任务3进阶】接任务3,定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

[ 参考:分数类中运算符重载 - /article/1353525.html ]

【任务4】在任务3的基础上拓展。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。
[ 参考:实现分数类中的运算符重载(分数与整数运算)- /article/1353528.html ]

【任务5】在教材P324例10.4基础上,在String类中增加一个数据成员len表示字符串中字任个数,然后构造String类的相关运算。这些运算可以包括:s1 + s2用于两个字符串的连接;s1 - s2用于将s1的尾部空格和s2的前导空格去除后的连接;s1*n的结果为将s1中的字符重复n次;s1/s2表示在s1中删除所有与s2相同的子串。这些运算本身并无统一的规范,多想一些字符串上的操作,利用运算符的重载实现之。

【任务6】建立一个二维数组类Douary,使该类中有以下数据成员、成员函数及友员函数,完成矩阵的输入、输出、加、减、相等判断等操作。

#include <iostream>
using namespace std;
class Douary
{
public:
	Douary(int m, int n);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为
	~Douary(); //析构函数:用于释放动态数组所占用的存储空间。
	   //此处增加一个复制构造函数
	friend istream &operator>>(istream &input, Douary &d);//重载运算符“>>”输入二维数组,其中d为Dousry类对象;
	friend ostream &operator<<(ostream &output, Douary &d);//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。
	friend Douary operator+(const Douary &d1,const Douary &d2);//两个矩阵相加,规则:对应位置上的元素相加
	friend Douary operator-(const Douary &d1,const Douary &d2);//两个矩阵相减,规则:对应位置上的元素相减
	bool operator==(const Douary &d);//判断两个矩阵是否相等,即对应位置上的所有元素是否相等
private:
	int * Array;      //Array 为动态数组指针。
	int row;          //row  为二维数组的行数。
	int col;          //col   为二维数组的列数。
}
int main()
{
	Douary d1(2,3),d2(2,3)
	cout<<"输入d1:"<<endl;
	cin>>d1;
	cout<<"输入d2:"<<endl;
	cin>>d2;
	coutt<<"d1="<<endl;
	cout<<d1;
	coutt<<"d2="<<endl;
	cout<<d2;
	coutt<<"d1+d2="<<endl;
	cout<<(d1+d2);
	coutt<<"d1-d2="<<endl;
	cout<<(d1-d2);
	cout<<"d1"<<((d1==d2)?"==":"!=")<<"d2"<<endl;
	system("pause");
	return 0;
}
[ 参考:二维数组类 - /article/1353524.html ]

【任务7】设计一元一次方程类,求形如ax+b=0的方程的解。
例如:输入3x-8=0时,输出的方程的解为x=2.66667;
再如:输入5s+18=0时,输出的方程的解为s=-3.6;
#include "iostream"
using namespace std;
class CEquation
{
private:
	double a;     // 未知数系数
	double b;     // 常数项
	char unknown; // 未知数的符号
public:
	CEquation(double aa=0,double bb=0);
	friend istream &operator >> (istream &in,CEquation &e);
	friend ostream &operator << (ostream &out,CEquation &e);
	double Solve();
	char getUnknown();
};

int main()
{
	CEquation e;
	cout<<"请输入一元一次方程(输入格式:3x-8=0):";
	cin>>e;   //在两次测试中,分别输入3x-8=0和5s+18=0
	cout<<"方程为:"<<e;
	cout<<"方程的解为:"<<e.getUnknown()<<"="<<e.Solve()<<endl; //两次测试,分别输出x=...和s=...
	e.Solve();
	system("pause");
	return 0;
}

[ 参考:一元一次方程类 - /article/1353523.html ]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: