5 条题解

  • 0
    @ 2024-08-27 20:18:04
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct Complex{
        int Re, Im, Mod;
    }c[5];
    bool cmp(Complex x, Complex y){
        return x.Mod < y.Mod;
    }
    int main(){
        for(int i = 1; i <= 3; i++){
            cin >> c[i].Re >> c[i].Im;
            c[i].Mod = c[i].Re*c[i].Re + c[i].Im*c[i].Im;
        }
        sort(c+1, c+4, cmp);
        for(int i = 1; i <= 3; i++){
            cout << c[i].Re;
            if(c[i].Im > 0)cout << '+';
            if(c[i].Im != 0)cout << c[i].Im << 'i';
            cout << endl;
        }
        return 0;
    }
    
  • 0
    #include <iostream>
    using namespace std;
    class Complex
    {
    private:
        int shibu;
        int xubu;
    
    public:
        Complex(int a, int b) : shibu(a), xubu(b) {}
    
        // 重载加法运算符
        Complex operator+(const Complex& c2)
        {
            return Complex(this->shibu + c2.shibu, this->xubu + c2.xubu);
        }
    
        // 重载乘法运算符
        Complex operator*(const Complex& c2)
        {
            return Complex(this->shibu * c2.shibu - this->xubu * c2.xubu, this->shibu * c2.xubu + this->xubu * c2.shibu);
        }
    
        void PrintComplex()
        {
            cout << shibu;
            if (xubu > 0)
                cout << "+" << xubu << "i" << endl;
            else if (xubu < 0)
                cout << "-" << -xubu << "i" << endl;
        }
        bool operator>(const Complex& c2)
        {
            return (this->shibu * this->shibu + this->xubu * this->xubu >
                c2.shibu * c2.shibu + c2.xubu * c2.xubu);
        }
        bool operator<(const Complex& c2)
        {
            return (this->shibu * this->shibu + this->xubu * this->xubu <
                c2.shibu * c2.shibu + c2.xubu * c2.xubu);
        }
        bool operator==(const Complex& c2)
        {
            return (this->shibu * this->shibu + this->xubu * this->xubu ==
                c2.shibu * c2.shibu + c2.xubu * c2.xubu);
        }
        Complex& operator=(const Complex& c2)
        {
            if (this != &c2)//赋值前要确保不等于自身 否则则会造成对象数据的损坏
            {
                this->shibu = c2.shibu;
                this->xubu = c2.xubu;
            }
            return *this; //也就是返回当前对象
        }
        int GetShibu() { return shibu; }//留一个只给知道值的外部接口
        int GetXubu() { return xubu; }
    };
    void test01(Complex c1,Complex c2,Complex c3) //类似与int a
    {
        Complex temp1 = c1 + c2 + c3;
        temp1.PrintComplex();
    
        Complex temp2 = c1 * c2 * c3;
        temp2.PrintComplex();
    
    }//标准的测试程序
    
    void test02(Complex c1, Complex c2, Complex c3)
    {
        Complex c[3] = { c1, c2, c3 };//类似于int A[3]={a1,a2,a3};
        // 冒泡排序
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2 - i; j++)
            {
                if (c[j] > c[j + 1])
                {
                    Complex temp = c[j];
                    c[j] = c[j + 1];
                    c[j + 1] = temp;
                }
            }
        }
        //输出函数
        for (int i = 0;i < 3;i++)
        {
            cout << c[i].GetShibu();
            if(c[i].GetXubu()>0){cout<<"+" << c[i].GetXubu()<<"i"; }
            else if (c[i].GetXubu() < 0) { cout << c[i].GetXubu() << "i"; }
            cout << endl;
    
        }
    }
        int main()
     {
        int a1, b1, a2, b2, a3, b3;
        cin >> a1 >> b1;
        cin >> a2 >> b2;
        cin >> a3 >> b3;
        Complex c1(a1, b1);
        Complex c2(a2, b2);
        Complex c3(a3, b3);
        test02(c1,c2,c3);//标准的传参
    
        return 0;}
    
  • 0
    @ 2019-03-31 13:27:01
    #include <iostream>
    #include <math.h>
    using namespace std;
    class Complex
    {
        float real, image;
    public:
        Complex()
        {
    
        }
        Complex(float r, float i)
        {
            real = r;
            image = i;
        }
        double Modulo()
        {
            return sqrt(real * real + image * image);
        }
        friend ostream& operator<<(ostream &o, Complex &c)
        {
            if (c.image > 0)
                return o << c.real << "+" << c.image << "i" << endl;
            else
            {
                if (c.image == 0)
                    return o << c.real << endl;
                else
                    return o << c.real << c.image << "i" << endl;
            }
                
        }
    };
    int main()
    {
        int r1, r2, r3, i1, i2, i3;
        cin >> r1 >> i1 >> r2 >> i2 >> r3 >> i3;
        Complex c1(r1, i1), c2(r2, i2), c3(r3, i3);
        if (c1.Modulo() <= c2.Modulo())
        {
            if (c2.Modulo() <= c3.Modulo())
            {
                cout << c1 << c2 << c3;
            }
            else
            {
                if (c1.Modulo() <= c3.Modulo())
                {
                    cout << c1 << c3 << c2;
                }
                else
                {
                    cout << c3 << c1 << c2;
                }
            }
        }
        else
        {
            if (c1.Modulo() <= c3.Modulo())
            {
                cout << c2 << c1 << c3;
            }
            else
            {
                if (c2.Modulo() < c3.Modulo())
                {
                    cout << c2 << c3 << c1;
                }
                else
                {
                    cout << c3 << c2 << c1;
                }
            }
        }
    
        return 0;
    }
    
  • -1
    @ 2019-06-16 22:35:37

    #include <iostream>
    using namespace std;
    class Complex
    {
    int a, b;
    public:
    Complex(int x = 0, int y = 0)
    {
    a = x;
    b = y;
    }

    int operator<(const Complex& c2)
    {
    if ((a*a + b * b)<(c2.a*c2.a + c2.b*c2.b))
    return 1;
    else
    return 0;
    }

    Complex operator=(const Complex &c2)
    {
    a = c2.a;
    b = c2.b;
    return *this;
    }

    void print()
    {
    if (b>0)
    cout << a << "+" << b << "i" << endl;
    if (b<0)
    cout << a << b << "i" << endl;
    if (b == 0)
    cout << a << endl;
    }
    };

    int main()
    {
    int a1, b1, a2, b2, a3, b3;
    cin >> a1 >> b1;
    cin >> a2 >> b2;
    cin >> a3 >> b3;
    Complex c1(a1, b1);
    Complex c2(a2, b2);
    Complex c3(a3, b3);
    Complex t;
    if (!(c1 < c2))
    {
    t = c1;
    c1 = c2;
    c2 = t;
    }
    if (!(c2 < c3))
    {
    t = c2;
    c2 = c3;
    c3 = t;
    }
    if (!(c1 < c2))
    {
    t = c1;
    c1 = c2;
    c2 = t;
    }
    c1.print();
    c2.print();
    c3.print();
    }

  • -2

    #include <iostream>
    #include <math.h>
    using namespace std;
    class Complex
    {
    float real, image;
    public:
    Complex()
    {

    }
    Complex(float r, float i)
    {
    real = r;
    image = i;
    }
    double Modulo()
    {
    return sqrt(real * real + image * image);
    }
    friend ostream& operator<<(ostream &o, Complex &c)
    {
    if (c.image > 0)
    return o << c.real << "+" << c.image << "i" << endl;
    else
    {
    if (c.image == 0)
    return o << c.real << endl;
    else
    return o << c.real << c.image << "i" << endl;
    }

    }
    };
    int main()
    {
    int r1, r2, r3, i1, i2, i3;
    cin >> r1 >> i1 >> r2 >> i2 >> r3 >> i3;
    Complex c1(r1, i1), c2(r2, i2), c3(r3, i3);
    if (c1.Modulo() <= c2.Modulo())
    {
    if (c2.Modulo() <= c3.Modulo())
    {
    cout << c1 << c2 << c3;
    }
    else
    {
    if (c1.Modulo() <= c3.Modulo())
    {
    cout << c1 << c3 << c2;
    }
    else
    {
    cout << c3 << c1 << c2;
    }
    }
    }
    else
    {
    if (c1.Modulo() <= c3.Modulo())
    {
    cout << c2 << c1 << c3;
    }
    else
    {
    if (c2.Modulo() < c3.Modulo())
    {
    cout << c2 << c3 << c1;
    }
    else
    {
    cout << c3 << c2 << c1;
    }
    }
    }

    return 0;
    }

  • 1

OO2-2 复数类的比较运算符重载

信息

ID
1012
难度
3
分类
(无)
标签
递交数
328
已通过
156
通过率
48%
被复制
4
上传者