【C++运算符重载】复数类(重载加减,关系运算,输入输出)

仅供本人学习记录
复数形如k = a + bi, a是实部,b是虚部。定义类形如
class complex
{
public:

private:
double real ;
double imaginary ;
}
成员函数要求包括:
(a) 重载运算符”+”和”-”,实现两个复数的加减运算,重载 ”==”, ”!=” ,”<” ,” >” 实现两个复数的比较运算,注意复数的">“和”<"的比较,当虚部不等于0时,规定按复数的模的大小进行比较,当虚部为0时,则直接比较实部
(b) 设置实部函数 setReal(double realVal), 设置虚部函数setImag(double imagVal)
© 返回实部函数getReal() ,返回虚部函数getImag()
(d) 重载流运算符”>>”, ”<<” 实现复数的输入输出(注意不能用友元函数)
主函数的测试代码如下:
int main()
{
complex c1, c2, c3, c4, c5 ;
cin >> c1 >> c2 ;
c3 = c1 + c2 ;
c4 = c1 - c2 ;
if ( c3 != c4 )
{
if (c3 < c4)
{
cout << “c3 is less than c4” ;
c5 = c4 - c3 ;
}
if ( c3 > c4 )
{
cout << “c3 is greater than c4” ;
c5 = c3 - c4 ;
}
}
if ( c3 == c4 )
{
cout << “c3 and c4 are equal”;
c5 = c4 ;
}
cout<<endl;
cout << c5 ;
return 0;
}
Input
以a+bi的形式输入两个复数,一行输入一个复数,

Output
第1行输出c3和c4比较大小的结果,第2行输出c5, 实部,虚部均保留两位小数

Sample Input
3+4i
5-6i
Sample Output
c3 is less than c4
-10.00+12.00i
HINT
输入按 a+bi 的格式,可以参考以下方法(最好自己想想还有什么其他的方法进行输入)

double real, imag;

char c1,c2;

is>>real>>c1>>imag>>c2;

注意当读取的c1是 “-” 时,需要将imag变成负数

#include"iostream"
#include"iomanip"
using namespace std;
class complex
{
public:
	complex(double x=0,double y=0):real(x),imaginary(y){}
	complex operator+(const complex & m) const;
	complex operator-(const complex & m) const;
	bool operator==(const complex & m) const;
	bool operator!=(const complex & m) const;
	bool operator>(const complex & m) const;
	bool operator<(const complex & m) const;
	double setReal(double realVal){real=realVal;return 0;}
	double setImag(double imagVal){imaginary=imagVal;return 0;}
	double getReal(){return real;}
	double getImag(){return imaginary;}
private:
    double real ;             
    double imaginary ;
};
complex complex::operator+(const complex & m) const//复习第十章const & 的用法和含义
{
	complex mid;
	mid.real=real+m.real ;
	mid.imaginary =imaginary+m.imaginary;
	return mid;//试试返回&mid会怎么样
}
complex complex::operator-(const complex & m) const
{
	complex mid;
	mid.real=real-m.real ;
	mid.imaginary =imaginary-m.imaginary;
	return mid;
}
bool complex::operator==(const complex & m) const
{
	if(real==m.real && imaginary==m.imaginary)
		return true;
	else
		return false;
}
bool complex::operator!=(const complex & m) const
{
	if(real==m.real && imaginary==m.imaginary)
		return false;
	else
		return true;
}
bool complex::operator>(const complex & m) const
{
	if(imaginary==0 ||m.imaginary==0)
		{
			if(real>m.real)
				return true;
			else
				return false;
		}
	else
		{
			if(((real*real)+(imaginary*imaginary)) > ((m.real*m.real)+(m.imaginary*m.imaginary)))
				return true;
			else
				return false;
		}
}
bool complex::operator<(const complex & m) const
{
	if(imaginary==0 ||m.imaginary==0)
		{
			if(real<m.real)
				return true;
			else
				return false;
		}
	else
		{
			if(((real*real)+(imaginary*imaginary)) < ((m.real*m.real)+(m.imaginary*m.imaginary)))
				return true;
			else
				return false;
		}
}
ostream& operator<<(ostream& os,complex& m)
{
	os<<fixed<<setprecision(2);
	if(m.getImag()<0)
	os<<m.getReal()<<m.getImag()<<"i"<<endl;
	if(m.getImag()>0)
	os<<m.getReal()<<"+"<<m.getImag()<<"i"<<endl;
	return os;
}
istream& operator>>(istream& is,complex &m)
{
	double real, imag;
    char c1,c2;
    is>>real>>c1>>imag>>c2;
	if(c1=='-')
	{
		m.setReal(real);
		m.setImag(-imag);
	}
	else
	{
		m.setReal(real);
		m.setImag(imag);
	}
	return is;
}
int main()
{
complex c1, c2, c3, c4, c5 ;
cin >> c1 >> c2 ;
c3 = c1 + c2 ;
c4 = c1 - c2 ;
if ( c3 != c4 )
{
if (c3 < c4)
{
cout << "c3 is less than c4" ;
c5 = c4 - c3 ;
}
if ( c3 > c4 )
{
cout << "c3 is greater than c4" ;
c5 = c3 - c4 ;
}
}
if ( c3 == c4 )
{
cout << "c3 and c4 are equal";
c5 = c4 ;
}
cout<<endl;
cout << c5 ;
return 0;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值