C ++复数模板-/ =运算符

时间:2018-11-21 08:56:30

标签: c++

我有一个用于复数的模板类。我在那里做了一些工作的运算符,例如:<<,==,=,/,,+,-,+ =,-+, =。

我对/ =有问题。我试图写该方法。但是代码运行不正常。该方法返回错误的值。

您可以看看我的代码,看看为什么我的方法/ =不起作用吗?

这是方法/ =:

   //OPERATOR /=
    template<class T>
    Zespolona<T> Zespolona<T>::operator /=(const Zespolona<T>& obj) 
    {
        Zespolona<T> temp;
        temp.reNum = ((reNum * obj.reNum) + (imNum * obj.imNum)) / ((pow(obj.reNum, 2.0)) + (pow(obj.imNum, 2.0)));
        temp.imNum = ((obj.reNum * imNum) - (reNum * obj.imNum)) / ((pow(obj.reNum, 2.0)) + (pow(obj.imNum, 2.0)));
        return temp;
    }

其余代码:

 #include <iostream>

using namespace std;

template <class T>
class Zespolona {
private:
    //Zmienne prywatne
    T reNum;
    T imNum;
public:
    //Deklaracja konstruktorów
    Zespolona();
    Zespolona(T re);
    Zespolona(T re, T im);

    //Deklaracja operatorów
    Zespolona<T> operator+(const Zespolona<T>&)const;
    Zespolona<T> operator-(const Zespolona<T>&)const;
    Zespolona<T> operator*(const Zespolona<T>&)const;
    Zespolona<T> operator/(const Zespolona<T>&)const;
    Zespolona<T> operator+=(const Zespolona<T>&);
    Zespolona<T> operator-=(const Zespolona<T>&);
    Zespolona<T> operator*=(const Zespolona<T>&);
    Zespolona<T> operator/=(const Zespolona<T>&);
    Zespolona<T> operator=(const Zespolona<T>&);
    bool operator==(const Zespolona&) const;
    template <class U>
    friend ostream& operator<<(ostream&, const Zespolona<U>&);


};

//Konstruktor bez argumetów
template<class T>
Zespolona<T>::Zespolona()
{
    reNum = 0.0;
    imNum = 0.0;
}
//Konstruktor z jednym argumentem
template<class T>
Zespolona<T>::Zespolona(const T re)
{
    reNum = re;
    imNum = 0.0;
}


//Konstruktor z dwoma argumentami
template<class T>
Zespolona<T>::Zespolona(const T re, const T im)
{
    reNum = re;
    imNum = im;
}

