定义复数类(Complex),包含必要的构造函数和析构函数,重载乘法赋值运算符“*=”实现复数与整数,复数与复数之间的乘法运算,自行举例并按格式输出

#要求达到的效果:
1.格式要求举例
c=2+3i
c*=5,c=10+15i
c*=4+5i,c=-7+22i
2.要求使用转换构造函数把整数转换为复数后进行运算
3.遇到的问题:数学逻辑不清晰
4.

using namespace std;

class Complex
{public:
Complex(){real=0;imag=0;}//定义构造函数
Complex(double r,double i){real=r;imag=i;}//构造函数重载
Complex operator*=(Complex &c2);
void display();
private:
    double real;
    double imag;
};
Complex Complex::operator*=(Complex &c1)
{Complex c;
    c.real=real*c1.real-imag*c1.imag;
    c.imag=real*c1.imag+imag*c1.real;
    
return c;}
void Complex::display()
{cout<<" "<<real<<"+"<<imag<<"i"<<endl;}
int main()
{Complex c1(2,3),c2=(4,5),c3;
c3=c1*=c2;
cout<<"c1*=";c1.display();
cout<<"c2*=";c2.display();
cout<<"c1*c2=";c3.display();
return 0;
}




img

#include <iostream>
using namespace std;
class Complex
{
public:
    int real;
    int image;
public:
    Complex(int r, int i)
    {
        real = r; image = i;
    }
    Complex(int r)
    {
        real = r; image = 0;
    }
    Complex()
    {
        real = 0; image = 0;
    }
 
    ~Complex()
    {
        //do nothing
    }
    Complex operator *= (const Complex Right) 
    {
        int a = real, b = image;
        real = a * Right.real - b * Right.image;
        image = b * Right.real + a * Right.image;
        return *this;
    }
 
    Complex operator *= (const int n) 
    {
        Complex t(n, 0);
        *this *= t;
        return *this;
    }
    friend void print(Complex comp);
 
};
 
 
void print(Complex comp)
{
    cout << comp.real;
    if (comp.image < 0)
        cout << comp.image << "i" << endl;
    else if (comp.image > 0)
        cout <<"+" << comp.image << "i" << endl;
}
 
int main()
{
    Complex c1(2, 3);
    Complex c2(2, 3);
    
    
    //显示C1
    cout<<"c=";
    print(c1);
 
    //与int类型相乘
    c1 *= 5;
    cout<<"c*=5,c=";
    print(c1);
 
    
    //与复数相乘
    cout<<"c*=4+5i,";
    Complex t(4, 5);
    c2 *= t;
    print(c2);
 
    return 0;
}

代码没啥大问题,做了点优化


#include<iostream>
using namespace std;

class Complex
{
public:
    Complex() { real = 0; imag = 0; }//定义构造函数
    Complex(double r, double i) { real = r; imag = i; }//构造函数重载
    Complex operator*=(Complex& c2);
    Complex operator*=(int i);//重载用于和整数相乘
    ~Complex() { cout << "bye\n"; }//析构函数,没有new分配的内存,构造函数无需做事,此处只是说明有
    void display();
private:
    double real;
    double imag;
};
Complex Complex::operator*=(Complex& c1)
{
    Complex c;
     //复数的乘法公式,复数c1(a+bi)和复数c2(c+di)相乘,结果为a*c-b*d+(b*c+a*d)i
    c.real = real * c1.real - imag * c1.imag;
    c.imag = real * c1.imag + imag * c1.real;

    return c;
}
//void Complex::display()
//{
//    cout << " " << real << "+" << imag << "i" << endl;
//}

//重载乘整数
Complex Complex::operator*=(int i)
{
  //整数分别乘虚实
    return Complex(real * i, imag * i);
}

void Complex::display()
{
    //考虑虚部为复数的情况
    if (imag < 0)
    {
        cout << real << imag << 'i' << endl;
    }
    else
    {
        cout << real << '+' << imag << 'i' << endl;
    }
}

int main()
{
    //Complex c1(2, 3), c2 = (4, 5), c3;  c2调用构造函数不对

    //获取输入
  /*  double a, b;
    cin >> a >> b;
    Complex c1(a, b);
    cin >> a >> b;
    Complex c2(a, b);*/

    Complex c1(2, 3), c2 = { 4,5 }, c3,c4;
    //c2调用了一次析构函数,先生成一个4,5的临时复数再赋值给c2
    c3 = c1 *= c2;
    c4 = c1 *= 3;
    //c3,c4都调用了一次析构函数(计算中的临时变量用到)
    cout << "c1*="; c1.display();
    cout << "c2*="; c2.display();
    cout << "c1*c2="; c3.display();
    cout << "c1*3="; c4.display();
    //c1,c2,c3,c4都调用一次
    return 0;
}


img

operator*= 最好返回引用,返回值的话多一次复制,效率不高