第九周-任务1-Complex类中运算符的重载

简介: 【题目】接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。 【参考解答】 #include <iostream>using namespace std;class Complex{public: Complex(){real=0;imag=0;} Co

【题目】接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。


【参考解答】

#include <iostream>
using namespace std;
class Complex
{
public:
	Complex(){real=0;imag=0;}
	Complex(double r,double i){real=r;imag=i;}
	Complex operator-();
	//实现输入、输出的运算符重载
	friend ostream& operator << (ostream& output,Complex& c);
	friend istream& operator >> (istream& input,Complex& c);
	//实现加减乘除的运算符重载
	friend Complex operator+(Complex &c1, Complex &c2);
	friend Complex operator+(double d1, Complex &c2);
	friend Complex operator+(Complex &c1, double d2);
	friend Complex operator-(Complex &c1, Complex &c2);
	friend Complex operator-(double d1, Complex &c2);
	friend Complex operator-(Complex &c1, double d2);
	friend Complex operator*(Complex &c1, Complex &c2);
	friend Complex operator*(double d1, Complex &c2);
	friend Complex operator*(Complex &c1, double d2);
	friend Complex operator/(Complex &c1, Complex &c2);
	friend Complex operator/(double d1, Complex &c2);
	friend Complex operator/(Complex &c1, double d2);
private:
	double real;
	double imag;
};

//实现输出的运算符重载
ostream& operator << (ostream& output,Complex& c)
{	output<<"("<<c.real;
	if(c.imag>=0) output<<"+";  
	output<<c.imag<<"i)";    
	return output;
}

//实现输入的运算符重载
istream& operator >> (istream& input,Complex& c)
{	int a,b;
	char sign,i;
	do
	{	cout<<"input a complex number(a+bi或a-bi):";
		input>>a>>sign>>b>>i;
	}
	while(!((sign=='+'||sign=='-')&&i=='i'));
	c.real=a;
	c.imag=(sign=='+')?b:-b;
	return input;
}


Complex Complex::operator-()
{
	return(0-*this);
}

//复数相加:(a+bi)+(c+di)=(a+c)+(b+d)i. 
Complex operator+(Complex &c1, Complex &c2)
{
	Complex c;
	c.real=c1.real+c2.real;
	c.imag=c1.imag+c2.imag;
	return c;
}
Complex operator+(double d1, Complex &c2)
{
	Complex c(d1,0);
	return c+c2; //按运算法则计算的确可以,但充分利用已经定义好的代码,既省人力,也避免引入新的错误,但可能机器的效率会不佳
}
Complex operator+(Complex &c1, double d2)
{
	Complex c(d2,0);
	return c1+c;
}
//复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.
Complex operator-(Complex &c1, Complex &c2)
{
	Complex c;
	c.real=c1.real-c2.real;
	c.imag=c1.imag-c2.imag;
	return c;
}
Complex operator-(double d1, Complex &c2)
{
	Complex c(d1,0);
	return c-c2;  
}
Complex operator-(Complex &c1, double d2)
{
	Complex c(d2,0);
	return c1-c;
}

//复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.
Complex operator*(Complex &c1, Complex &c2)
{
	Complex c;
	c.real=c1.real*c2.real-c1.imag*c2.imag;
	c.imag=c1.imag*c2.real+c1.real*c2.imag;
	return c;
}
Complex operator*(double d1, Complex &c2)
{
	Complex c(d1,0);
	return c*c2;
}
Complex operator*(Complex &c1, double d2)
{
	Complex c(d2,0);
	return c1*c;
}

//复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i 
Complex operator/(Complex &c1, Complex &c2)
{
	Complex c;
	c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
	c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
	return c;
}
Complex operator/(double d1, Complex &c2)
{
	Complex c(d1,0);
	return c/c2;
}

Complex operator/(Complex &c1, double d2)
{
	Complex c(d2,0);
	return c1/c;
}

