复数类的完整实现(C++版)

实现一个完整的复数类,包括复数的 + ,-,*,/,+=,-=,*=,/=,前置++,后置++,前置--,后置--, ==(条件判断),!=,>,<=,=(赋值操作符)

1.复数的+,-,*,/,+=,-=,*=,/=

       在这里复数的加减乘除就不做叙述了(中学知识),而加等,减等,乘等,除等与复数的加减乘除类似,不同的是加等,减等,乘等,除等是将相加,相减,相乘,相除的结果直接+,-,*,/到原本的数据处,在这里也不加叙述了,结果如下:
        

2.复数的前置++,后置++,前置--,后置--

       我们知道在C++中是允许重载的,那仫如何区分前置++,和后置++呢?只需要在构成重载的基础上多加一个参数就可以了,经过测试只能传一个整形(int)参数; 由前置++(先加再使用)和后置++(先使用再加)的特性可知,要模拟实现前置++是很容易的只需要实部和虚部直接加并返回相加之后的复数就可以了,而模拟实现后置++就不太容易了,其实只需要我们先利用拷贝构造函数构造出新的复数,再相加实部,虚部,最后返回的是拷贝构造的复数
 
Complex.h
        
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __COMPLEX_H__
#define __COMPLEX_H__

#include<iostream>
using namespace std;

class Complex
{
public:
	Complex(double real,double image);
	void Print();
	bool operator==(const Complex &c);
	Complex& operator++();   //前置++
	Complex operator++(int); //后置++
	Complex& operator--();   //前置--
	Complex operator--(int); //后置--
	bool 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);
	Complex& operator/=(const Complex &c);
private:
	double _real;
	double _image;
};

#endif //__COMPLEX_H__


  Complex.cpp
           
#define _CRT_SECURE_NO_WARNINGS 1
#include"Complex.h"

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

void Complex::Print()
{
	if(_image == 0.0)   //虚部为0
	{
		cout<<_real<<endl;
	}
	else
	{
		cout<<_real<<"+"<<_image<<"*i"<<endl;
	}
}

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

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

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

Complex Complex::operator/(const Complex &c)
{
	Complex tmp;
	double t=c._real*c._real+c._image*c._image;
	tmp._real=(_real*c._real-_image*(-c._image))/t;
	tmp._image=(_real*(-c._image)+_image*c._real)/t;
	return tmp;

}

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

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

Complex& Complex::operator*=(const Complex &c)
{
	Complex tmp(*this);  //拷贝构造函数
	_real=tmp._real*c._real-_image*c._image;
	_image=tmp._real*c._image+tmp._image*c._real;
	return *this;
}

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

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

Complex& Complex::operator++()  //前置++
{
	_real++;
	_image++;
	return *this;
}

Complex Complex::operator++(int) //后置++
{
	Complex tmp(*this);  //拷贝构造函数暂存this所指向的值
	_real++;
	_image++;
	return tmp;
}

Complex& Complex::operator--()   //前置--
{
	_real--;
	_image--;
	return *this;
}

Complex Complex::operator--(int) //后置--
{
	Complex tmp(*this);
	_real--;
	_image--;
	return tmp;
}

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

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

    测试代码:test.cpp
            
#define _CRT_SECURE_NO_WARNINGS 1
#include"Complex.h"

void test1()
{
	Complex c1(1.0,2.0);
	Complex c2(3.0,6.0);
	Complex c3=c1+c2;
	Complex c4=c1-c2;
	Complex c5=c1*c2;
	Complex c6=c1/c2;
	cout<<"c1:";
	c1.Print();
	cout<<"c2:";
	c2.Print();
	cout<<"c1+c2:";
	c3.Print();
	cout<<"c1-c2:";
	c4.Print();
	cout<<"c1*c2:";
	c5.Print();
	cout<<"c1/c2:";
	c6.Print();
}
void test2()
{
	Complex c1(1.0,2.0);
	Complex c2(3.0,6.0);
	cout<<"c1:";
	c1.Print();
	cout<<"c2:";
	c2.Print();
	//Complex c3=c1+=c2;
	//cout<<"c1+=c2:";
	//c1.Print();

	//Complex c3=c1-=c2;
	//cout<<"c1-=c2:";
	//c3.Print();
	//c1.Print();

	//Complex c3=c1*=c2;
	//cout<<"c1*=c2:";
	//c3.Print();
	//c1.Print();
	Complex c3=c1/=c2;
	cout<<"c1/=c2:";
	c3.Print();
}
void test3()
{
	Complex c1(1.0,2.0);
	Complex c2(3.0,6.0);
	Complex c3(c1);

	//c1.Print();  //1+2*i
	//Complex c4=++c1;  
	//c4.Print();  //2+3*i
	//c1.Print();  //2+3*i

	//c1.Print();  //1+2*i
	//Complex c5=c1++;
	//c5.Print();  //1+2*i
	//c1.Print();  //2+3*i

	//c2.Print();  //3+6*i
	//Complex c4=--c2;
	//c4.Print();  //2+5*i
	//c2.Print();  //2+5*i

	c2.Print();  //3+6*i
	Complex c4=c2--;
	c4.Print();  //3+6*i
	c2.Print();  //2+5*i
}
void test4()
{
	Complex c1(1.0,2.0);
	Complex c2(3.0,6.0);
	Complex c3(c1);

	//cout<<(c1 == c3)<<endl;    //1   ==
	//cout<<(c1 == c2)<<endl;    //0   ==
	//cout<<(!(c1 == c2))<<endl; //1   !=

	//cout<<(c2 > c1)<<endl;     //1   >
	//cout<<(!(c2 > c1))<<endl;  //0   <=

	c3.Print();  //1+2*i
	c3=c2;
	c3.Print();  //3+6*i
}
int main()
{
	//test1();
	//test2();
	test3();
	//test4();
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值