C++上机题:编写一个复数四则运算计算器。

具体要求:*定义一个实数类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;
}

实现效果如下:
在这里插入图片描述

在这里插入图片描述

创作不易,感谢支持,欢迎大家在评论区交流!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值