int main()
{
	Complex c1,c2,c3;
	double d=11;
	cout<<"c1: "<<endl;; 
	cin>>c1;          
	cout<<"c2: "<<endl;
	cin>>c2;
	cout<<"c1="<<c1<<endl;
	cout<<"c2="<<c2<<endl;
	cout<<"d="<<d<<endl;
	cout<<"-c1="<<(-c1);
	c3=c1+c2;
	cout<<"c1+c2="<<c3<<endl;
	cout<<"c1+d="<<(c1+d)<<endl;
	cout<<"d+c1="<<(d+c1)<<endl;
	c3=c1-c2;
	cout<<"c1-c2="<<c3<<endl;
	cout<<"c1-d="<<(c1-d)<<endl;
	cout<<"d-c1="<<(d-c1)<<endl;
	c3=c1*c2;
	cout<<"c1*c2="<<c3<<endl;
	cout<<"c1*d="<<(c1*d)<<endl;
	cout<<"d*c1="<<(d*c1)<<endl;
	c3=c1/c2;
	cout<<"c1/c2="<<c3<<endl;
	cout<<"c1/d="<<(c1/d)<<endl;
	cout<<"d/c1="<<(d/c1)<<endl;

	system("pause");
	return 0;
}


目录
相关文章
面对对象程序设计之运算符重载(1)定义一个复数类Complex,重载运算符“+”、“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。
面对对象程序设计之运算符重载(1)定义一个复数类Complex,重载运算符“+”、“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。
【C++之运算符重载1】复数类 Complex 重载运算符 “+”
【C++之运算符重载1】复数类 Complex 重载运算符 “+”
艾伟_转载:.NET 4.0 Beta2中的BigInteger和Complex类
.NET4.0 Beta2中提供了新的System.Numerics命名空间,对应于System.Numerics.dll。该命名空间下就两个类BigInteger和Complex,我们来简单了解下这两个类的用法。
1029 0
|
3月前
|
机器学习/深度学习 数据挖掘
【博士每天一篇文献-综述】Communication dynamics in complex brain networks
本文综述了复杂脑网络中的通信动态,提出了一个将通信动态视为结构连接和功能连接之间必要联系的概念框架,探讨了结构网络的局部和全局拓扑属性如何支持网络通信模式,以及网络拓扑与动态模型之间的相互作用如何提供对大脑信息转换和处理机制的额外洞察。
47 2
【博士每天一篇文献-综述】Communication dynamics in complex brain networks
|
3月前
|
机器学习/深度学习
【文献学习】Exploring Deep Complex Networks for Complex Spectrogram Enhancement
介绍了一种用于语音增强的复数深度神经网络(CDNN),它通过复数值的短时傅立叶变换(STFT)映射到干净的STFT,并提出了参数整流线性单位(PReLU)的复数扩展,实验结果表明CDNN在语音增强方面相对于实值深层神经网络(DNN)具有更好的性能。
51 2
【文献学习】Exploring Deep Complex Networks for Complex Spectrogram Enhancement
|
3月前
|
数据可视化 算法 Go
【博士每天一篇文献-实验】Exploring the Morphospace of Communication Efficiency in Complex Networks
这篇论文探讨了复杂网络中不同拓扑结构下的通信效率,并使用"效率形态空间"来分析网络拓扑与效率度量之间的关系,得出结论表明通信效率与网络结构紧密相关。
43 3
|
3月前
|
机器学习/深度学习 网络协议 PyTorch
【文献学习】DCCRN: Deep Complex Convolution Recurrent Network for Phase-Aware Speech Enhancement
本文介绍了一种新的深度复数卷积递归网络(DCCRN),用于处理语音增强问题,特别是针对低模型复杂度的实时处理。
156 5
|
3月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【文献学习】Phase-Aware Speech Enhancement with Deep Complex U-Net
文章介绍了Deep Complex U-Net模型,用于复数值的语音增强,提出了新的极坐标掩码方法和wSDR损失函数,并通过多种评估指标验证了其性能。
56 1
|
3月前
|
机器学习/深度学习 算法 TensorFlow
【文献学习】Analysis of Deep Complex-Valued Convolutional Neural Networks for MRI Reconstruction
本文探讨了使用复数卷积神经网络进行MRI图像重建的方法,强调了复数网络在保留相位信息和减少参数数量方面的优势,并通过实验分析了不同的复数激活函数、网络宽度、深度以及结构对模型性能的影响,得出复数模型在MRI重建任务中相对于实数模型具有更优性能的结论。
39 0
【文献学习】Analysis of Deep Complex-Valued Convolutional Neural Networks for MRI Reconstruction

热门文章

最新文章

下一篇
无影云桌面