ravs22
asked on
Adding/Substracting Two Complex Numbers
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::Rea dLine());
Console::WriteLine(S" Enter the Real Part of the 2nd Number: ");
double Real2 = double::Parse(Console::Rea dLine());
Console::WriteLine(S"Enter the Imaginary Part of the 1st Number: ");
double Complex1 = double::Parse(Console::Rea dLine());
Console::WriteLine(S"Enter the Imaginary Part of the 2nd Number:");
double Complex2 = double::Parse(Console::Rea dLine());
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::Rea dLine());
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
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::Rea
Console::WriteLine(S" Enter the Real Part of the 2nd Number: ");
double Real2 = double::Parse(Console::Rea
Console::WriteLine(S"Enter
double Complex1 = double::Parse(Console::Rea
Console::WriteLine(S"Enter
double Complex2 = double::Parse(Console::Rea
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::Rea
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
ASKER
ok thanks,
As per the above suggestion, I removed Parse and replaced with the following code:
Double Real1 = Double(Console::ReadLine() );
Is this correct?
After compiling I get the following errors:
error C2440: 'type cast' : cannot convert from 'System::String __gc *' to 'double'
Thank You
As per the above suggestion, I removed Parse and replaced with the following code:
Double Real1 = Double(Console::ReadLine()
Is this correct?
After compiling I get the following errors:
error C2440: 'type cast' : cannot convert from 'System::String __gc *' to 'double'
Thank You
Double of mscorelib does support Parse...
Double Real1 = Double::Parse(Console::Rea dLine());
Double Real1 = Double::Parse(Console::Rea
ASKER
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
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
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
this could do most of the work for you...
http://wwwasd.web.cern.ch/wwwasd/lhc++/RW/stdlibcr/com_8038.htm
Instead of Parse use Convert.ToDouble Method (String).
ASKER
Can you please clarify on how use the Convert.ToDouble Method?
Thank You
Thank You
double Real1 = double::Parse(Console::Rea dLine());
double Real1 = System::Convert::ToDouble( Console::R eadLine()) ;
double Real1 = System::Convert::ToDouble(
ASKER
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:
complex (double, double); //overloaded constructor
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: ");
Double Real1 = Double::Parse(Console::Rea dLine());
Console::WriteLine(S" Enter the Real Part of the 2nd Number: ");
Double Real2 = Double::Parse(Console::Rea dLine());
Console::WriteLine(S"Enter the Imaginary Part of the 1st Number: ");
Double Complex1 = Double::Parse(Console::Rea dLine());
Console::WriteLine(S"Enter the Imaginary Part of the 2nd Number:");
Double Complex2 = Double::Parse(Console::Rea dLine());
Console::WriteLine(S"If adding Enter 1, If Sub enter 2, If Mult Enter 3, If Div Enter 4 ");
int input1 = Int32::Parse(Console::Read Line());
complex *c1 = new complex (Real1,Complex1);
complex *c2 = new complex (Real2,Complex2);
complex *c3 = new complex (0,0);
switch (input1)
{
case 1:
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" ");
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
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 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: ");
Double Real1 = Double::Parse(Console::Rea
Console::WriteLine(S" Enter the Real Part of the 2nd Number: ");
Double Real2 = Double::Parse(Console::Rea
Console::WriteLine(S"Enter
Double Complex1 = Double::Parse(Console::Rea
Console::WriteLine(S"Enter
Double Complex2 = Double::Parse(Console::Rea
Console::WriteLine(S"If adding Enter 1, If Sub enter 2, If Mult Enter 3, If Div Enter 4 ");
int input1 = Int32::Parse(Console::Read
complex *c1 = new complex (Real1,Complex1);
complex *c2 = new complex (Real2,Complex2);
complex *c3 = new complex (0,0);
switch (input1)
{
case 1:
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" ");
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
}
return 0;
}
Any input, help or comments appreciated.
Thank You
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;
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;
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("Dividi ng by zero");
}
I think the last way is the best.
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("Dividi
}
I think the last way is the best.
BTW, multiplication and division math is your responsibility, I am not sure that you do this right :-)
ASKER
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("Dividi ng 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
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("Dividi
}
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...