C++实现复数类,并用其编写一个Mandelbrot程序

我们把形如z=a+bi(a,b均为实数)的数称为复数,其中a称为实部,b称为虚部,i称为虚数单位。当z的虚部等于零时,常称z为实数;当z的虚部不等于零时,实部等于零时,常称z为纯虚数。

用C++实现了自定义的复数类,重载了四则运算和+=、-=、*=,/=,重载了==判断相等,输入>> 、输出<<。用(~)这个符号求共轭复数。

头部文件:

#pragma once
#include 
#include 
using namespace std;
namespace ComplexNumber {
        class Complex
        {
                friend ostream& operator<<(ostream&, const Complex&);
                friend istream& operator >> (istream&,Complex&);
                friend bool operator==(const Complex&, const Complex&);
                friend Complex operator+(double a, Complex z) { return Complex(a + z.Rez(), z.Imz()); }
                friend Complex operator*(double a, Complex z) { return Complex(a*z.Rez(), a*z.Imz()); }
        public:
                Complex();
                Complex(double, double);
                double Abs() { return sqrt(real*real + imag*imag); }
                double Rez() { return real; }
                double Imz() { return imag; }
                bool PureIm() { return real == 0 ? true : false; }

                Complex operator+(const Complex&);
                Complex operator-(const Complex&);
                Complex operator*(const Complex&);
                Complex operator/(const Complex&);
                Complex& operator=(const Complex&);
                Complex& operator+= (const Complex&);
                Complex& operator-= (const Complex&);
                Complex& operator*= (const Complex&);
                Complex& operator/= (const Complex&);
                Complex operator~ () { return Complex(real, -imag); }

        private:
                double real;
                double imag;
                Complex multiply(const Complex, const Complex);
                Complex divide(const Complex, const Complex);
        };


}

 成员函数定义:

#include "Complex.h"
using namespace std;
using namespace ComplexNumber;
Complex::Complex():real(0),imag(0)
{
}

Complex::Complex(double Re, double Im):real(Re),imag(Im)
{
}

Complex Complex::operator+(const Complex& cplx)
{
        Complex res;
        res.real = this->real + cplx.real;
        res.imag = this->imag + cplx.imag;
        return res;
}

Complex Complex::operator-(const Complex& cplx)
{
        Complex res;
        res.real = this->real - cplx.real;
        res.imag = this->imag - cplx.imag;
        return res;
}

Complex Complex::operator*(const Complex& cplx)
{
        return multiply(*this,cplx);
}
Complex Complex::operator/(const Complex &cplx)
{
        return divide(*this,cplx);
}
Complex& Complex::operator=(const Complex& cplx)
{
        real = cplx.real;
        imag = cplx.imag;
        return *this;
}

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

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

Complex& Complex::operator*= (const Complex& cplx)
{
        *this = multiply(*this, cplx);
        return *this;
}

Complex& Complex::operator/= (const Complex& cplx)
{
        *this = divide(*this, cplx);
        return *this;
}

bool ComplexNumber::operator==(const Complex & c1, const Complex & c2)
{
        if (c1.real == c2.real&&c1.imag == c2.imag)return true;
        else return false;
}

Complex Complex::multiply(const Complex c1, const Complex c2)
{
        Complex res;
        res.real = c1.real*c2.real - c1.imag*c2.imag;
        res.imag = c1.real*c2.imag + c1.imag*c2.real;
        return res;

}

Complex Complex::divide(const Complex c1, const Complex c2)
{
        Complex res;
        res.real = (((c1.real*c2.real) + (c1.imag*c2.imag)) / (c2.real*c2.real + c2.imag*c2.imag));
        res.imag = (((c1.imag*c2.real) - (c1.real*c2.imag)) / (c2.real*c2.real + c2.imag*c2.imag));
        return res;
}

ostream & ComplexNumber::operator<<(ostream& os, const Complex& cplx)
{
        if (cplx.imag== 0.0) {
                os << cplx.real;
        }
        else
        {
                if (cplx.real == 0.0) {
                        os << cplx.imag << "i";
                }
                else {
                        if (cplx.imag < 0) {
                                os << cplx.real << cplx.imag << "i";
                        }
                        else {
                                os << cplx.real << "+" << cplx.imag << "i";
                        }

                }
        }
        return os;
}


istream& ComplexNumber::operator >> (istream& is, Complex& cplx)
{
        is >> cplx.real >> cplx.imag;
        return is;
}

调用程序片段:

    static Complex i(0, 1);
	Complex c = 1.4 + 3.1*i;
	Complex	conj = ~c;//求共轭复数
	cout << conj << endl;
	cout << conj*c << endl;

 

结果:

1.4-3.1i
11.57

Mandelbrot算法程序:

#include 
#include 
#include "Complex.h"
#define WIDTH 600//图片的宽度(像素)
#define HEIGHT 600//图片的高度
using namespace std;
using namespace ComplexNumber;
int Mandelbrot(Complex);
int main(){
        ofstream ofs("mandel.dat",ios::out);
        Complex i(0,1.0);
        Complex c;
        for (int k=0; k < WIDTH; k++) {
                for (int j=0; j < HEIGHT; j++) {
                        double a = ((double)k / (double)WIDTH - 0.5) * 4;
                        double b = ((double)j / (double)HEIGHT - 0.5) * 4;
                        //将图片的宽度和高度分别转换到区间(-2.0,2.0)
                        c = a + b*i;
                        int m=Mandelbrot(c);
                        ofs<< k << ',' << j << ',' << m << endl;//输出到文件
                }
        }
}

int Mandelbrot(Complex c) {
        int i = 0;
        Complex z;
        for (i; i < 255; i++) {
                if (z.Abs() > 2)break;
                z = z*z + c;
        }
        return i;
}

用生成的数据文件绘出图形,如下: 

 

C++实现复数类,并用其编写一个Mandelbrot程序_第1张图片

你可能感兴趣的:(C++实现复数类,并用其编写一个Mandelbrot程序)