C++使用类将值传递给函数

标签 c++ class complex-numbers

我在这里遇到了一些问题。在主函数中,我似乎无法弄清楚如何写出 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;
}

最佳答案

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

我希望我做对了(这是第一个问题。)

但这个例子实际上是对运算符重载故事的完美介绍。像上面这样可怕的不可读的表达式正是你想通过重载 +、* 等来避免的事情。

也许这是学校的任务,你现在只需要这样做,但我强烈建议阅读一些关于运算符重载的内容。 :)

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

相关文章:

c++ - 如何在 Visual Studio 中选择启动对象?

java - Class as Hashtable 键——这是个好主意吗?

c++ - 像数组一样初始化类对象

c - 负复数幂

python - 如果我想让 OpenCV dnn 模块加载 PyTorch 模型,我应该如何保存它

c++ - 为什么这些点被破坏了三次?

c++ - 特征类是如何工作的,它们是做什么的?

class - 什么是 D 中的类(class)监视器?

matlab - fsolve 返回复杂的解决方案 - 如何仅限于实际搜索空间?

python - 如何在 numpy 中制作二维复数数组?