//DODAWANIE
template<class T>
Zespolona<T> Zespolona<T>::operator +(const Zespolona<T>& obj) const
{
    Zespolona<T> temp;
    temp.reNum = reNum + obj.reNum;
    temp.imNum = imNum + obj.imNum;
    return temp;
}
//ODEJMOWANIE
template<class T>
Zespolona<T> Zespolona<T>::operator -(const Zespolona<T>& obj) const
{
    Zespolona<T> temp;
    temp.reNum = reNum - obj.reNum;
    temp.imNum = imNum - obj.imNum;
    return temp;
}
//MNOZENIE
template<class T>
Zespolona<T> Zespolona<T>::operator *(const Zespolona<T>& obj) const
{
    Zespolona<T> temp;
    temp.reNum = (reNum * obj.reNum) - (imNum * obj.imNum);
    temp.imNum = (reNum * obj.imNum) + (imNum * obj.reNum);
    return temp;
}
//DZIELENIE
template<class T>
Zespolona<T> Zespolona<T>::operator /(const Zespolona<T>& obj) const
{
    Zespolona<T> temp;
    temp.reNum = ((reNum * obj.reNum) + (imNum * obj.imNum)) / ((pow(obj.reNum, 2.0)) + (pow(obj.imNum, 2.0)));
    temp.imNum = ((obj.reNum * imNum) - (reNum * obj.imNum)) / ((pow(obj.reNum, 2.0)) + (pow(obj.imNum, 2.0)));
    return temp;
}
//OPERATOR PORÓWNANIA
template <class T>
bool Zespolona<T>::operator ==(const Zespolona& obj) const
{
    return (reNum == obj.reNum && imNum == obj.imNum);
}
//OPERATOR =
template <class T>
Zespolona<T> Zespolona<T>::operator=(const Zespolona& obj)
{
    if (this != &obj)
    {
        reNum = obj.reNum;
        imNum = obj.imNum;
    }

    return  *this;
}
//OPERATOR +=
template<class T>
Zespolona<T> Zespolona<T>::operator +=(const Zespolona<T>& obj)
{
    reNum += obj.reNum;
    imNum += obj.imNum;

    return *this;
}
//OPERATOR -=
template<class T>
Zespolona<T> Zespolona<T>::operator -=(const Zespolona<T>& obj)
{
    reNum -= obj.reNum;
    imNum -= obj.imNum;
    return *this;
}
//OPERATOR *=
template<class T>
Zespolona<T> Zespolona<T>::operator *=(const Zespolona<T>& obj)
{

    reNum = (reNum * obj.reNum) - (reNum * obj.imNum);
    imNum = (imNum * obj.reNum) + (imNum * obj.imNum);
    return *this;
}
//OPERATOR /=
template<class T>
Zespolona<T> Zespolona<T>::operator /=(const Zespolona<T>& obj) 
{
    Zespolona<T> temp;
    temp.reNum = ((reNum * obj.reNum) + (imNum * obj.imNum)) / ((pow(obj.reNum, 2.0)) + (pow(obj.imNum, 2.0)));
    temp.imNum = ((obj.reNum * imNum) - (reNum * obj.imNum)) / ((pow(obj.reNum, 2.0)) + (pow(obj.imNum, 2.0)));
    return temp;
}
//OPERATOR WEJSCIA
template<class T>
ostream& operator<<(ostream& out, const Zespolona<T>& zesp)
{
    out << zesp.reNum;
    out << "+";
    out << zesp.imNum;
    out << "i";
    return out;
}
//funkcja testująca
void test() {
    try {
        Zespolona<double> a(1.0, 2.1);
        Zespolona<double> b(0.8, 1.7);
        Zespolona<double> c;

        cout << a << " + " << b << " = " << a + b << endl;
        cout << a << " * " << b << " = " << a * b << endl;

        cout << a << " == " << b << " = " << (a == b) << endl;
        c = a;
        cout << a << " == " << c << " = " << (a == c) << endl;

        c = Zespolona<double>(1.0, 1.0);
    }
    catch (const char *e) {
        cout << "Exception: " << e << endl;
    }
    catch (const exception &e) {
        cout << "Exception: " << e.what() << endl;
    }
    catch (...) {
        cout << "Caught not expected exception" << endl;
    }

}
int main() {
    Zespolona<double> x(1.0, 1.0);
    Zespolona<double> z(0.5, 1.0);

    cout << x / z << endl;

    x /= z;
    cout << x << endl;




    //test();
    system("pause");
}

2 个答案:

答案 0 :(得分:5)

您正在计算正确的结果,但没有将其存储在变量中。将您的代码更改为:

template<class T>
Zespolona<T>& Zespolona<T>::operator /=(const Zespolona<T>& obj) 
{
    *this = *this / obj;
    return *this;
}

您不应在多个地方实现这样的逻辑。实现方法或运算符后,请使用它。 请记住,方法的工作原理类似于带有指向对象的指针的函数

Zespolona<T> operator /=(Zespolona<T>* this, const Zespolona<T>& obj) 

如果使用调用此方法

a /= b;

指向a的指针将是第一个参数,而b是第二个参数。返回值未复制到a

或者您可以将呼叫更改为

x = x /= z;

进行修复。也许这可以帮助您理解问题。

编辑: 最好将逻辑实现到operator/=中,并在operator/的实现中使用它。这样一来,您就可以在operator/=中实现计算,并避免重复计算。

答案 1 :(得分:2)

  • 首先将this的当前值存储到temp变量中
  • 使用tempobj计算除法将其分配给this
  • 返回*this

>

template<class T>
Zespolona<T> Zespolona<T>::operator /=(const Zespolona<T>& obj) 
{
    Zespolona<T> temp = *this;
    this->reNum = ((temp.reNum  * obj.reNum) + (temp.imNum * obj.imNum)) / ((pow(obj.reNum, 2.0)) + (pow(obj.imNum, 2.0)));
    this->imNum = ((obj.reNum * temp.imNum) - (temp.reNum  * obj.imNum)) / ((pow(obj.reNum, 2.0)) + (pow(obj.imNum, 2.0)));
    return *this;
}