C++实现的复数类Complex

使用C++实现的复数类计算:

//Coded by ZZ
//#define COMPLEX_H
#if !defined(COMPLEX_H)
#define COMPLEX_H
#endif

#include 
#include 

class Complex
{
public:
	double real;                                                        
	double imag;
public:
	Complex();
	Complex(double _real,double _imag = 0.0):real(_real),imag(_imag){} //构造函数,初始化列表和默认参数
	void SetReal(double _real);                                        //更改实部的值
	void SetImag(double _imag);                                        //更改虚部的值
	void SetVal(double _real,double _imag);                            //更改整个复数
	double GetModule();                                                //获得模值
	void Conj();                                                       //设置共轭

	void ej(double Value);                                             //ej设置数值
	void ej(double Value1,double Value2);                              //ej设置数值
	double GetReal() const;                                            //获取实部,常成员函数
    double GetImag() const;                                            //获取虚部,常成员函数

	Complex& operator=(const Complex &val);                           //成员操作符=
	Complex& operator+=(const Complex &val);                           //成员操作符+=
	Complex& operator*=(const Complex &val);                           //成员操作符-=
	Complex& operator*=(const double Val);                             //成员操作符*=double
	Complex& operator/=(const int N);                                  //成员操作符*=double
};  



Complex operator+(const Complex &lhs, const Complex &rhs);             //实现两个复数+操作
Complex operator*(const Complex &lhs, const Complex &rhs);             //实现两个复数*操作
Complex operator*(const Complex &lhs, const double rhs);               //实现复数与实数*操作






#include "StdAfx.h"
#include "math.h"
#include "Complex.h"

Complex::Complex ()
{
	real=0;
	imag=0;
}

void Complex::SetReal(double _real)
{
    real = _real;
}

void Complex::SetImag(double _imag)
{
    imag = _imag;
}

void Complex::SetVal(double _real,double _imag)
{
    real = _real;
    imag = _imag;
}

void Complex::ej(double Value) 
{
	real=cos(Value);
	imag=sin(Value);
}

void Complex::ej(double Value1,double Value2) 
{
	real=cos(Value1+Value2);
	imag=sin(Value1+Value2);
}

double Complex::GetReal() const 
{
    return real;
}

double Complex::GetImag() const
{
    return imag;
}


double Complex::GetModule ()
{
	return sqrt(real*real+imag*imag);
}


Complex& Complex::operator=(const Complex &val)
{
    real = val.real;
    imag = val.imag;
    return *this;
}



Complex& Complex::operator+=(const Complex &val)
{
    real += val.real;
    imag += val.imag;
    return *this;
}


Complex& Complex::operator*=(const Complex &val)
{
    double tReal = real;
    double tImag = imag;
    real = tReal*val.real - tImag*val.imag;
    imag = tReal*val.imag + tImag*val.real;
    return *this;
}


Complex& Complex::operator*=(const double Val)
{
    double tReal = real;
    double tImag = imag;
    real = tReal*Val;
    imag = tImag*Val;
    return *this;
}


Complex& Complex::operator/=(const int N)
{
    double tReal = real;
    double tImag = imag;
    real = tReal/N;
    imag = tImag/N;
    return *this;
}



Complex operator+(const Complex &lhs,const Complex &rhs)
{
    Complex ret(lhs);
    ret += rhs;
    return ret;
}

Complex operator*(const Complex &lhs,const Complex &rhs)
{
    Complex ret(lhs);
    ret *= rhs;
    return ret;
}
Complex operator*(const Complex &lhs,const double rhs)
{
    Complex ret(lhs);
    ret *= rhs;
    return ret;
}

void Complex::Conj() 
{
	imag=-imag;
}

 

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