Category : Printer + Display Graphics
Archive   : BMRPH102.ZIP
Filename : COMPLEX.CPP

 
Output of file : COMPLEX.CPP contained in archive : BMRPH102.ZIP
// Module: Complex
// Version: 2.00 28-Oct-1989
//
// Language: C++ 2.0
// Environ: Any
// Compilers: Zortech C++ 2.01
//
// Purpose: Provides the class "Complex" for C++ programs. The majority
// of the class is implemented inline for efficiency. Only
// the division, power, and i/o methods are actual functions.
//
// Written by: Scott Robert Ladd
// 705 West Virginia
// Gunnison CO 81230
//
// BBS (303)641-6438
// FidoNet 1:104/708

#include "math.h"
#include "stdlib.h"

#include "Complex.hpp"
#include "stream.hpp"

static void DefaultHandler();

void (* Complex::ErrorHandler)() = DefaultHandler;

static void DefaultHandler()
{
cout << "\aERROR in complex object: DIVIDE BY ZERO\n";
exit(1);
}

// division methods

Complex operator / (const Complex & C1, const Complex & C2)
{
Complex Result;
double Den;

Den = norm(C2);

if (Den != 0.0)
{
Result.Real = (C1.Real * C2.Real + C1.Imag * C2.Imag) / Den;
Result.Imag = (C1.Imag * C2.Real - C1.Real * C2.Imag) / Den;
}
else
Complex::ErrorHandler();

return Result;
}

Complex Complex::operator /= (const Complex & C)
{
double Den, OldReal;

Den = norm(C);

if (Den != 0.0)
{
OldReal = Real;
Real = (Real * C.Real + Imag * C.Imag) / Den;
Imag = (Imag * C.Real - OldReal * C.Imag) / Den;
}
else
Complex::ErrorHandler();

return *this;
}

// "power" methods
Complex pow(const Complex & C, const Complex & Power)
{
Complex Result;

if (Power.Real == 0.0 && Power.Imag == 0.0)
{
Result.Real = 1.0;
Result.Imag = 0.0;
}
else
{
if (C.Real != 0.0 || C.Imag != 0.0)
Result = exp(log(C) * Power);
else
Complex::ErrorHandler();
}

return Result;
}

Complex sqrt(const Complex & C)
{
Complex Result;
double r, i, ratio, w;

if (C.Real != 0.0 || C.Imag != 0.0)
{
r = C.Real < 0.0 ? -C.Real : C.Real;
i = C.Imag < 0.0 ? -C.Imag : C.Imag;

if (r > i)
{
ratio = i / r;
w = sqrt(r) * sqrt(0.5 * (1.0 + sqrt(1.0 + ratio * ratio)));
}
else
{
ratio = r / i;
w = sqrt(i) * sqrt(0.5 * (ratio + sqrt(1.0 + ratio * ratio)));
}

if (C.Real > 0)
{
Result.Real = w;
Result.Imag = C.Imag / (2.0 * w);
}
else
{
Result.Imag = (C.Imag > 0.0) ? w : - w;
Result.Real = C.Imag / (2.0 * Result.Imag);
}
}
else
Complex::ErrorHandler();

return Result;
}

// output method
ostream & operator << (ostream & Output, const Complex & C)
{
Output << form("(%+1g%+1gi)",C.Real,C.Imag);

return Output;
}

istream & operator >> (istream & Input, Complex & C)
{
char Ch;

C.Real = 0.0;
C.Imag = 0.0;

Input >> Ch;

if (Ch == '(')
{
Input >> C.Real >> Ch;

if (Ch == ',')
Input >> C.Imag >> Ch;

if (Ch != ')')
Input.clear(_bad);
}
else
{
Input.putback(Ch);
Input >> C.Real;
}

return Input;
}


  3 Responses to “Category : Printer + Display Graphics
Archive   : BMRPH102.ZIP
Filename : COMPLEX.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/