Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

Posted on 2006-04-23
Medium Priority
229 Views
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
0
Question by:ravs22
• 6
• 5
• 4

LVL 4

Expert Comment

ID: 16518620
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 Comment

ID: 16518644
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

LVL 4

Expert Comment

ID: 16518652
Double of mscorelib does support Parse...
0

Author Comment

ID: 16518683
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

LVL 4

Expert Comment

ID: 16518709
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

LVL 48

Expert Comment

ID: 16518802
Instead of Parse use Convert.ToDouble Method (String).
0

Author Comment

ID: 16521093
Can you please clarify on how use the Convert.ToDouble Method?
Thank You
0

LVL 48

Expert Comment

ID: 16522373

0

Author Comment

ID: 16523564
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

LVL 4

Expert Comment

ID: 16523928
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

LVL 48

Expert Comment

ID: 16524001
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

LVL 48

Expert Comment

ID: 16524041
BTW, multiplication and division math is your responsibility, I am not sure that you do this right :-)
0

Author Comment

ID: 16532913
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 Comment

ID: 16532943
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

LVL 48

Accepted Solution

AlexFM earned 1400 total points
ID: 16533052
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourtâ€¦
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext mayâ€¦
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signatâ€¦
###### Suggested Courses
Course of the Month10 days, 16 hours left to enroll