关于加,乘,除,减复数的问题C ++-运算符重载

问题描述 投票:0回答:1

我编写了一个处理复数的程序-写,读,做一些简单的操作。我的老师说我必须定义许多运算符(十八个)。我有一个操作员有问题。

    Comp operator=(const Comp x)
    {
        Comp temp;
        temp.real = re();
        temp.imag = im();
        return temp;
    }

当我将此运算符放入代码中时,加,乘,除和减法不起作用。我的输出中有

(1.10,2.00) 
(1.70,3.14) 
2.28
3.57
1.07
1.07
(1.10,-2.00) 
(1.70,-3.14) 
(0.00,0.00) 
(0.00,0.00) 
(0.00,0.00) 
(0.00,0.00) 

代替

(1.10,2.00) 
(1.70,3.14) 
2.28
3.57
1.07
1.07
(1.10,-2.00) 
(1.70,-3.14) 
(2.80,5.14) 
(-0.60,-1.14) 
(-4.41,6.85) 
(0.64,-0.00)

这是我的整个代码

#include <fstream>
#include <cstdlib> 
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif
using namespace std;

namespace ComplexNumbers
{
class Comp {
    double real, imag;

public:
    Comp(){
    real=0;
    imag=0;
    }
    double re(void) const
    {
        return real;
    }
    double im(void) const
    {
        return imag;
    }
    double mod(void) const
    {
        return sqrt(re()*re() + im()*im());
    }
    double arg(void) const
    {
        double faza;
        if (im() >= 0)
            faza = acos(re()/mod());
        else
            faza = 2*M_PI - acos(re()/mod());

    return faza;
    }
    const Comp conj(void) const
    {
        Comp temp;
        temp.real = re();
        temp.imag = -im();
        return temp;
    }
    ~Comp(){}
    const Comp operator+();
    const Comp operator-();
    bool operator!(void);
    const Comp& operator++()
    { 
        return *this;
    }
    const Comp operator++(int)
    { 
        Comp temp(*this); 
        operator++(); 
        return temp;  
    }
    const Comp& operator--()
    {
        return *this;
    }
    const Comp operator--(int)
    {
        Comp temp(*this); 
        operator--(); 
        return temp; 
    }
    Comp operator=(const Comp x)
    {
        Comp temp;
        temp.real = re();
        temp.imag = im();
        return temp;
    }
    Comp& operator-=(const Comp& x)
    {
        int value = 0;
        value -= x.real;
        value -= x.imag;
        return *this;
    } 
    Comp& operator+=(const Comp& x)
    {
        int value = 0;
        value += x.real;
        value += x.imag;
        return *this;
    }
    Comp& operator*=(const Comp& x)
    {
        int value = 0;
        value *= x.real;
        value *= x.imag;
        return *this;
    }
    Comp& operator/=(const Comp& x)
    {
        int value = 0;
        value /= x.real;
        value /= x.imag;
        return *this;
    } 
    friend const Comp operator+(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = x.real + y.real;
        temp.imag = x.imag + y.imag;
        return temp;
    }
    friend const Comp operator-(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = x.real - y.real;
        temp.imag = x.imag - y.imag;
        return temp;
    }
    friend const Comp operator*(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = (x.real * y.real - x.imag * y.imag);
        temp.imag = (x.real * y.imag + x.imag * y.real);
        return temp;
    }
    friend const Comp operator/(const Comp& x, const Comp& y)
    {
        Comp temp;
        temp.real = ((x.real * y.real) + (x.imag * y.imag))/(y.real*y.real + y.imag*y.imag);
        temp.imag = ((x.imag * y.real) - (x.real * y.imag))/(y.real*y.real + y.imag*y.imag);
        return temp;
    }
    friend bool operator==(const Comp& x, const Comp& y)
    {
        if (x.real == y.real && x.imag == y.imag)
            return 1;
        else
            return 0;
    }
    friend bool operator!=(const Comp& x, const Comp& y)
    {

        if (x.real != y.real || x.imag != y.imag)
            return 1;
        else
            return 0;
    }

    friend std::ostream& operator<<(std::ostream& wart1,  const Comp& a)
    {
        return wart1 <<fixed << setprecision(2) << '(' << a.re() << "," << a.im() << ')' << ' ' << endl;
    }
    friend std::istream& operator>>(std::istream& wart2, Comp& b){
        char c = '0';
        return wart2>>c>>b.real>>c>>b.imag>>c; 
    }
};
}
using namespace ComplexNumbers;
int main(int argc, char* argv[])
{ 
    ifstream read(argv[1]);
    if (!read)
        { cerr << "Open error: " << argv[1] << endl; exit(1);}
    ofstream write(argv[2]);

    if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);} 
    read.clear();
    read.seekg(0);
    Comp x1;
    read >> x1;
    write << x1;
    cout << x1;
    Comp x2;
    read >> x2;
    write << x2;
    cout << x2;
    cout << x1.mod() << endl;
    cout << x2.mod() << endl;
    cout << x1.arg() << endl;
    cout << x2.arg() << endl;
    cout << x1.conj();
    cout << x2.conj();
    write << x2;
    write << x1.mod() << endl;
    write << x2.mod() << endl;
    write << x1.arg() << endl;
    write << x2.arg() << endl;
    write << x1.conj();
    write << x2.conj();
    Comp sum;
    sum = x1 + x2;
    cout << sum;
    write << sum;
    Comp sub;
    sub = x1 - x2;
    cout << sub;
    write << sub;
    Comp mult;
    mult = x1 * x2;
    cout << mult;
    write << mult;
    Comp div;
    div = x1 / x2;
    cout << div;
    write << div;

    return 0;
}  

c++ oop operator-overloading operators complex-numbers
1个回答
0
投票

您的副本分配运算符应分配给对象本身,如下所示:

Comp *operator=(const Comp x)
{
    real = x.real;
    imag = x.imag;
    return *this;
}

也请注意功能签名的更改

您的许多其他运算符(operator-=operator+=operator*=和operator / =`)也遇到相同的问题,因此,您还应该提供一个副本构造函数('3规则')。

© www.soinside.com 2019 - 2024. All rights reserved.