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:
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: ");

Console::WriteLine(S" Enter the Real Part of the 2nd Number: ");

Console::WriteLine(S"Enter the Imaginary Part of the 1st Number: ");

Console::WriteLine(S"Enter the Imaginary Part of the 2nd Number:");

complex *c1 = new complex (Real1,Complex1);
complex *c2 = new complex (Real2,Complex2);
complex *c3 = new complex (0,0);
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,

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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
double is the inbuilt compiler type, its no class, it don't have any Parse routine...
Just use Double, this is the class from mscorelib which support Parse...
0
Author Commented:
ok thanks,
As per the above suggestion, I removed Parse and replaced with the following code:

Is this correct?

After compiling I get the following errors:
error C2440: 'type cast' : cannot convert from 'System::String __gc *' to 'double'

Thank You
0
Commented:
Double of mscorelib does support Parse...
0
Author Commented:
opps I guess its really late. Did not notice the 'd' from 'D'.
Now, I can continue on to the other parts of the problem.
Any suggestions or input to make this program better is very much appreciated.

Thank You
0
Commented:
well, have you looked at the complex implementation of the STL (standard template lib) from c++? (#include <complex>)
this could do most of the work for you...
http://wwwasd.web.cern.ch/wwwasd/lhc++/RW/stdlibcr/com_8038.htm
0
Commented:
Instead of Parse use Convert.ToDouble Method (String).
0
Author Commented:
Can you please clarify on how use the Convert.ToDouble Method?
Thank You
0
Commented:

0
Author Commented:
Thanks guys for all of your input. This is the code I have right now. When dividing the two complex numbers, I want to be sure that when a zero is in the denominator, the code will prompt the user by saying that 0 is invalid in the denominator and the program will exit. I wrote an if statement trying to check that, but it does not work. The code just continues and gives the answer as infinity, if a zero is entered in the denominator.
Code:

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

public __gc class complex {

public:
void add (complex *,  complex *);
void substract (complex *, complex *);
void multiplication (complex *, complex *);
void division (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;
}
void complex::add (complex * c1, complex * c2)
{
rP = c1->rP + c2->rP;
iP = c1->iP + c2->iP;
}
void complex::substract (complex * c1, complex * c2)
{
rP = c1->rP - c2->rP;
iP = c1->iP - c2->iP;
}
void complex::multiplication (complex * c1, complex * c2)
{
rP = c1->rP * c2->rP;
iP = c1->iP * c2->iP;
}
void complex::division (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 ());
}
double _tmain()
{
Console::WriteLine(S" Enter the Real Part of the 1st Number: ");

Console::WriteLine(S" Enter the Real Part of the 2nd Number: ");

Console::WriteLine(S"Enter the Imaginary Part of the 1st Number: ");

Console::WriteLine(S"Enter the Imaginary Part of the 2nd Number:");

Console::WriteLine(S"If adding Enter 1, If Sub enter 2, If Mult Enter 3, If Div Enter 4 ");

complex *c1 = new complex (Real1,Complex1);
complex *c2 = new complex (Real2,Complex2);
complex *c3 = new complex (0,0);

switch (input1)
{
case 1:
Console::Write (S"The sum of ");
c1->print ();
Console::Write (S" and ");
c2->print ();
Console::Write (S" is ");
c3->print ();
Console::WriteLine (S" ");
break;
case 2:
c3->substract (c1,c2);
Console::Write (S"The substraction of ");
c1->print ();
Console::Write (S" and ");
c2->print ();
Console::Write (S" is ");
c3->print ();
Console::WriteLine (S" ");
break;
case 3:
c3->multiplication (c1,c2);
Console::Write (S"The multiplication of ");
c1->print ();
Console::Write (S" and ");
c2->print ();
Console::Write (S" is ");
c3->print ();
Console::WriteLine (S" ");
break;
case 4:
if ( c2 == 0 )
{
Console::WriteLine(S" Number in Denominator cannot be 0");
}
else
c3->division (c1,c2);
Console::Write (S"The division of ");
c1->print ();
Console::Write (S" and ");
c2->print ();
Console::Write (S" is ");
c3->print ();
Console::WriteLine (S" ");
break;

case 5:

default:
Console::WriteLine(S"Enter a Correct Entry");
}

return 0;

}

Any input, help or comments appreciated.
Thank You
0
Commented:
you have to put the part after else in brackets...
if not, only the line c3->division (c1,c2); will be skipped, if c2 == 0 is true

case 4:
if ( c2 == 0 )
{
Console::WriteLine(S" Number in Denominator cannot be 0");
}
else
{
c3->division (c1,c2);
Console::Write (S"The division of ");
c1->print ();
Console::Write (S" and ");
c2->print ();
Console::Write (S" is ");
c3->print ();
Console::WriteLine (S" ");
}
break;
0
Commented:
if ( c2 == 0 )

You need to write operator== for complex class, or write something like IsNull function:

bool IsNull()
{
return ( rP == 0.0  &&  rI == 0.0);
}

if ( c2->IsNull() )
...

Since double numbers cannot be compared exactly, you can use some small value for testing:

bool IsNull()
{
return ( Mat.Abc(rP) < 0.00001  &&  Math.Abs(rI) < 0.000001 );    // define some small e number which is handled as 0
}

Other way is to handle DivideByZeroException in the class or in the client:

try
{
c3->division (c1,c2);
...
}
catch (DivideByZeroException * e)
{
Console::WriteLine("Dividing by zero");
}

I think the last way is the best.
0
Commented:
BTW, multiplication and division math is your responsibility, I am not sure that you do this right :-)
0
Author Commented:
I modified my case 4 (the division case) like this:

case 4:
c3->division (c1,c2);
Console::Write (S"The division of ");
c1->print ();
Console::Write (S" and ");
c2->print ();
Console::Write (S" is ");
c3->print ();
Console::WriteLine (S" ");
catch (DivideByZeroException * e)
{
Console::WriteLine("Dividing by zero");
}
break;

I include the catch command as per your suggestion but I get this error:

error C2318: no try block associated with this catch handler

Thanks

0
Author Commented:
chip3d:

I fixed my mistake of not have the { after the else statement and close it with a }. Thanks. But still my probelm of having a guard against having a non-zero number is not working. For a number divided by zero, my answers still are infinity.

Thanks
0
Commented:
case 4:
try
{
c3->division (c1,c2);
Console::Write (S"The division of ");
c1->print ();
Console::Write (S" and ");
c2->print ();
Console::Write (S" is ");
c3->print ();
Console::WriteLine (S" ");
}
catch (DivideByZeroException * e)
{
Console::WriteLine("Dividing by zero");
}
break;
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.