# Die Implementierung der Rechenfunktionen für Komplexe Zahlen

Um in C++ mit komplexenZahlen operieren zu koennen war es notwendig folgende Klasse zu schaffen:

```// mathplus.h : header file
//

#include "math.h"

/////////////////////////////////////////////////////////////////////////////
// class CComplex

class CComplex
{       double getphi();
public:
double r,i;
CComplex(double a=0.0,double b=0.0);
CComplex(CComplex& c);
CComplex operator ! ();                         //changes real and imag part of the number
CComplex operator ~ ();                         //liefert die konjugiert komplexe Nummer
CComplex operator + ();
CComplex operator - ();
CComplex operator * (CComplex& b);
CComplex operator / (CComplex& b);
CComplex operator + (CComplex& b);
CComplex operator - (CComplex& b);
CComplex operator * (double b);
CComplex operator / (double b);
CComplex operator + (double b);
CComplex operator - (double b);
friend CComplex operator * (double a, CComplex& b);
friend CComplex operator / (double a, CComplex& b);
friend CComplex operator + (double a, CComplex& b);
friend CComplex operator - (double a, CComplex& b);
CComplex operator *= (CComplex& b);
CComplex operator /= (CComplex& b);
CComplex operator += (CComplex& b);
CComplex operator -= (CComplex& b);
CComplex operator *= (double b);
CComplex operator /= (double b);
CComplex operator += (double b);
CComplex operator -= (double b);
friend double cabs (CComplex& c);
friend CComplex cpow (CComplex& c,int n);
friend CComplex croot (CComplex& c,int n, int number=0);
// es wird die Loesung mit der nummer number (number

Der Body

// mathplus.cpp : implementation file
//

#include "stdafx.h"
#include "mathplus.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// class CComplex

double CComplex::getphi()
{       return atan(i/r);
}

CComplex::CComplex(double a,double b)  // default: a=0 b=0!
{       r=a;
i=b;
}

CComplex::CComplex(CComplex& c)
{       *this=c;
}

CComplex CComplex::operator ! ()
{       CComplex c;
c.r=i;
c.i=r;
return c;
}

CComplex CComplex::operator ~ ()
{       CComplex c;
c.r=r;
c.i=-i;
return c;
}

CComplex CComplex::operator + ()
{       return *this;
}

CComplex CComplex::operator - ()
{       CComplex c;
c.r=-r;
c.i=-i;
return c;
}

CComplex CComplex::operator * (CComplex& b)
{       CComplex c;
c.r=r*b.r-i*b.i;
c.i=i*b.r+r*b.i;
return c;
}

CComplex CComplex::operator / (CComplex& b)
{       CComplex c;
c=~b;
c=*this*c;
c.r/=(b.r*b.r+b.i*b.i);
c.i/=(b.r*b.r+b.i*b.i);
return c;
}

CComplex CComplex::operator + (CComplex& b)
{       CComplex c;
c.r=r+b.r;
c.i=i+b.i;
return c;
}

CComplex CComplex::operator - (CComplex& b)
{       CComplex c;
c.r=r-b.r;
c.i=i-b.i;
return c;
}

CComplex CComplex::operator * (double b)
{       CComplex c;
c=b;
c=*this*c;
return c;
}

CComplex CComplex::operator / (double b)
{       CComplex c;
c=b;
c=*this/c;
return c;
}

CComplex CComplex::operator + (double b)
{       CComplex c;
c=b;
c=*this+c;
return c;
}

CComplex CComplex::operator - (double b)
{       CComplex c;
c=b;
c=*this-c;
return c;
}

CComplex operator * (double a, CComplex& b)
{       CComplex c;
c=a;
c=c*b;
return c;
}

CComplex operator / (double a, CComplex& b)
{       CComplex c;
c=a;
c=c/b;
return c;
}

CComplex operator + (double a, CComplex& b)
{       CComplex c;
c=a;
c=c+b;
return c;
}

CComplex operator - (double a, CComplex& b)
{       CComplex c;
c=a;
c=c-b;
return c;
}

CComplex CComplex::operator *= (CComplex& b)
{       r=r*b.r-i*b.i;
i=i*b.r+r*b.i;
return *this;
}

CComplex CComplex::operator /= (CComplex& b)
{       CComplex c;
c=~b;
*this*=c;
r/=(b.r*b.r+b.i*b.i);
i/=(b.r*b.r+b.i*b.i);
return *this;
}

CComplex CComplex::operator -= (CComplex& b)
{       r=r-b.r;
i=i-b.i;
return *this;
}

CComplex CComplex::operator *= (double b)
{       CComplex c;
c=b;
*this*=c;
return *this;
}

CComplex CComplex::operator /= (double b)
{       CComplex c;
c=b;
*this/=c;
return *this;
}

CComplex CComplex::operator += (double b)
{       CComplex c;
c=b;
*this+=c;
return c;
}

CComplex CComplex::operator -= (double b)
{       CComplex c;
c=b;
*this-=c;
return c;
}

double cabs (CComplex& c)
{       return sqrt(c.r*c.r+c.i*c.i);
}

CComplex cpow (CComplex& c,int n)
{       CComplex a;
double r=pow(cabs(c),n);
double phi=n*c.getphi();
a.r=r*cos(phi);
a.i=r*sin(phi);
return a;
}

CComplex croot (CComplex& c,int n, int number)
{       CComplex a;
double r=pow(cabs(c),1.0/n);
double phi=(c.getphi()*number*2*PI)/n;
a.r=r*cos(phi);
a.i=r*sin(phi);
return a;
}

CComplex csqrt (CComplex& c, int number)
{       CComplex a;
a=croot(c,2,number);
return a;
}

/////////////////////////////////////////////////////////////////////////////
//      mathematical functions

double abs(double wert)
{       double retval;
(wert<0) ? (retval=-wert) : (retval=wert);
return retval;
}

Index

```