1 条题解

  • 2
    @ 2020-03-10 22:43:44

    此处摘录做得较好的同学的答案

    @乔南

    //姓名:乔南 学号:201930100499 
    #include <iostream>
    using namespace std;
    class Complex
    {
        public:
            Complex(double x=0.0,double y=0.0):real(x),imag(y){}
            friend Complex operator+(Complex,Complex);
            friend Complex operator-(Complex,Complex);
            friend Complex operator*(Complex,Complex);
            friend bool operator==(Complex,Complex);
            friend bool operator!=(Complex,Complex);
            friend istream &operator>>(istream &,Complex&);
            friend ostream &operator<<(ostream &,Complex);
        private:
            double real;
            double imag;
    };
    istream &operator>>(istream &input,Complex &x)
    {
        input>>x.real;
        input>>x.imag;
        return input;
    }
    ostream &operator<<(ostream &output,Complex x)
    {
        output<<x.real;
        if(x.imag>=0)output<<"+";
        if(x.imag==-1)output<<"-";
        if(x.imag!=1&&x.imag!=-1)output<<x.imag;
        output<<"i";
        return output;
    }
    Complex operator+(Complex x1,Complex x2)
    {
        Complex x;
        x.real=x1.real+x2.real;
        x.imag=x1.imag+x2.imag;
        return x;
    }
    Complex operator-(Complex x1,Complex x2)
    {
        Complex x;
        x.real=x1.real-x2.real;
        x.imag=x1.imag-x2.imag;
        return x;
    }
    Complex operator*(Complex x1,Complex x2)
    {
        Complex x;
        x.real=x1.real*x2.real-x1.imag*x2.imag;
        x.imag=x1.real*x2.imag+x1.imag*x2.real;
        return x;
    }
    bool operator==(Complex x1,Complex x2)
    {
        return (x1.real==x2.real)&&(x1.imag==x2.imag);
    }
    bool operator!=(Complex x1,Complex x2)
    {
        return !(x1==x2);
    }
    int main()
    {
        Complex x1;
        Complex x2;
        cin>>x1;
        cin>>x2;
        cout<<x1+x2<<endl;
        cout<<x1-x2<<endl;
        cout<<x1*x2<<endl;
        if(x1==x2)cout<<"a==b";
        else cout<<"a!=b";
    }
    
  • 1

信息

ID
1001
难度
1
分类
(无)
标签
递交数
120
已通过
31
通过率
26%
上传者