简体   繁体   中英

C++ operator overloading for complex number operations

I have an assignment in C++ and I'm having trouble getting started. The goal is to "design a class that uses the following overloaded operators for complex numbers: >> << + - * / "

My question isn't about the syntax of this, but more about the logic. I could use some help brain storming.

Input Sample:
2.5 -2.2
1.0 1.0

OutPut Sample:
A = (2.5) + (-2.2)i
B = (1.0) + (1.0)i

A + B = (3.5) + (-1.2)i
A - B = ..............
A * B = ..............
A / B = ..............

So how do I start this? The class "Complex" overloads these operators, so does that mean that I can only use these operators in the class (ie inside public functions)? If so would I want to do it this way? Or would I want to do it in my client/driver code?

Second, is it just adding i to the second value of each line? That seems too easy. Any direction would be much appreciated. (Just for the record, I'm not looking for anybody to do my homework for me... could just use some input)

It seems to me that the point is to demonstrate class operation overloading, so I think the idea is for you to make a class Complex which holds information about the real number and the imaginary number (the i means it's imaginary). Handle various operations between complex numbers in operator overrides you do yourself.

Once you have that and you see that it works (make a static test method which does various operations and prints the results to the screen), then worry about using that class to work with input since parsing input will be another task in of itself. Sometimes it's just simpler to divide problems into smaller problems than to attempt to do both at the same time.

Hope that helps. Good luck!

They like pairs of values:

A = N1 + I1i
B = N2 + I2i


A + B = (N1 + I1i) + (N2 + I2i)
      = N1 + I1i + N2 + I2i
      = (N1 + N2) + (I1i + I2i)
      = (N1 + N2) + (I1 + I2)i
A - B = (N1 + I1i) - (N2 + I2i)
      = N1 + I1i - N2 - I2i
      = (N1 - N2) + (I1i - I2i)
      = (N1 - N2) + (I1 - I2)i

// N1, N2, I1, I2 are all just normal numbers.
// You can multiply them like normal. You just have to keep track of the `i`
// Also not that i = sqrt(-1)
// Therefore  i * i = sqrt(-1) * sqrt(-1)
//                  = sqrt(-1)^2
//                  = -1
A * B = (N1 + I1i) * (N2 + I2i)
      = (N1 * N2) + (N1 * I2i) + (I1i * N2) + (I1i * I2i)
      = (N1 * N2) + (N1 * I2)i + (N2 * I1)i + (i * i * I1 * I2)
      = (N1 * N2) + i((N1 * I2) + (N2 * I1)) + (-1 * I1 * I2)

      // Simplest form
      = ((N1 * N2) - (I1 * I2)) + ((N1 * I2) + (N2 * I1))i


A / B = Repeat as above.

You need to design a class called Complex that includes at least:

  • a constructor allowing you to construct a Complex object from real and imaginary component values eg Complex(1, 5)

  • overrides the + operator so that you can add two Complex objects, returning a new Complex object eg Complex(1, 5) + Complex(3, 7) is Complex(4, 12)

  • similarly for other operators

But first you need to understand the basic math behind complex numbers so that you can write the operator overload methods.

There are a few things you have to do to accomplish this task:

Define a class (eg Complex) that can hold the data for the real and imaginary part of a complex number.

Overload the respective operators (eg):

class Complex
{
public:
    // other declarations here
    Complex operator+ (const Complex& rhs) const;
    // other stuff here
};

Implement the respective operators to actually perform the mathematical operation (eg):

Complex Complex::operator+ (const Complex& rhs) const
{
    Complex result = *this;
    result.Real += rhs.Real;
    result.Imaginary += rhs.Imaginary;
    return result;
}

Hope you are done with homework now :) Here is my solution if anyone still needs help.

#include <string>
#include <sstream>
#include <iostream>

using namespace std;


class Complex {
    float real_, imaginary_;
  public:
    Complex (float, float);
    Complex operator= (const Complex& rhs);
    Complex operator+ (const Complex& rhs) const;
    Complex operator- (const Complex& rhs) const;
    Complex operator* (const Complex& rhs) const;
    string toString() const;
};

Complex::Complex (float r, float i){
  real_ = r;
  imaginary_ = i;
}

Complex Complex::operator= (const Complex& rhs){
    real_ = rhs.real_;
    imaginary_ = rhs.imaginary_;
    return *this;
}

Complex Complex::operator+ (const Complex& rhs) const{
    Complex result = *this;
    result.real_ += rhs.real_;
    result.imaginary_ += rhs.imaginary_;
    return result;
}

Complex Complex::operator- (const Complex& rhs) const{
    Complex result = *this;
    result.real_ -= rhs.real_;
    result.imaginary_ -= rhs.imaginary_;
    return result;
}

Complex Complex::operator* (const Complex& rhs) const{
    Complex result = *this; // this-> == *this == (*this)
    result.real_ = real_ * rhs.real_ - imaginary_ * rhs.imaginary_;
    //cout << result.real_ << "R " << result.imaginary_ << "I "<< "|" << rhs.real_ << "R " << rhs.imaginary_ << "I\n";
    result.imaginary_ = (real_ * rhs.imaginary_) + (rhs.real_ * imaginary_);
    //cout << result.real_ << "R " << result.imaginary_ << "I "<< "|" << rhs.real_ << "R " << rhs.imaginary_ << "I\n";
    return result;
}

string Complex::toString() const {
  stringstream ss;
  if (imaginary_ > 0){
    ss << real_ << " + " << imaginary_ << "i";
  }
  else {
    ss << real_ << " " << imaginary_ << "i";
  }
  return ss.str();
}

int main () {
  Complex a(5, 6);
  Complex b(1, 4);

  Complex sum = a + b;
  Complex dif = a - b;
  Complex pro = a * b;

  cout << "sum: " << sum.toString() << "\n";
  cout << "difference: " << dif.toString() << "\n";
  cout << "product: " << pro.toString() << "\n";

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM