# complex number arithmetic

• 12-10-2003
Micko
complex number arithmetic
I'm trying to make class "Complex" with operator overload so I can perform c1+c2 etc.
I don't know how to deal with following problem:
I ask user to enter complex number from keyboard and he(she)
type: 2+i3. I can ask to enter Real and Imaginary part separately, but how can I achive this.
I was thinking to enter information from keyboard to string and then parse string like array of character seeking '+' or '-'.
Is it possible to achive this in some other simple way??
• 12-10-2003
Prelude
>I'm trying to make class "Complex" with operator overload so I can perform c1+c2 etc.
>I ask user to enter complex number from keyboard and he(she)
I hope you aren't trying to ask for this input from within the class. A complex number class doesn't lend itself well to asking for its own input. I would imagine that a good complex class would look something like this:
Code:

```class Complex { public:   Complex ( const int real, const int imaginary );   Complex ( const Complex& com ); public:   int real() const;   int imaginary() const; public:   Complex& operator= ( const int rhs );   Complex& operator+= ( const int rhs );   Complex& operator-= ( const int rhs );   Complex& operator*= ( const int rhs );   Complex& operator/= ( const int rhs ); public:   Complex& operator= ( const Complex& rhs );   Complex& operator+= ( const Complex& rhs );   Complex& operator-= ( const Complex& rhs );   Complex& operator*= ( const Complex& rhs );   Complex& operator/= ( const Complex& rhs ); private:   int real_part;   int imag_part; };```
If, on the other hand, you are parsing input from without and then passing it to your class, parsing a complete string would work, but it really depends on how your program is laid out whether or not there is a better way. I personally would take input in a more simplistic manner to make things easier, such as a complex number pair without an operation that you can work with later:
Code:

`2 3 4 5 6 7`
This way you can more easily use formatted input to get what you want:
Code:

```vector<Complex> get_complex_vector ( string& input ) {   vector<Complex> ret;   stringstream sin ( input );   int re, im;   while ( sin>> re && sin>> im )     ret.push_back ( Complex ( re, im ) );   return ret; }```