m0_62391773 2021-11-26 14:30 采纳率: 33.3%
浏览 119
已结题

定义一个复数类,重载运算符“+”,“-”,“++”,“>”,使之用于复数的加、减、自加并比较两个复数大小;

定义一个复数类,重载运算符“+”,“-”,“++”,“>”,使之用于复数的加、减、自加并比较两个复数大小;

  • 写回答

2条回答 默认 最新

  • 关注

    代码如下:

    #include <iostream>
    using namespace std;
    class Complex
    {
    public:
        double real;
        double image;
    public:
        Complex(double r,double i)
        {
            real=r; image=i;
        }
        Complex(double r)
        {
            real=r; image=0;
        }
        Complex()
        {
            real=0; image=0;
        }
    
        Complex operator + (const Complex Right) 
        {
            Complex temp;
            temp.real = real + Right.real;
            temp.image = image + Right.image;
            return temp;
        }
    
        Complex operator - (const Complex Right) {
            Complex temp;
            temp.real = real - Right.real;
            temp.image = image - Right.image;
            return temp;
        }
        Complex operator ++(){
            this->real += 1;
            this->image += 1;
            return *this;
        }
    
        Complex operator --(){
            this->real -=1;
            this->image -=1;
            return *this;
        }
    
    
        Complex operator * (const Complex Right) {
            Complex temp;
            temp.real = real * Right.real - image * Right.image;
            temp.image = real * Right.image + image * Right.image;
            return temp;
        }
    
        Complex operator /(const Complex Right)
        {
            Complex temp;
            temp.real=real/Right.real;
            temp.image =image/Right.real;
            return temp;
        }
    
        int operator ==(const Complex Right)
        {
            if (this->real == Right.real && this->image < Right.image)
            {
                return 1;
            }else
                return 0;
        }
    
        int operator > (const Complex Right)
        {
            if(this->real > Right.real)
                return 1;
            else if (this->real == Right.real && this->image < Right.image)
            {
                return 1;
            }
            else if (this->real == Right.real && this->image == this->image)
            {
                return 0;
            }else
                return -1;
        }
    
        friend void print(Complex comp);
    
    };
    
    
    void print(Complex comp)
    {
        cout<< comp.real << " + "<<comp.image << "i"<<endl;
    }
    
    int main()
    {
        Complex c1(3.5,5.5);
        Complex c2(-3.5,1.0);
    
        Complex t1 = c1 + c2; 
        print(t1);
        Complex t2 = c1 - c2;
        print(t2);
        
        t2--;
        print(t2);
    
        t1++;
        print(t1);
    
        if(t1>t2)
            cout << "t1 > t2"<<endl;
        else if(t1==t2)
            cout <<"t1==t2"<<endl;
        else
            cout <<"t1<t2"<<endl;
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月4日
  • 已采纳回答 11月26日
  • 创建了问题 11月26日

悬赏问题

  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 三菱FX系列PLC串口指令
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型