复数类的功能及要求
复数对于从小学习数学的我们并不陌生,其操作性相对比较简单。复数类主要考察的是我们对运算符重载的熟练程度,主要有四则运算的+、-、×、/,以及+=、-=、>、<、==、!=前置++,后置++的运算符重载。复数类共有两个参数,实部(_real)与虚部(_imag),因此需要定义两个成员变量,具体的实现思路如下。
复数类的设计思路
设计思路主要针对复数的四则运算展开:即+、-、×、/。复数的加法+、减法-遵守的规则为实部-实部,虚部-虚部,乘法的运算规则为(a+bi)(c+di)=(ac-bd)+(bc+ad)i ,除法较为复杂,运算规则为:(a+bi)/(c+di),化简为:(a+bi)(c-di)/(c+di)(c-di) = ((ac+bd)+(bc-ad)i)/(c2+d2)。除此之外的运算符重载比较简单,具体实现过程如下。
参考代码
complex.h
#include <iostream>
#include <Windows.h>
using namespace std
;
class Complex
{
public
:
Complex(double real
,double imag
)
:_real(real
)
,_imag(imag
)
{}
Complex(const Complex
& t
)
{
_real
= t
._real
;
_imag
= t
._imag
;
}
~Complex()
{}
Complex
& operator
=(const Complex
& t
);
bool operator
==(const Complex
& t
);
bool operator
>(const Complex
& t
);
bool operator
<(const Complex
& t
);
bool operator
!=(const Complex
& t
);
bool operator
>=(const Complex
& t
);
bool operator
<=(const Complex
& t
);
Complex operator
++();
Complex operator
++(int);
Complex
& operator
+=(const Complex
& t
);
Complex
& operator
-=(const Complex
& t
);
Complex operator
+(const Complex
& t
);
Complex operator
-(const Complex
& t
);
Complex operator
*(const Complex
& t
);
Complex operator
/(const Complex
& t
);
friend ostream
& operator
<<(ostream
& _cout
,const Complex
& c
);
private
:
double _real
;
double _imag
;
};
main.cpp
#include "complex.h"
void FunTest()
{
Complex
c1(1,2);
Complex
c2(3,4);
Complex c3
= c1
*c2
;
Complex c4
= ++c1
;
Complex c5
= c2
++;
Complex c6
= c2
/c1
;
cout
<<c3
<<endl
;
cout
<<c4
<<endl
;
cout
<<c5
<<endl
;
cout
<<c6
<<endl
;
}
int main()
{
FunTest();
system("pause");
return 0;
}
complex.cpp
#include "complex.h"
Complex
& Complex
::operator
=(const Complex
& t
)
{
if(this
!= &t
)
{
_real
= t
._real
;
_imag
= t
._imag
;
}
return *this
;
}
bool Complex
::operator
==(const Complex
& t
)
{
if(_imag
!= 0 || t
._imag
!= 0)
{
cout
<<"虚部不为0,无法比较!"<<endl
;
return false
;
}
return (_real
== t
._real
)&&(_imag
== t
._imag
);
}
bool Complex
::operator
!=(const Complex
& t
)
{
return !(*this
== t
);
}
bool Complex
::operator
>(const Complex
& t
)
{
if(_imag
!= 0 || t
._imag
!= 0)
{
cout
<<"虚部不为0,无法比较大小!"<<endl
;
return false
;
}
else
{
if(_real
> t
._real
)
return true
;
return false
;
}
}
bool Complex
::operator
<(const Complex
& t
)
{
if(_imag
!= 0 || t
._imag
!= 0)
{
cout
<<"虚部不为0,无法比较大小!"<<endl
;
return false
;
}
return !(*this
>= t
);
}
bool Complex
::operator
>=(const Complex
& t
)
{
if(_imag
!= 0 || t
._imag
!= 0)
{
cout
<<"虚部不为0,无法比较大小!"<<endl
;
return false
;
}
return (*this
== t
)||(*this
> t
);
}
bool Complex
::operator
<=(const Complex
& t
)
{
if(_imag
!= 0 || t
._imag
!= 0)
{
cout
<<"虚部不为0,无法比较大小!"<<endl
;
return false
;
}
return (*this
== t
)||(*this
< t
);
}
Complex
& Complex
::operator
+=(const Complex
& t
)
{
_real
+= t
._real
;
_imag
+= t
._imag
;
return *this
;
}
Complex
& Complex
::operator
-=(const Complex
& t
)
{
_real
-= t
._real
;
_imag
-= t
._imag
;
return *this
;
}
Complex Complex
::operator
+(const Complex
& t
)
{
Complex
temp(*this
);
temp
._real
+= t
._real
;
temp
._imag
+= t
._imag
;
return temp
;
}
Complex Complex
::operator
-(const Complex
& t
)
{
Complex
temp(*this
);
temp
._real
-= t
._real
;
temp
._imag
-= t
._imag
;
return temp
;
}
Complex Complex
::operator
*(const Complex
& t
)
{
Complex
temp(*this
);
temp
._real
= this
->_real
*t
._real
- this
->_imag
*t
._imag
;
temp
._imag
= this
->_imag
*t
._real
+ this
->_real
*t
._imag
;
return temp
;
}
Complex Complex
::operator
/(const Complex
& t
)
{
Complex
temp(*this
);
temp
._real
= (this
->_real
*t
._real
+this
->_imag
*t
._imag
)/(t
._real
*t
._real
+t
._imag
*t
._imag
)\
+(this
->_imag
*t
._real
- this
->_real
*t
._imag
)/((t
._real
*t
._real
+t
._imag
*t
._imag
));
return temp
;
}
Complex Complex
::operator
++()
{
++_real
;
++_imag
;
return *this
;
}
Complex Complex
::operator
++(int)
{
Complex
temp(*this
);
_real
++;
_imag
++;
return temp
;
}
ostream
& operator
<<(ostream
& _cout
,const Complex
& t
)
{
_cout
<<t
._real
<<"+"<<t
._imag
<<"i"<<endl
;
return _cout
;
}