Solved.

Solved.
Last edited on
This function determines what the output looks like:
53
54
55
56
std::ostream& operator<<(std::ostream& lhs, const Complex& rhs) // << operator.
{
    return lhs << '(' << rhs.r << " + " << rhs.i << "i)";
}


You need to write the logic into this function so it gives the format desired.
For example, to make it print 2 - 3i instead of 2 + -3i:
1
2
3
4
5
6
7
8
9
10
std::ostream& operator<<(std::ostream& lhs, const Complex& rhs) // << operator.
{
    lhs << '(' << rhs.r;
    if( rhs.i >= 0 ) 
        lhs  << " + " << rhs.i << "i)";
    else
        lhs  << " - " << -rhs.i << "i)";

   return lhs;
}

and so on to cover the cases when r or i are zero.
This part is your main problem:

1
2
3
4
5
std::ostream& operator<<(std::ostream& lhs, const Complex& rhs) // << operator.
{
    return lhs << '(' << rhs.r << " + " << rhs.i << "i)";
}


You're forcing the output to only be one thing. You could fix it with a few if 's. Such as:

1
2
3
4
5
6
7
8

   if (rhs.i = 0)
          return lhs << '(' << rhs.r << ')';
   if (rhs.i < 0)
          return lhs << '(' << rhs.r <<  rhs.i << "i)";
   if (rhs.i >0)
          return lhs << '(' << rhs.r << '+' << rhs.i << "i)";


As for getting the proper output on the right-hand side of the equation you will probably need to create a separate object (I'm not sure of the proper terminology for C++, and its late) for outputting the right-hand side of the equation.

I'm pretty sure that's your main problem anyway. I could be wrong.
Topic archived. No new replies allowed.