!!!!!!.. 2022-04-20 20:18 采纳率: 66.7%
浏览 21
已结题

类的友元重载函数构造复数进行计算

为什么类里的析构函数delete去掉就可以正确得到,而加上就出现如图二的错误了呢?想了一下午还是想不明白?(┯_┯)!

img

#include<iostream>
using namespace std;
class complex
{
    double *p;
public:
    complex(double m=0,double n=0)
    {
        p=new double[2];
        p[0]=m;p[1]=n;
    }
    //complex(){}
    complex(const complex &obj)
    {
        p[0]=obj.p[0];
        p[1]=obj.p[1];
    }
    ~complex()
    {
        delete[]p;
    }
    friend complex operator+ ( const complex & c1, const complex & c2 )
    {
        double r=c1.p[0]+c2.p[0];
        double i=c1.p[1]+c2.p[1];
        return complex(r,i);
    }
    friend complex operator- ( const complex & c1, const complex & c2 )
    {
        double r=c1.p[0]-c2.p[0];
        double i=c1.p[1]-c2.p[1];
        return complex(r,i);
    }
    friend complex operator* ( const complex & c1, const complex & c2 )
    {
        double r=c1.p[0]*c2.p[0]-c1.p[1]*c1.p[1];
        double i=c2.p[0]*c1.p[1]+c2.p[1]*c1.p[0];
        return complex(r,i);
    }
    void show()
    {
        if(p[0]!=0)
        {
            cout<<p[0];
            if(p[1]>0&&p[1]!=1)
            {
                cout<<'+'<<p[1]<<'i'<<endl;
            }
            else if(p[1]<0&&p[1]!=-1)
                cout<<p[1]<<'i'<<endl;
            else if(p[1]==1)
                cout<<'+'<<'i'<<endl;
            else if(p[1]==-1)
                cout<<'-'<<'i'<<endl;
            else
                cout<<endl;
        }
        else
        {
            if(p[1]>0&&p[1]!=1)
                cout<<p[1]<<'i'<<endl;
            else if(p[1]<0&&p[1]!=-1)
                cout<<p[1]<<'i'<<endl;
            else if(p[1]==1)
                cout<<'i'<<endl;
            else if(p[1]==-1)
                cout<<'-'<<'i'<<endl;
        }
    }
};

int main()
{
    double q,p,s,e;
    cin>>q>>p>>s>>e;
    complex a(q,p),b(s,e),c;
    char d;
    cin>>d;
    if(d=='-')
        c=a-b;
    else if(d=='+')
        c=a+b;
    else if(d=='*')
        c=a*b;
    c.show();
    return 0;
}

img

  • 写回答

1条回答 默认 最新

  • bostonAlen 2022-04-20 21:05
    关注

    这里尽量别用指针变量作为成员,会有很多问题(深拷贝等),就用两个成员,一个实部一个虚部就行了

    #define _CRT_SECURE_NO_WARNINGS
    
    #include<iostream>
    using namespace std;
    
    class Complex
    {
    public:
        Complex(double real = 8.0, double image = 6.0)       //构造函数
            :_real(real)
            , _image(image)
        {
            //cout << " Complex(double real, double image)" << endl;
        }
        Complex(const Complex& d)          //拷贝函数
        {
            //cout << "Complex(const Complex& d)" << endl;
            _real = d._real;
            _image = d._image;
        }
        ~Complex()       //析构函数
        {
            //cout << "~Complex() " << endl;
            _real = 0.0;
            _image = 0.0;
        }
        Complex& operator=(const Complex& d)         //赋值运算符重载
        {
            //cout << "=" << endl;
            if (this != &d)
            {
                _real = d._real;
                _image = d._image;
            }
            return *this;
        }
        void show()const                //打印复数
        {
            cout << _real << "+" << _image<<"i" << endl;
        }
    
        bool operator==(const Complex& d)             //==
        {
            cout << "==" << endl;
            return this->_real == d._real
                && this->_image == d._image;
        }
        bool operator!=(const Complex& d)             //!=
        {
            cout << "!=" << endl;
            return this->_real != d._real
                || this->_image == d._image;
        }
        //复数只有当虚部为0时,即_image=0时,才可以比较大小,这时比较的是实部即_real的大小
        bool operator>(const Complex& d)          //>
        {
            if (this->_image != 0 || d._image != 0)
            {
                cout << "无法比较       ";
                return false;
            }
    
            else
            {
                return this->_real > d._real;
            }
        }
        bool operator<(const Complex& d)           //<
        {
            if (this->_image != 0 || d._image != 0)
            {
                cout << "无法比较      ";
                return false;
            }
            else
            {
                return this->_real < d._real;
            }
        }
        bool operator<=(const Complex& d)           //<=
        {
            if (this->_image != 0 || d._image != 0)
            {
                cout << "无法比较      ";
                return false;
            }
            else
            {
                return this->_real <= d._real;
            }
        }
        bool operator>=(const Complex& d)           //>=
        {
            if (this->_image != 0 || d._image != 0)
            {
                cout << "无法比较        ";
                return false;
            }
            else
            {
                return this->_real >= d._real;
            }
        }
        Complex operator+ (const Complex& d)           //+
        {
            //cout << "+" << endl;
            Complex ret;
            ret._real = (this->_real + d._real);
            ret._image = (this->_image + d._image);
            return ret;
        }
        Complex operator- (const Complex& d)           //-
        {
            //cout << "-" << endl;
            Complex ret;
            ret._real = (this->_real - d._real);
            ret._image = (this->_image - d._image);
            return ret;
        }
        Complex operator* (const Complex& d)           //*
        {
            //cout << "*" << endl;
            Complex ret;
            ret._real = (this->_real * d._real);
            ret._image = (this->_image * d._image);
            return ret;
        }
        Complex operator / (const Complex& d)           //
        {
            //cout << "/" << endl;
            Complex ret;
            if (d._real == 0 || d._image) return d;
            ret._real = (this->_real / d._real);
            ret._image = (this->_image / d._image);
            return ret;
        }
        Complex& operator+=(const Complex& d)          //+=
        {
            cout << "+=" << endl;
            this->_real += d._real;
            this->_image += d._image;
            return *this;
        }
        Complex& operator++()            //前置++
        {
            cout << "前置++" << endl;
            this->_real += 1;
            return *this;
        }
        Complex operator++(int)         //后置++
        {
            cout << "后置++" << endl;
            Complex tmp(*this);
            this->_real += 1;
            return tmp;
        }
    
    
    protected:
        double _real;
        double _image;
    };
    
    void Test1()
    {
        Complex d1;
        Complex d2(4.0, 9.6);
        d1.show();
        d2.show();
        d1 += d2;
        d1.show();
        d2.show();
    }
    void Test2()
    {
        double q, p, s, e;
        cin >> q >> p >> s >> e;
        Complex a(q, p), b(s, e), c;
        char d;
        cin >> d;
        if (d == '-')
            c = a - b;
        else if (d == '+')
            c = a + b;
        else if (d == '*')
            c = a * b;
        c.show();
    
    }
    int main()
    {
        Test2();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月11日
  • 已采纳回答 5月3日
  • 创建了问题 4月20日

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址