Need Help with complex numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Need Help with complex numbers

 
0
  #1
Feb 2nd, 2005
I'm having problems. I get the addition part. can somebody help.

Here is the code i wrote so far.
thanks,

  1. #ifndef complex.h
  2. #define complex.h
  3.  
  4. class complex
  5. {
  6. public:
  7. complex();
  8. complex(double real, double imaginary);
  9. void complex addition(const complex &a);
  10.  
  11. void complex printcomplex();
  12. void complex setcomplexnumber();
  13.  
  14. private:
  15. double realpart, imaginarypart;
  16. };
  17. #endif
  18.  
  19. //constructor
  20. complex::complex(double real, double imaginary)
  21. {
  22. setcomplexnumber(real, imaginary);
  23. }
  24.  
  25. void complex:: addition( const complaex &a)
  26. {
  27. //adding-This is where i'm stuck\\
  28. }
  29. void complex:: printcomplex()
  30. {
  31. cout<< '(' <<realpart <<", " << imaginarypart<<')'
  32. }
  33. void complex::setcomplexnumber(double real, double imaginary)
  34. {
  35. realpart=real;
  36. imaginarypart=imaginary;
  37. }
Last edited by alc6379; Feb 3rd, 2005 at 3:29 pm. Reason: fixed [code] tags
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,388
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 244
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need Help with complex numbers

 
0
  #2
Feb 2nd, 2005
How would you express the addition of two complex numbers without code?

Then try to translate that into code.

(And keep in mind what you intend to do: add to the current object, or add two distinct objects and return a third.)

[And you can edit your post to add the missing closing tag: [/code].]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Re: Need Help with complex numbers

 
0
  #3
Feb 2nd, 2005
my brain is about to explode. Its not coming to me. Need Help/.

I need to write a statement to add the realpart of a to the class realpart.
and write a statement too add the realpart of a to class imaginarypart.

Here's the rest of the code
  1. int main()
  2. {
  3. complex b(1,7), c(9,2);
  4.  
  5. b.printcomplex();
  6. cout<< "+";
  7. c.printcomplex();
  8. cout<< "=";
  9. b.addition(c);
  10. b.printcomplex();
  11.  
  12. cout<< endl;
  13. return 0;
Last edited by alc6379; Feb 3rd, 2005 at 3:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Need Help with complex numbers

 
0
  #4
Feb 2nd, 2005
You're working in C++. C++ has operator overloading.
You're working on a mathematical addition operation. It makes in this context perfect sense to define an operator in (or as a friend of) your complex class.

As a start I'll give you the headerfile for a complex number class I implemented for fun a while ago.
The implementation of the methods and operator is trivial and I leave that to you.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class complexNumber
  6. {
  7. private:
  8. float real;
  9. float imag;
  10.  
  11. public:
  12. complexNumber();
  13. complexNumber(float rPart, float iPart);
  14. ~complexNumber();
  15.  
  16. complexNumber operator+(const complexNumber& c);
  17. complexNumber operator-(const complexNumber& c);
  18. friend ostream& operator<<(ostream& s, const complexNumber& c);
  19. };

When properly implemented you can use the following testcode on it:
  1. #include "complexnum.h"
  2.  
  3. int main()
  4. {
  5. complexNumber t(1, 1);
  6. complexNumber t2(2, 2);
  7. complexNumber t3 = t + t2;
  8. cout << t << endl << t2 << endl << t3;
  9. return 0;
  10. }

As you see I created not just a + operator but also a << operator so I can output a complex number directly and consistently.

The output of that small program should be
1+i
2+2i
3+3i
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Need Help with complex numbers

 
0
  #5
Feb 3rd, 2005
write a statement too add the realpart of a to class imaginarypart.
that doesnt sound right! For 2 complex numbers

(a+bi) + (c+di) = z (z is the result)

real(z) = a+c
im(z) = b+d

So why do you want to add the real of one complex number to a different complex number's imaginary?
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC