我在这里遇到一些麻烦。在主函数中,我似乎无法弄清楚如何写出result2和result3的计算。它们如下:

result2计算(z2 + z3)/(z3 + z2)

result3测试以查看z1 x(z2 + z3)和(z1 x z2)+(z1 x z3)是否相等。

我假设result2应该总是等于1,而result3应该总是相等,但是我不明白应该如何写。有任何想法吗?这是我到目前为止的内容:

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;
}

最佳答案

编辑:我犯了一个错误。水平线之后的代码计算z2 / z3,而不是(z2 + z3)/(z3 + z2)。以下将得到正确的结果:

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




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

关于c++ - C++使用类将值传递给函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20447757/

10-13 05:00