------------------------------------------------------------------------------------------------------
/*
**复数x被定义为二元有序实数对(a,b),记为z=a+bi,这里a和b是实数,i是虚数单位。
**在复数a+bi中,a=Re(z)称为实部,b=Im(z)称为虚部。当虚部等于零时,这个复数可以视为实数;
**当z的虚部不等于零时,实部等于零时,常称z为纯虚数。
**
**复数的四则运算规定为:
**加法法则:(a+bi)+(c+di)=(a+c)+(b+d)i;
**减法法则:(a+bi)-(c+di)=(a-c)+(b-d)i;
**乘法法则:(a+bi)·(c+di)=(ac-bd)+(bc+ad)i;
**除法法则:(a+bi)÷(c+di)=[(ac+bd)/(c2+d2)]+[(bc-ad)/(c2+d2)]i.
**
**当复数的实部和虚部都相等时,两个复数相等
**只有当复数的虚部等于零的时候两个复数才可以比较大小
**
*/
------------------------------------------------------------------------------------------------------
C++代码:
-------------------------------------------头文件-----------------------------------------------------
# ifndef __COMPLEX_H__
# define __COMPLEX_H__
# define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <stdlib.h>
using namespace std;
//声明复数类
class Complex
{
public:
void Complex::Print();
public:
Complex(double real, double p_w_picpath);
Complex(const Complex & Z);
~Complex();
bool Complex::operator< (const Complex & Z);
bool Complex::operator> (const Complex & Z);
bool Complex::operator== (const Complex & Z);
public:
Complex ComplexAdd(const Complex & Z);
Complex ComplexSub(const Complex & Z);
Complex ComplexMul(const Complex & Z);
Complex ComplexDiv(const Complex & Z);
private:
double _real;
double _p_w_picpath;
};
# endif //__COMPLEX_H__
----------------------------------------------函数----------------------------------------------------
# include "Complex.h"
//打印函数
void Complex::Print()
{
if (!this->_p_w_picpath)
{
if (!this->_real)
{
cout << 0 << endl;
}
else
cout << this->_real << endl;
}
else if (!this->_real)
{
cout << this->_p_w_picpath << 'i' << endl;
}
else
{
if (this->_p_w_picpath < 0)
{
cout << this->_real << this->_p_w_picpath << 'i' << endl;
}
else
cout << this->_real << '+' << this->_p_w_picpath << 'i' << endl;
}
}
//构造函数
Complex::Complex(double real, double p_w_picpath)
{
_real = real;
_p_w_picpath = p_w_picpath;
}
//拷贝构造函数
Complex::Complex(const Complex & Z)
{
_real = Z._real;
_p_w_picpath = Z._p_w_picpath;
}
//析构函数
Complex::~Complex()
{
//这里的析构函数不需要做任何操作
}
//操作符重载
/*小于*/
bool Complex::operator< (const Complex & Z)
{
if (!this->_p_w_picpath && !Z._p_w_picpath)
{
if (this->_real < Z._real)
{
return true;
}
}
return false;
}
/*大于*/
bool Complex::operator> (const Complex & Z)
{
if (!this->_p_w_picpath && !Z._p_w_picpath)
{
if (this->_real > Z._real)
{
return true;
}
}
return false;
}
/*等于*/
bool Complex::operator== (const Complex & Z)
{
if (!this->_p_w_picpath && !Z._p_w_picpath)
{
if (this->_real == Z._real)
{
return true;
}
}
else if (this->_p_w_picpath == Z._p_w_picpath)
{
if (this->_real == Z._real)
{
return true;
}
}
return false;
}
//四则运算
/*加法*/
Complex Complex::ComplexAdd(const Complex & Z)
{
Complex tmp(*this);
tmp._real += Z._real;
tmp._p_w_picpath += Z._p_w_picpath;
return tmp;
}
/*减法*/
Complex Complex::ComplexSub(const Complex & Z)
{
Complex tmp(*this);
tmp._real -= Z._real;
tmp._p_w_picpath -= Z._p_w_picpath;
return tmp;
}
/*乘法*/
Complex Complex::ComplexMul(const Complex & Z)
{
Complex tmp(*this);
tmp._real = (this->_real * Z._real) - (this->_p_w_picpath * Z._p_w_picpath);
tmp._p_w_picpath = (this->_p_w_picpath * Z._real) + (this->_real * Z._p_w_picpath);
return tmp;
}
/*除法*/
Complex Complex::ComplexDiv(const Complex & Z)
{
Complex tmp(*this);
tmp._real = ((this->_real * Z._real) + (this->_p_w_picpath * Z._p_w_picpath))
/ ((Z._real * Z._real) + (Z._p_w_picpath * Z._p_w_picpath));
tmp._p_w_picpath = ((this->_p_w_picpath * Z._real) - (this->_real * Z._p_w_picpath))
/ ((Z._real * Z._real) + (Z._p_w_picpath * Z._p_w_picpath));
return tmp;
}
------------------------------------------ 测试用例 --------------------------------------------------
# include "Complex.h"
//测试四则运算
void Test4()
{
/*测试加法*/
/*Complex Z1(1, 2);
Complex Z2(1, 2);
Complex ret = Z1.ComplexAdd(Z2);
ret.Print();*/
/*测试减法*/
/*Complex Z1(-1, 2);
Complex Z2(1, 1);
Complex ret = Z1.ComplexSub(Z2);
ret.Print();*/
/*测试乘法*/
/*Complex Z1(1, -2);
Complex Z2(1, 2);
Complex ret = Z1.CompleMul(Z2);
ret.Print();*/
/*测试除法*/
Complex Z1(1, 2);
Complex Z2(1, 1);
Complex ret = Z1.ComplexDiv(Z2);
ret.Print();*/
}
//测试操作符重载
void Test3()
{
bool RET;
/*测试“<”*/
//Complex Z1(1, 4);
//Complex Z2(1, 4);
//RET = Z1 < Z2;
//cout << RET << endl;
//Complex Z3(1, 0);
//Complex Z4(2, 0);
//RET = Z3 < Z4;
//cout << RET << endl;
/*测试“>”*/
/*Complex Z1(1, 0);
Complex Z2(2, 0);
RET = Z1 > Z2;
cout << RET << endl;
Complex Z3(3, 0);
Complex Z4(2, 0);
RET = Z3 > Z4;
cout << RET << endl;*/
/*测试“==”*/
Complex Z1(1, 4);
Complex Z2(1, 4);
RET = Z1 == Z2;
cout << RET << endl;
Complex Z3(1, 1);
Complex Z4(1, 3);
RET = Z3 == Z4;
cout << RET << endl;
Complex Z5(1, 0);
Complex Z6(1, 0);
RET = Z5 == Z6;
cout << RET << endl;
}
//测试拷贝构造函数
void Test2()
{
Complex Z1(1, 3);
Z1.Print();
Complex Z2(Z1);
Z2.Print();
}
//测试构造函数
void Test1()
{
Complex Z1(1,3);
Z1.Print();
}
int main()
{
//Test1();
//Test2();
//Test3();
Test4();
system("pause");
return 0;
}
-----------------------------------------------------------------------------------------------------
C++中的空类,默认产生六个默认成员函数,分别是:构造函数,拷贝(赋值)构造函数,析构函数,赋值操作符重载,取地址操作符重载,const修饰的取地址操作符重载。
所谓默认的成员函数,是指,若程序员没有创建该函数,系统则会自动创建。
------------------------------------------------------------------------------------------------------
转载于:https://blog.51cto.com/814193594/1733928