第九周任务1(Complex类中的<<和>>运算符的重载)

本文介绍了一个名为Complex的类,该类实现了复数的基本算术运算符重载,包括加、减、乘、除,并进一步扩展了输入输出运算符(<< 和 >>)。通过这些重载,可以更直观地进行复数的数学操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*(文件注释头部开始)
*程序的版权和版本声明部分
*Copyright (c) 2011,烟台大学计算机学院学生
*All rights reserved.
*文件名称:Complex类中的<<和>>运算符的重载

*作    者:王智凯
*完成日期:2011年4月16号
*版本号:凯子

* 对任务及求解方法的描述部分
* 输入描术:

* 问题描述:在重载运算符+、-、*、/的基础上,增加重载运算符>>  << ,实现输入和输出。
* 程序输出:

* 程序头部的注释结束
*/ 

 

#include <iostream>

#include <iomanip>

using namespace std;

class Complex
{
public:
	Complex(){real = 0; imag = 0;}
	Complex(double r){real = r;imag = 0;}//类型转换函数
	Complex(double r,double i){real = r; imag = i;}
	friend Complex operator+ (Complex c1,Complex c2);
	friend Complex operator- (Complex c1, Complex c2);
	friend Complex operator- (Complex &c);
	friend Complex operator* (Complex c1, Complex c2);
	friend Complex operator/ (Complex c1, Complex c2);
	friend ostream& operator << (ostream &,Complex &);
	friend istream& operator >> (istream &,Complex &);
private:
	double real;
	double imag;
};

