| | |
Need Help with complex numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 11
Reputation:
Solved Threads: 0
I'm having problems. I get the addition part. can somebody help.
Here is the code i wrote so far.
thanks,
Here is the code i wrote so far.
thanks,
C++ Syntax (Toggle Plain Text)
#ifndef complex.h #define complex.h class complex { public: complex(); complex(double real, double imaginary); void complex addition(const complex &a); void complex printcomplex(); void complex setcomplexnumber(); private: double realpart, imaginarypart; }; #endif //constructor complex::complex(double real, double imaginary) { setcomplexnumber(real, imaginary); } void complex:: addition( const complaex &a) { //adding-This is where i'm stuck\\ } void complex:: printcomplex() { cout<< '(' <<realpart <<", " << imaginarypart<<')' } void complex::setcomplexnumber(double real, double imaginary) { realpart=real; imaginarypart=imaginary; }
Last edited by alc6379; Feb 3rd, 2005 at 3:29 pm. Reason: fixed [code] tags
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].]
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
•
•
Join Date: Sep 2004
Posts: 11
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
int main() { complex b(1,7), c(9,2); b.printcomplex(); cout<< "+"; c.printcomplex(); cout<< "="; b.addition(c); b.printcomplex(); cout<< endl; return 0;
Last edited by alc6379; Feb 3rd, 2005 at 3:30 pm.
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.
When properly implemented you can use the following testcode on it:
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
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class complexNumber { private: float real; float imag; public: complexNumber(); complexNumber(float rPart, float iPart); ~complexNumber(); complexNumber operator+(const complexNumber& c); complexNumber operator-(const complexNumber& c); friend ostream& operator<<(ostream& s, const complexNumber& c); };
When properly implemented you can use the following testcode on it:
C++ Syntax (Toggle Plain Text)
#include "complexnum.h" int main() { complexNumber t(1, 1); complexNumber t2(2, 2); complexNumber t3 = t + t2; cout << t << endl << t2 << endl << t3; return 0; }
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
•
•
•
•
write a statement too add the realpart of a to class imaginarypart.
(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:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- Help! Convert pixel array of complex numbers to image. (Java)
- Trouble writing code for arithmetic calculations of complex numbers (C++)
- Code Snippet: multiply of complex numbers matrix (C++)
- Complex Numbers (C++)
- Complex Numbers (C)
- Help on Complex Number Calculator (C)
Other Threads in the C++ Forum
- Previous Thread: Help with error message
- Next Thread: 1.#IND problem
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






