Solved
Adding/Substracting Two Complex Numbers
Posted on 2006-04-23
Hello,
I am trying to add/subtract/multiply two complex numbers and also convert a complex number to its polar form. I have gotten the math down. I know that a complex number has the format:
realPart +imaginaryPart * j
where j=sqrt-1.
So far I have gotten the follwing code.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
public __gc class complex {
public:
complex (double, double); //overloaded constructor
void add (complex *, complex *);
void print ();
private:
double rP; // real part
double iP; // imaginary part
double c1,c2;
};
complex::complex (double r, double i)
{
rP = r;
iP = i;
}
/*
complex::complex () {
rP = 0;
iP = 0;
}
*/
void complex::add (complex * c1, complex * c2)
{
rP = c1->rP + c2->rP;
iP = c1->iP + c2->iP;
}
void complex::print ()
{
Console::Write (S" {0} + {1} j", rP.ToString (),iP.ToString ());
}
int _tmain()
{
Console::WriteLine(S" Enter the Real Part of the 1st Number: ");
double Real1 = double::Parse(Console::ReadLine());
Console::WriteLine(S" Enter the Real Part of the 2nd Number: ");
double Real2 = double::Parse(Console::ReadLine());
Console::WriteLine(S"Enter the Imaginary Part of the 1st Number: ");
double Complex1 = double::Parse(Console::ReadLine());
Console::WriteLine(S"Enter the Imaginary Part of the 2nd Number:");
double Complex2 = double::Parse(Console::ReadLine());
complex *c1 = new complex (Real1,Complex1);
complex *c2 = new complex (Real2,Complex2);
complex *c3 = new complex (0,0);
c3->add (c1, c2);
Console::Write (S"The sum of ");
c1->print ();
Console::Write (S" and ");
c2->print ();
Console::Write (S" is ");
c3->print ();
Console::WriteLine (S" ");
return 0;
}
This is how far I have gotten with the addition part of the problem. I will have to add more code for the subtraction, multiplication and polar conversion part of the problem.
But, at the moment, I am getting two errors. At first, I had used int32:Parse in the following code,
double Real1 = double::Parse(Console::ReadLine());
instead of double and the program worked fine. But, I wanted the program to also calculate type double numbers just in case if they were entered. Since, I have replaced Int32 with double, I get the following two errors:
1) error C2039: 'Parse' : is not a member of 'operator``global namespace'''
2) error C2062: type 'double' unexpected
Your help and input is very much appreciated.
Thanks