Question

I'm having trouble with a couple things here. In the main function, I cannot seem to figure out how write out the computation for result2 and result3. They are as follows:

result2 calculates (z2 + z3) / (z3 + z2)

result3 tests to see if z1 x (z2 + z3) and (z1 x z2) + (z1 x z3) are equal.

I'm assuming result2 should always come out to be 1 and result3 should always be equal, but I do not understand how they should be written. Any ideas? Here's what I have so far:

class Complex
{
private:
    double real;
    double imag;

public:
    Complex()
    {
        real = 0.0;
        imag = 0.0;
    }

    Complex(double r, double i)
    {
        real = r;
        imag = i;
    }

    Complex add(const Complex& z) const
    {
        Complex sum;
        sum.real = (real + z.real);
        sum.imag = (imag + z.imag);

        return sum;
    }

    Complex conjugate() const
    {
        Complex conj;

        conj.real = real;
        conj.imag = imag * -1;

        return conj;
    }

    bool equals(const Complex& z) const
    {
        if (real == z.real && imag == z.imag)
            return true;
        else
            return false;
    }

    Complex inverse() const
    {
        Complex inv;

        inv.real = (real / (pow(real, 2) + pow(imag, 2)));
        inv.imag = -1 * (imag / (pow(real, 2) + pow(imag, 2)));

        return inv;
    }

    double modulus() const
    {
        double mod;

        mod = sqrt(pow(real, 2) + pow(imag, 2));

        return mod;
    }

    Complex multiply(const Complex& z) const
    {
        Complex prod;
        prod.real = (real * z.real) - (imag * z.imag);
        prod.imag = (real * z.imag) + (imag * z.real);

        return prod;
    }

    Complex subtract(const Complex& z) const
    {
        Complex diff;
        diff.real = real - z.real;
        diff.imag = imag - z.imag;

        return diff;
    }

    string str() const
    {
        stringstream sout;
        sout<<fixed<<setprecision(6);
        if (real == 0 && imag == 0)
            sout<<"0";
        else if (imag == 0)
            sout<<real;
        else if (real == 0 && imag == 1)
            sout<<"i";
        else if (real == 0 && imag == -1)
            sout<<"-i";
        else if (imag == 1)
            sout<<real<<"+i";
        else if (imag == -1)
            sout<<real<<"-i";
        else if (real == 0)
            sout<<real<<"i";
        else if (imag > 0)
            sout<<real<<"+"<<imag<<"i";
        else if (imag < 0)
            sout<<real<<imag<<"i";

        return sout.str();
    }
};

int main()
{
double r1, r2, r3, c1, c2, c3;

cout<<endl;
cout<<"Enter the real and imaginary part of the first complex number-> ";
cin>>r1>>c1;
cout<<"Enter the real and imaginary part of the second complex number-> ";
cin>>r2>>c2;
cout<<"Enter the real and imaginary part of the third complex number-> ";
cin>>r3>>c3;

Complex z1(r1, c1);
Complex z2(r2, c2);
Complex z3(r3, c3);

cout<<endl;
cout<<"z1 = "<<z1.str()<<endl;
cout<<"z2 = "<<z2.str()<<endl;
cout<<"z3 = "<<z3.str()<<endl;
cout<<endl;

Complex result1 = z2.subtract(z3).multiply(z1);
Complex result2 = (z2.add(z3)); //not sure what goes next
Complex result3 = 

cout<<"(z2 - z3) x z1 = "<<result1.str()<<endl;
cout<<"(z2 + z3) / (z3 + z2) = "<<result2.str()<<endl;
cout<<"z1 x (z2 + z3) and (z1 x z2) + (z1 x z3) "<<result3.str()<<endl;

return 0;
}
Was it helpful?

Solution 2

Edit: I made a mistake. The code after the horizontal line computes z2 / z3, NOT (z2 + z3)/(z3 + z2). The following gets the correct result:

Complex z2(4, 4);
Complex z3(2, 2);

Complex numerator = z2.add(z3);
Complex denominator = z3.add(z2);
Complex conjugate = denominator.conjugate();
numerator = numerator.multiply(conjugate);
denominator = denominator.multiply(conjugate);
Complex result = numerator.multiply(denominator.inverse());

cout<<"(z2 + z3) / (z3 + z2) = "<< result.str(); // 1

Pulled the formula from Wikipedia.

Complex z2(4, 4);
Complex z3(2, 2);

// z2.real = A
// z2.imag = B
// z3.real = C
// z3.imag = D
// Formula is:
// a*c + b*d  +  b*c - a*d
// -----------------------
// c^2 + d^2     c^2 + d^2
double first  = ((z2.real * z3.real) + (z2.imag * z3.imag))
                 /(z3.real * z3.real + z3.imag * z3.imag);
double second = ((z2.imag * z3.real) - (z2.real * z3.imag))
                 /(z3.real * z3.real + z3.imag * z3.imag);
Complex result(first, second);

cout<<"z2 / z3 = "<<result.str()<<endl;
// Result: 2

OTHER TIPS

Complex result2 = ((z2.add(z3)).multiply((z3.add(z2)).inverse());

I hope I got it right (this is for the first question.)

But this example would actually be the perfect introduction to a story about operator overloading. Horribly unreadable expressions such as the above are exactly the thing you want to avoid by overloading +, * etc.

Maybe this is a task for school and you just have to do it like this for now, but I would strongly suggest reading a bit about operator overloading. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top