具体要求:*定义一个实数类Real,类中包括一个数据成员real,构造函数、display()函数以及重载的 + 、一、 * 、 / 运算符。用公有继承方式声明一个复数类Complex,该类包括两个数据成员real, image,并重载Real类的函数和 - 、一、 、/ 运算符。
重载这些操作符可以写为成员函数重载或者全局函数重载,而本文采取的方法是成员函数重载。
此外还要注意复数的乘除公式:
(a+bi)(c+di)=(ac-bd)+(bc+ad)i
(a+bi)/(c+di)=(ac+bd)/(c2+d2)+((bc-ad)/(c2+d2))i。
具体代码如下:
#include <iostream>
using namespace std;
class Real
{
public:
Real() { real = 0; };
Real(float real);
void display();
Real operator + (Real&R);
Real operator - (Real& R);
Real operator * (Real& R);
Real operator / (Real& R);
protected:
float real;
};
class Complex:public Real
{
public:
Complex() :Real() { image = 0; };
Complex(float real, float image);
void display();
Complex operator+(Complex& C);
Complex operator-(Complex& C);
Complex operator*(Complex& C);
Complex operator/(Complex& C);
protected:
float image;
};
Real::Real(float real)
{
this->real = real;
}
void Real::display()
{
cout << "real:" << real << endl;
}
Real Real::operator + (Real& R)
{
Real temp;
temp.real = this->real + R.real;
return temp;
}
Real Real::operator - (Real& R)
{
Real temp;
temp.real = this->real - R.real;
return temp;
}
Real Real::operator * (Real& R)
{
Real temp;
temp.real = this->real * R.real;
return temp;
}
Real Real::operator / (Real& R)
{
Real temp;
temp.real = this->real / R.real;
return temp;
}
Complex::Complex(float real, float image):Real(real)
{
this->image = image;
}
void Complex::display()
{
cout << "(" << real << "," << image << ")" << endl;
}
Complex Complex::operator+(Complex& C)
{
Complex temp;
temp.real = this->real + C.real;
temp.image = this->image + C.image;
return temp;
}
Complex Complex::operator-(Complex& C)
{
Complex temp;
temp.real = this->real - C.real;
temp.image = this->image - C.image;
return temp;
}
Complex Complex::operator*(Complex& C)
{
Complex temp;
temp.real = this->real * C.real-this->image*C.image;
temp.image = this->image * C.real+this->real*C.image;
return temp;
}
Complex Complex::operator/(Complex& C)
{
Complex temp;
temp.real = (real * C.real + image * C.image) / (C.real * C.real + C.image * C.image);
temp.image = (image * C.real - real * C.image) / (C.real * C.real + C.image * C.image);
return temp;
}
enum jisuan
{
tuichu,
sum,
jian,
cheng,
chu
};
void menu()
{
cout << "*****************************" << endl;
cout << "***** 1.加法 2.减法 ****" << endl;
cout << "***** 3.乘法 4.除法 ****" << endl;
cout << "***** 0.退出 ****" << endl;
}
int main()
{
Complex z;
float numberx1, numberx2, numbery1, numbery2;
int input;
do
{
cout << "请分别输入两个复数" << endl;
cin >> numberx1 >> numberx2 >> numbery1 >> numbery2;
Complex x(numberx1, numberx2), y(numbery1, numbery2);
menu();
cout << "请选择->:";
cin >> input;
switch (input)
{
case sum:
z = x.operator+(y);
cout << "相加的结果为:";
z.display();
system("pause");
system("cls");
break;
case jian:
z = x.operator-(y);
cout << "相减的结果为:";
z.display();
system("pause");
system("cls");
break;
case cheng:
z = x.operator*(y);
cout << "相乘的结果为:";
z.display();
system("pause");
system("cls");
break;
case chu:
z = x.operator/(y);
cout << "相除的结果为:";
z.display();
system("pause");
system("cls");
break;
case tuichu:
break;
default:cout << "输入错误,请重新输入" << endl;break;
}
} while (input);
return 0;
}
实现效果如下:
创作不易,感谢支持,欢迎大家在评论区交流!