Complex operator+ (Complex c1, Complex c2)
{
	return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

Complex operator- (Complex c1, Complex c2)
{
	return Complex(c1.real - c2.real, c1.imag - c2.imag);
}

Complex operator - (Complex &c)
{ 
	return Complex(-c.real, -c.imag);
}

Complex operator* (Complex c1, Complex c2)
{
	return Complex(c1.real * c2.real - c1.imag * c2.imag, c1.imag * c2.real + c1.real * c2.imag);
}

Complex operator/ (Complex c1, Complex c2)
{
	return Complex((c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag), (c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag));
}
ostream& operator << (ostream &ouput,Complex &c)
{
	ouput << "(" << setprecision(3) << c.real;
	if(c.imag >= 0)
		ouput << "+";
	ouput << setprecision(3) << c.imag << "i)" << endl;
	return ouput;
}

istream& operator >> (istream &input, Complex &c)
{
	char c1;
	cout << "input real part and imaginary part of complex number:";
	input >> c.real >> c.imag;
	return input;
}

int main()
{
	double d = 3;
	Complex c1, c2, c3;
	cin >> c1;
	cout << "c1 = " << c1;

	cin >> c2;
	cout << "c2 = " << c2;

	c3 = c1 + c2;
	cout << "c1 + c2 = " << c3;

	c3 = c1 + d;
	cout << "c1 + " << d << "= " << c3;

	c3 = d + c1;
	cout << d << " + c1" << "= " << c3;

	c3 = c1 - c2;
	cout << "c1 - c2 = " << c3;

	c3 = c1 - d;
	cout << "c1 - " << d << "= " << c3;

	c3 = d - c1;
	cout << d << " - c1" << "= " << c3;

	c3 = - c1;
	cout << " -c1= " << c3;


	c3 = c1 * c2;
	cout << "c1 * c2 = " << c3;

	c3 = c1 * d;
	cout << "c1 * " << d << "= " << c3;

    c3 = d * c1;
	cout << d << " * c1" << "= " << c3;

	c3 = c1 / c2;
	cout << "c1 / c2 = " << c3;

	c3 = c1 / d;
	cout << "c1 / " << d << "= " << c3;

	c3 = d / c1;
	cout << d << " / c1" << "= " << c3;

	system("pause");
	return 0;
}

 

 

上机感言:其实用重载流运算符挺方便的 

// 编写一个复数模板Complex,其数据成员realimage的型未知,定义相应 // 的成员函数,包括构造函数、返回实部的函数、返回虚部的函数、重载加法运 // 算符、重载减法运算符以及重载输出运算符。 // 温馨小帖示:参考“相关知识”中的“示例程序”。 #include <iostream> // 编译预处理命令 using namespace std; // 使用命名空间std /************************************************************************ // 数据成员: T real; // 实部 T image; // 虚部 // 成员函数: Complex(T r = 0, T i = 0); // 构造函数 T GetReal() const; // 返回实部 T GetImage() const; // 返回虚部 Complex<T> operator+(const Complex<T> &z); // 加法运算符重载函数(成员函数) // 非成员函数: Complex<T> operator-(const Complex<T> &z1, const Complex<T> &z2); // 减法运算符重载函数(非成员函数) ostream &operator<<(ostream &out, const Complex<T> &z); // 输出运算符重载函数(非成员函数) ************************************************************************/ // 声明复数模板 template <class T> class Complex { private: // 数据成员 // 在下面对realimage进行声明 // 温馨小帖示:参考“相关知识”中“编程要求”的“数据成员、成员函数及 // 重载运算符具体如下”中第2-3行。 /********* Begin *********/ /********* End *********/ public: // 公有函数 Complex(T r = 0, T i = 0): real(r), image(i){ }// 构造函数 T GetReal() const { return real; } // 返回实部 T GetImage() const { return image; } // 返回虚部 // 在下面对加法运算符重载函数operator+()进行声明 // 温馨小帖示:参考“相关知识”中“编程要求”的“数据成员、成员函数及 // 重载运算符具体如下”中第9行。 /********* Begin *********/ /********* End *********/ }; // 在下面定义成员函数及相关函数的定义 // 输出运算符重载函数(非成员函数) template <class T> ostream &operator<<(ostream &out, const Complex<T> &z)// 重载运算符<< { // 温馨小贴士: // (1)运算符重载函数要求输出运算符“<<”,输出格式为“实部+虚部i”,例如:2+3i, 2-3i, -2+3i, 8, -8, 8i, -5i, 0 // (2)在输出完复数后不用加换行符 // (3)z的实部应写为“z.GetReal()”,z的虚部应写为“z.GetImage()” // (4)在本函数体代码中,输出流对象不应cout,而应是形参out,例如“cout << 0;”应改为“out << 0;” /********* Begin *********/ if (z.GetReal() != 0) { // z.GetReal()非0,对z.GetImage()进行讨论(为负,为0,为正),可能输出示例:2-3i, 8, -8, 2+3i if (z.GetImage() < 0) out << z.GetReal() << z.GetImage() << "i";//虚部为负,例如2-5i else if (z.GetImage() == 0) out << z.GetReal(); // 虚部为0,例如3 else out << z.GetReal() << "+" << z.GetImage() << "i"; // 虚部为正,例如2+5i } else { // z.GetReal()为0,对z.GetImage()进行讨论(为0,非0),可能输出示例:0, 8i, -5i if (z.GetImage() == 0) out << 0; // 虚部为0,例如0 else out << z.GetImage() << "i"; // 虚部非0, 例如5i, -8i } /********* End *********/ return out; // 返回输出流对象 } // 加法运算符重载函数(成员函数) template <class T> Complex<T> Complex<T>::operator+(const Complex<T> &z2) { // 温馨小贴士: // (1)运算符重载函数要求返回复数(*this)与z2之; // (2)现在复数型应加上模板参数,也就是应写为“Complex<T>”; // (3)(*this)的实部可写为“this->real”,(*this)的实虚应写为“this->image”; // (4)z2的实部应写为“z2.GetReal()”,z2的虚部应写为“z2.GetImage()”; // (5)参考“相关知识”中“运算符重载基础”的“示例程序”中第16行。 /********* Begin *********/ /********* End *********/ } // 减法运算符重载函数(非成员函数) template <class T> Complex<T> operator-(const Complex<T> &z1, const Complex<T> &z2) { // 温馨小贴士: // (1)运算符重载函数要求返回复数z1与z2之差; // (2)现在复数型应加上模板参数,也就是应写为“Complex<T>”; // (3)z1的实部应写为“z1.GetReal()”,z1的虚部应写为“z1.GetImage()”; // (4)z2的实部应写为“z2.GetReal()”,z2的虚部应写为“z2.GetImage()”; // (5)参考“相关知识”中“运算符重载基础”的“示例程序”中第16行。 /********* Begin *********/ /********* End *********/ } 补全代码
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值