Thread: complex number arithmetic

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003

    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??
    Thanks in advance!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >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:
    class Complex {
      Complex ( const int real, const int imaginary );
      Complex ( const Complex& com );
      int real() const;
      int imaginary() const;
      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 );
      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 );
      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:
    2 3 4 5 6 7
    This way you can more easily use formatted input to get what you want:
    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;
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  3. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 01:42 PM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
Website Security Test