// Complex class implementation file

#include <stdafx.h>

#include <math.h>
#include <iostream.h>
#include <stdlib.h>

#include "complex.h"

// constructors

CComplex::CComplex(void)
{
	real = 0;
	imag = 0;
}

CComplex::CComplex(double re)
{
	real = re;
	imag = 0;
}

CComplex::CComplex(double re, double im)
{
	real = re;
	imag = im;
}

// destructor

CComplex::~CComplex()
{
	// nothing to do!
}

// accessor functions

double CComplex::realpart(void)
{
	return real;
}

double CComplex::imagpart(void)
{
	return imag;
}

// set function

void CComplex::set(double re, double im)
{
	real = re;
	imag = im;
}

// complex functions

double CComplex::mag(void)
{
	return sqrt(real*real + imag*imag);
}

double CComplex::magsqrd(void)
{
	return real*real + imag*imag;
}

// overloaded operators

ostream& operator <<(ostream& out, CComplex& comp)
{	
	if(comp.real >= 0)
		out << ' ' << comp.real;
	else
		out << comp.real;

	if(comp.imag >= 0)
		out << " + " << comp.imag;
	else
		out << " - " << fabs(comp.imag);
	out << "i ";
		return out;
}

CComplex operator *(CComplex& c1, CComplex& c2)
{	
	return CComplex(c1.real*c2.real-c1.imag*c2.imag,
		c1.real*c2.imag+c1.imag*c2.real);
}

CComplex operator *(CComplex& c1, double scalar)
{	
	return CComplex(scalar * c1.real, scalar * c1.imag);
}

CComplex operator +(CComplex& c1, CComplex& c2)
{	
	return CComplex(c1.real+c2.real, c1.imag+c2.imag);
}

CComplex operator +=(CComplex& c1, CComplex& c2)
{	
	return CComplex(c1.real += c2.real, c1.imag += c2.imag);
}

CComplex operator -=(CComplex& c1, CComplex& c2)
{	
	return CComplex(c1.real -= c2.real, c1.imag -= c2.imag);
}

CComplex operator /=(CComplex& c, double scalar)
{	
	return CComplex(c.real /= scalar, c.imag /= scalar);
}

bool operator ==(CComplex& c1, CComplex& c2)
{	
	return (c1.real == c2.real && c1.imag == c2.imag);
}

