题目描述
定义一个复数类,通过重载运算符:+、-、*,实现两个复数之间的各种运算。
class Complex
{
private:
float real, image;
public:
Complex(float x = 0, float y = 0);
friend Complex operator+(Complex&, Complex&);
friend Complex operator-(Complex&, Complex&);
friend Complex operator*(Complex&, Complex&);
void show();
};
要求如下:
1.实现Complex类;
2.编写main函数,初始化两个Complex对象,计算它们之间的加减乘,并输出结果。
复数相乘的运算规则
设z1=a+bi,z2=c+di(a、b、c、d∈R)是任意两个复数,那么它们的积(a+bi)(c+di)=(ac-bd)+(bc+ad)i.
输入
第1行:输入两个数值,分别为第一个Complex对象的实部和虚部。
第2行:输入两个数值,分别为第二个Complex对象的实部和虚部。
输出
第1行:两个Complex对象相加后的输出结果。
第2行:两个Complex对象相减后的输出结果。
第3行:两个Complex对象相乘后的输出结果。
#include<iostream>
#include <cstring>
using namespace std;
class Complex {
float Real, Image;
public:
Complex(float x = 0, float y = 0) {
Real = x;
Image = y;
}
friend Complex operator+(Complex&, Complex&);
friend Complex operator-(Complex&, Complex&);
friend Complex operator*(Complex&, Complex&);
void show() {
cout << "Real=" << Real << " Image=" << Image << endl;
}
};
//运算符重载
//创建新对象 其real =c1+c2的real
Complex operator+(Complex& c1, Complex& c2) {
Complex temp;
temp.Real = c1.Real + c2.Real;
temp.Image = c1.Image + c2.Image;
return temp;
}
//减法
Complex operator-(Complex& c1, Complex& c2) {
Complex temp;
temp.Real = c1.Real - c2.Real;
temp.Image = c1.Image - c2.Image;
return temp;
}
//乘法
Complex operator*(Complex& c1, Complex& c2) {
Complex temp;
temp.Real = c1.Real * c2.Real - c1.Image * c2.Image;
temp.Image = c1.Image * c2.Real + c1.Real * c2.Image;
return temp;
}
int main() {
float real, image;
//创建两个对象
cin >> real >> image;
Complex c1(real, image);
cin >> real >> image;
Complex c2(real, image);
Complex c3;
c3 = c1 + c2;
c3.show();
c3 = c1 - c2;
c3.show();
c3 = c1 * c2;
c3.show();
}