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

Complex.h 文件

#ifndef __COMPLEX_H__
#define __COMPLEX_H__
#include<iostream>

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"<<endl;
	}
private:
	double _real;
	double _image;
};

#endif //__COMPLEX_H__

Complex.cpp实现文件:

#include<iostream>
#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<stdio.h>
#include<Windows.h>
#include"Complex.h"


void test(){
	/*cout<<"复数c1"<<endl;
	Complex c1(3,4);
	c1.PrintComplex();
	cout<<"复数c2"<<endl;
	Complex c2(2,2);
	c2.PrintComplex();
	cout<<"测试复数的 '/' "<<endl;
	(c1 / c2).PrintComplex();
	cout<<"测试复数的 '/=' "<<endl;
	(c1 /= c2).PrintComplex();
	cout<<"复数c1"<<endl;
	c1.PrintComplex();

	cout<<"复数c3"<<endl;
	Complex c3(2,4);
	c3.PrintComplex();
	cout<<"复数c4"<<endl;
	Complex c4(3,2);
	c4.PrintComplex();
	cout<<"测试复数的 '*' "<<endl;
	(c3 * c4).PrintComplex();
	cout<<"测试复数的 '*=' "<<endl;
	(c3 *= c4).PrintComplex();
	cout<<"复数c3"<<endl;
	c3.PrintComplex();*/

	/*cout<<"复数c1"<<endl;
	Complex c1(3,4);
	c1.PrintComplex();
	cout<<"复数c2"<<endl;
	Complex c2(2,2);
	c2.PrintComplex();
	cout<<"测试复数的 '+' "<<endl;
	(c1 + c2).PrintComplex();
	cout<<"测试复数的 '+=' "<<endl;
	(c1 += c2).PrintComplex();
	cout<<"复数c1"<<endl;
	c1.PrintComplex();

	cout<<"复数c3"<<endl;
	Complex c3(2,4);
	c3.PrintComplex();
	cout<<"复数c4"<<endl;
	Complex c4(3,2);
	c4.PrintComplex();
	cout<<"测试复数的 '-' "<<endl;
	(c3 - c4).PrintComplex();
	cout<<"测试复数的 '-=' "<<endl;
	(c3 -= c4).PrintComplex();
	cout<<"复数c3"<<endl;
	c3.PrintComplex();*/

	cout<<"复数c1"<<endl;
	Complex c1(3,4);
	c1.PrintComplex();
	cout<<"复数c2"<<endl;
	Complex c2(2,2);
	c2.PrintComplex();
	int i = 3,j = 3;
	i = (c1 == c2);
	j = (c1 != c2);
	cout<<"测试两个复数是否相等    "<<"测试两个复数是否不相等\n"<<endl;
	cout<<"        "<<i<<"                      "<<j<<endl;

	cout<<"复数c3"<<endl;
	Complex c3(2,4);
	c3.PrintComplex();
	cout<<"复数c4"<<endl;
	Complex c4(3,2);
	c4.PrintComplex();
	cout<<"测试复数c4给c3赋值: "<<endl;
	(c3 = c4).PrintComplex();
	cout<<"复数c3"<<endl;
	c3.PrintComplex();
}

int main(){
	test();
	system("pause");
	return 0;
}

程序运行结果如下所示:


乘除:



加减:




相等赋值:


  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值