复数类Complex

#include<iostream>
using namespace std;

class Complex
{
public:
	Complex(double real = 0.0, double image = 0.0);
	~Complex();
	Complex(const Complex& c1);

	Complex& operator=(const Complex& c2);//赋值运算
	Complex operator+(const Complex& c2);//两复数相加
	Complex operator-(const Complex& c2);//两复数相减
	Complex operator*(const Complex& c2);//两复数相乘
	Complex operator/(const Complex& c2);//两复数相除
	Complex operator+=(const Complex& c2);//复数对象+=
	Complex operator-=(const Complex& c2);//复数对象-=
	Complex operator*=(const Complex& c2);//复数对象*=
	Complex operator/=(const Complex& c2);//复数对象/=
	Complex operator++();//复数对象的前置++
	Complex operator++(int);//复数对象的后置++
	Complex operator--();//复数对象的前置--
	Complex operator--(int);//复数对象的后置--

	void display();   //输出函数

private:
	double _real;     //实部
	double _image;     //虚部
};

//构造函数
Complex::Complex(double real, double image)
:_real(real),
_image(image)
{
}

// 拷贝构造函数
Complex::Complex(const Complex& c1)
:_real(c1._real),
_image(c1._image)
{
}

//析构函数
Complex::~Complex()
{
}

//赋值函数
Complex& Complex::operator=(const Complex& c2)
{
	_real = c2._real;
	_image = c2._image;
	
	return *this;
}

//两复数相加函数
Complex Complex::operator+(const Complex& c2)
{
	Complex c;

	c._real = _real + c2._real;
	c._image= _image + c2._image;

	return c;
}

//两复数相减函数
Complex Complex::operator-(const Complex& c2)
{
	Complex c;

	c._real = _real - c2._real;
	c._image = _image - c2._image;

	return c;
}

//两复数相乘函数
Complex Complex::operator*(const Complex& c2)
{
	Complex c;

	c._real = (_real * c2._real) - (_image*c2._image);
	c._image = (_image * c2._real) + (_real*c2._image);

	return c;
}

//两复数相除函数
Complex Complex::operator/(const Complex& c2)
{
	Complex c;

	c._real = (_real*c2._real + _image*c2._image) / (c2._real*c2._real + c2._image*c2._image);
	c._image = (_image*c2._real - _real*c2._image) / (c2._real*c2._real + c2._image*c2._image);

	return c;
}

//复数对象+=函数
Complex Complex::operator+=(const Complex& c2)
{
	/*_real = _real + c2._real;
	_image =_image + c2._image;

	return *this;*/

	*this = *this + c2;

	return *this;
}

//复数对象的-=函数
Complex Complex::operator-=(const Complex& c2)
{
	/*_real -= c2._real;
	_image -= c2._image;

	return *this;*/

	*this = *this - c2;

	return *this;
}

//复数对象的*=函数
Complex Complex::operator*=(const Complex& c2)
{
	/*double a = _real;
	double b = _image;

	_real = (a * c2._real) - (b*c2._image);
	_image = (b * c2._real) + (a*c2._image);

	return *this;*/

	*this = *this*c2;

	return *this;
}

//复数对象的/=函数
Complex Complex::operator/=(const Complex& c2)
{
	/*double a = _real;
	double b = _image;

	_real = (a*c2._real + b*c2._image) / (c2._real*c2._real + c2._image*c2._image);
	_image = (b*c2._real - a*c2._image) / (c2._real*c2._real + c2._image*c2._image);

	return *this;*/

	*this = *this / c2;

	return c2;
}

//复数对象的前置++函数
Complex Complex::operator++()
{
	_real++;
	_image++;

	return *this;
}

//复数对象的后置++函数
Complex Complex::operator++(int)
{
	Complex c = *this;

	_real++;
	_image++;

	return c;
}

//复数对象的前置--函数
Complex Complex::operator--()
{
	_real--;
	_image--;

	return *this;
}

//复数对象的后置--函数
Complex Complex::operator--(int)
{
	Complex c = *this;

	_real--;
	_image--;

	return c;
}

//输出函数
void Complex::display()
{
	cout <<"("<< _real <<","<< _image << "i)" << endl;
}

void test1()//赋值运算函数测试
{
	Complex d1(3, 4), d2;

	d2 = d1.operator=(d1);

	cout << "d1="; d1.display();
	cout << "d2="; d2.display();
}

void test2()//两复数相加函数测试
{
	Complex d1(3, 4), d2(5, -10), d3;

	d3 = d1.operator+(d2);

	cout << "d1="; d1.display();
	cout << "d2="; d2.display();
	cout << "d1+d2="; d3.display();
}

void test3()//两复数相减函数测试
{
	Complex d1(3, 4), d2(1, 10), d3;

	d3 = d1.operator-(d2);

	cout << "d1="; d1.display();
	cout << "d2="; d2.display();
	cout << "d1-d2="; d3.display();
}

void test4()//两复数相乘函数测试
{
	Complex d1(3, 4), d2(1, 2), d3;

	d3 = d1.operator*(d2);

	cout << "d1="; d1.display();
	cout << "d2="; d2.display();
	cout << "d1*d2="; d3.display();
}

void test5()//两复数相除函数测试
{
	Complex d1(2, 4), d2(1, 2), d3;

	d3 = d1.operator/(d2);

	cout << "d1="; d1.display();
	cout << "d2="; d2.display();
	cout << "d1/d2="; d3.display();
}

void test6()//复数对象+=函数测试
{
	Complex d1(1, 2),d2(3,4);

	d1=d1.operator+=(d2);

	cout << "d1="; d1.display();
}

void test7()//复数对象的-=函数测试
{
	Complex d1(1, 2),d2(3,4);

	d1 = d1.operator-=(d2);

	cout << "d1="; d1.display();
}

void test8()//复数对象的*=函数测试
{
	Complex d1(3, 4),d2(1,2);

	d1 = d1.operator*=(d2);

	cout << "d1="; d1.display();
}

void test9()//复数对象的/=函数测试
{
	Complex d1(1, 2),d2(3,4);

	d1 = d1.operator/=(d2);

	cout << "d1="; d1.display();
}

void test10()//复数对象的前置++函数测试
{
	Complex d1(1, 2);

	d1 = d1.operator++();

	cout << "d1="; d1.display();
}

void test11()//复数对象的后置++函数测试
{
	Complex d1(1, 2),d2;

	d2 = d1.operator++(1);

	cout << "d1="; d1.display();
	cout << "d2="; d2.display();
}

void test12()//复数对象的前置--函数测试
{
	Complex d1(3, 4);

	d1 = d1.operator--();

	cout << "d1="; d1.display();
}

void test13()//复数对象的后置--函数测试
{
	Complex d1(3, 4), d2;

	d2 = d1.operator--(1);

	cout << "d1="; d1.display();
	cout << "d2="; d2.display();
}

int main()
{
	test6();

	system("pause");
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值