【C++】复数的运算符重载



运算符重载的定义:


用户对于自定义类型的运算操作,例如复数的运算。需要重新定义运算符号(创建函数)。

        除了类属关系运算符"."、成员指针运算符".*"、作用域运算符"::"、sizeof运算符和三目运算符"?:"以外,C++中的所有运算符都可以重载。




复数的运算符重载如下:

<span style="font-size:18px;">
#include<iostream>
#include<string>
using namespace std;


class Complex;
Complex operator+(int x, Complex &c);
ostream& operator<<(ostream &out, const Complex &c);
istream& operator>>(istream &in, Complex &c);

class Complex
{
	friend istream& operator>>(istream &in, Complex &c);
	friend ostream& operator<<(ostream &out, const Complex &c);
	friend Complex operator+(int x, Complex &c);
public:
	Complex(const int real = 0, const int imag = 0) :m_real(real), m_imag(imag)
	{}
	~Complex()
	{}
	void Show()const
	{
		cout << "(" << m_real << "," << m_imag << ")" << endl;
	}



public:
	Complex operator+(int x)
	{
		return Complex(m_real + x, m_imag);
	}
	Complex operator+(Complex &c)
	{
		return Complex(m_real + c.m_real, m_imag + c.m_imag);
	}

	Complex operator-(Complex &c)
	{
		return Complex(m_real - c.m_real, m_imag - c.m_imag);
	}



	friend Complex operator+(int x, Complex &c)
	{
		return Complex(x + c.m_real, c.m_imag);
	}

	friend Complex operator-(int x, Complex &c)
	{
		return Complex(x - c.m_real, -c.m_imag);
	}

	Complex operator*(Complex &c);
	friend Complex operator*(int x, Complex &c);
	Complex operator/(Complex &c);
	friend Complex operator/(int x, Complex &c);
	Complex operator+=(Complex &c);
	Complex operator-=(Complex &c);
	Complex operator*=(Complex &c);
	Complex operator/=(Complex &c);

private:
	int m_real;
	int m_imag;
};




Complex Complex::operator/(Complex &c)
{
	Complex temp;

	temp.m_real = (m_real*c.m_real + m_imag*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);
	temp.m_imag = (m_imag*c.m_real - m_real*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);
	return temp;
}

Complex operator/(int x, Complex &c)
{
	Complex temp;

	temp.m_real = (x*c.m_real) / (c.m_real*c.m_real + c.m_imag*c.m_imag);
	temp.m_imag = (-x*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);
	return temp;
}

Complex Complex:: operator+=(Complex &c)
{
	m_real = m_real + c.m_real;
	m_imag = m_imag + c.m_imag;
	return *this;
}

Complex Complex::operator-=(Complex &c)
{
	m_real = m_real - c.m_real;
	m_imag = m_imag - c.m_imag;
	return *this;
}


Complex Complex:: operator*(Complex &c)
{
	return Complex(m_real*c.m_real - m_imag*c.m_imag, m_imag*c.m_real + m_real*c.m_imag);
}

Complex operator*(int x, Complex &c)
{
	return Complex(c.m_real*x, c.m_imag*x);
}

Complex Complex::operator*=(Complex &c)
{
	int i = m_real;
	m_real = m_real*c.m_real - m_imag*c.m_imag;
	m_imag = m_imag*c.m_real + i*c.m_imag;
	return *this;
}


Complex Complex:: operator/=(Complex &c)
{

	m_real = (m_real*c.m_real + m_imag*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);
	m_imag = (m_imag*c.m_real - m_real*c.m_imag) / (c.m_real*c.m_real + c.m_imag*c.m_imag);
	return *this;
}
ostream& operator<<(ostream &out, const Complex &c)
{
	out << "(" << c.m_real << "," << c.m_imag << ")";
	return out;
}

istream& operator>>(istream &in, Complex &c)
{
	in >> c.m_real >> c.m_imag;
	return in;
}




int main()
{
	Complex c1(2, 1);
	Complex c2(3, 2);

	Complex c;
	
	c = c1 + c2;
	cout<<c1<<"+"<<c2<<"="<<c<<endl;

	c = c1 * c2;
	cout << c1 << "*" << c2 << "=" << c << endl;

	c = c1 / c2;
	cout << c1 << "*" << c2 << "=" << c << endl;

	c = 2 + c1;
	cout <<"2"<< "+" << c1 << "=" << c << endl;
	//    c1+=c2;
	//    cout<<c1<<endl;
	//    c1-=c2;
	//    cout<<c1<<endl;
	c1 *= c2;
	cout << c1 << endl;
	c1 /= c2;
	cout << c1 << endl;

	c = 4 / c1;
	cout << c << endl;
	 Complex c3;
	 cin>>c3;
	 cout<<c3;
	return 0;
}
</span>



运行结果:






复数定义包括两个私有成员变量:实部和虚部,以及一些公有成员函数来进行复数的基本运算。以下是一个简单的复数定义示例: ```C++ class Complex { private: double real; // 实部 double imag; // 虚部 public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} Complex operator+(const Complex& c) const { // 加法运算符重载 return Complex(real + c.real, imag + c.imag); } Complex operator-(const Complex& c) const { // 减法运算符重载 return Complex(real - c.real, imag - c.imag); } Complex operator*(const Complex& c) const { // 乘法运算符重载 return Complex(real*c.real - imag*c.imag, real*c.imag + imag*c.real); } Complex operator/(const Complex& c) const { // 除法运算符重载 double denominator = c.real*c.real + c.imag*c.imag; return Complex((real*c.real + imag*c.imag) / denominator, (imag*c.real - real*c.imag) / denominator); } friend ostream& operator<<(ostream& os, const Complex& c) { // 输出运算符重载 os << c.real << "+" << c.imag << "i"; return os; } }; ``` 这个复数定义包括了加、减、乘、除四种基本运算符重载,以及一个友元函数重载输出运算符`<<`。其中加、减、乘、除四种运算符均返回一个新的复数对象,而不改变原先的两个对象。 需要注意的是,除法运算符重载时需要注意分母不能为零,否则会导致除数为零的错误。因此,在除法运算符重载中需要进行分母的计算,并且需要对分母是否为零进行判断。 此外,可以根据需要添加其他运算符重载,比如复数的取模、幂运算等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值