【C++】模拟实现一个复数类,要求实现 加,减,乘,除等基本运算符的重载

Complex.h 文件

#ifndef __COMPLEX_H__
#define __COMPLEX_H__
#include

using namespace std;

class Complex{
public:
	Complex(double real,double image)
		:_real(real)
		,_image(image)
	{
	}
	Complex operator + (const Complex& c);
	Complex operator - (const Complex& c);
	Complex operator * (const Complex& c);
	Complex operator / (const Complex& c);

	Complex& operator += (const Complex& c);
	Complex& operator -= (const Complex& c);
	Complex& operator *= (const Complex& c);
	Complex& operator /= (const Complex& c);

	Complex& operator = (const Complex& c);
	bool operator != (const Complex& c);
	bool operator == (const Complex& c);
	//输出指定复数
	void PrintComplex(){
		cout<<_real<<"+"<<"("<<_image<<"i"<<")\n"<

Complex.cpp实现文件:

#include
#include"Complex.h"

using namespace std;


bool Complex::operator==(const Complex& c){
	return ((_real == c._real) && (_image == c._image));
}

bool Complex::operator!=(const Complex& c){
	return !(*this == c);
}
Complex& Complex::operator=(const Complex& c){
	_real = c._real;
	_image = c._image;
	return *this;
}


//(a+bi)+(c+di)=(a+c)+(b+d)i
Complex Complex::operator+(const Complex& c){
	Complex temp(*this);
	temp._real = _real + c._real;
	temp._image = _image + c._image;
	return temp;
}
//(a+bi)-(c+di)=(a-c)+(b-d)i
Complex Complex::operator-(const Complex& c){
	Complex temp(*this);
	temp._real = _real - c._real;
	temp._image = _image - c._image;
	/*if(temp._image)*/
	return temp;
}
//(a+bi)(c+di)=(ac-bd)+(bc+ad)i
Complex Complex::operator*(const Complex& c){
	Complex temp(*this);
	temp._real = (_real * c._real) - (_image * c._image);
	temp._image = (_image * c._real) + (_real * c._image);
	return temp;
}
//(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i
Complex Complex::operator/(const Complex& c){
	Complex temp(*this);
	temp._real = ((_real * c._real) + (_image * c._image))/((c._real * c._real) + (c._image * c._image));
	temp._image = ((_image * c._real) - (_real * c._image)) / ((c._real * c._real) + (c._image * c._image));
	return temp;
}

Complex& Complex::operator+=(const Complex& c){
	_real = _real + c._real;
	_image = _image + c._image;
	return *this;
}

Complex& Complex::operator-=(const Complex& c){
	_real = _real - c._real;
	_image = _image - c._image;
	return *this;
}

Complex& Complex::operator*=(const Complex& c){
	Complex temp(*this);
	temp._real = (_real * c._real) - (_image * c._image);
	temp._image = (_image * c._real) + (_real * c._image);
	_real = temp._real;
	_image = temp._image;
	return *this;
}

Complex& Complex::operator/=(const Complex& c){
	Complex temp(*this);
	temp._real = ((_real * c._real) + (_image * c._image))/((c._real * c._real) + (c._image * c._image));
	temp._image = ((_image * c._real) - (_real * c._image)) / ((c._real * c._real) + (c._image * c._image));
	_real = temp._real;
	_image = temp._image;
	return *this;
}

test.cpp 测试文件:

#include
#include
#include"Complex.h"


void test(){
	/*cout<<"复数c1"<
乘除:

【C++】模拟实现一个复数类,要求实现 加,减,乘,除等基本运算符的重载_第1张图片

加减:


【C++】模拟实现一个复数类,要求实现 加,减,乘,除等基本运算符的重载_第2张图片

相等赋值:

【C++】模拟实现一个复数类,要求实现 加,减,乘,除等基本运算符的重载_第3张图片

你可能感兴趣的:(C++)