复数之运算符重载

#include<iostream>
using namespace std;
class Complex   //复数类
{
public:
	double real, image;

public:
	Complex(double r = 0, double i = 0) { real = r; image = i; };

	

	Complex operator+(const Complex& c)   //复数加复数
	{
		Complex temp;
		temp.real = this->real + c.real;
		temp.image = this->image + c.image;
		return temp;
	}
	
	Complex operator-(const Complex& c)   //复数减复数
	{
		Complex temp;
		temp.real = this->real - c.real;
		temp.image = this->image - c.image;
		return temp;
	}

	Complex operator*(const 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 operator/(const Complex& c)   //复数除复数
	{
		Complex temp;
		temp.real = (this->real * c.real + this->image * c.image) / (c.real * c.real + c.image * c.image);
		temp.image=(this->image * c.real - this->real * c.image)/ (c.real * c.real + c.image * c.image);
		return temp;
	}

	Complex operator+(const double& n)   //复数加实数
	{
		Complex temp;
		temp.real = this->real + n;
		temp.image = this->image;
		return temp;
	}

	Complex operator-(const double& n)   //复数减实数
	{
		Complex temp;
		temp.real = this->real - n;
		temp.image = this->image;
		return temp;
	}

	Complex operator*(const double& n)   //复数乘实数
	{
		Complex temp;
		temp.real = this->real * n;
		temp.image = this->image * n;
		return temp;
	}

	Complex operator/(const double& n)   //复数除实数
	{
		Complex temp;
		temp.real = this->real / n;
		temp.image = this->image / n;
		return temp;
	}

	

};

ostream& operator<<(ostream& cout, const Complex& c) 
{
	if (c.image != 0)
		cout << c.real << " + " << c.image << "i";
	else
	{
		cout << c.real;
	}
	return cout;
}

Complex operator+(const double& n, const Complex& c)   //实数加复数
{
	Complex temp;
	temp.real = n + c.real;
	temp.image = c.image;
	return temp;
}

Complex operator-(const double& n, const Complex &c)   //实数减复数
{
	Complex temp;
	temp.real = n - c.real;
	temp.image = -c.image;
	return temp;
}

Complex operator*(const double& n, const Complex& c)   //实数乘复数
{
	Complex temp;
	temp.real = n * c.real;
	temp.image = n * c.image;
	return temp;
}

Complex operator/(const double& n, const Complex &c)   //实数除复数
{
	Complex temp;
	temp.real = n * c.real / c.real * c.real + c.image * c.image;
	temp.image = -n * c.image / c.real * c.real + c.image * c.image;
	return temp;
}

void fJiaf()   //复数加复数
{
	double r1, i1, r2, i2;
	cout << "请输入第一个复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	cout << "请输入第二个复数的实部和虚部" << endl;
	cin >> r2 >> i2;
	Complex c1(r1, i1), c2(r2, i2), c;
	c = c1 + c2;
	cout << "c=" << c1 + c2 << endl;
	system("pause");
	system("cls");
}

void fJianf()   //复数减复数
{
	double r1, i1, r2, i2;
	cout << "请输入第一个复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	cout << "请输入第二个复数的实部和虚部" << endl;
	cin >> r2 >> i2;
	Complex c1(r1, i1), c2(r2, i2), c;
	c = c1 - c2;
	cout << "c=" << c1 - c2 << endl;
	system("pause");
	system("cls");
}

void fChengf()   //复数乘复数
{
	double r1, i1, r2, i2;
	cout << "请输入第一个复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	cout << "请输入第二个复数的实部和虚部" << endl;
	cin >> r2 >> i2;
	Complex c1(r1, i1), c2(r2, i2), c;
	c = c1 * c2;
	cout << "c=" << c1 * c2 << endl;
	system("pause");
	system("cls");
}

void fChuf()   //复数除复数
{
	double r1, i1, r2, i2;
	cout << "请输入第一个复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	cout << "请输入第二个复数的实部和虚部" << endl;
	cin >> r2 >> i2;
	Complex c1(r1, i1), c2(r2, i2), c;
	cout << "c=" << c1 / c2 << endl;
	system("pause");
	system("cls");
}

void fJias()   //复数加实数
{
	double r1, i1, n;
	cout << "请输入复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	cout << "请输入一个实数" << endl;
	cin >> n;
	Complex c(r1, i1);
	cout << "c=" << c + n << endl;
	system("pause");
	system("cls");
}

void fJians()   //复数减实数
{
	double r1, i1, n;
	cout << "请输入复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	cout << "请输入一个实数" << endl;
	cin >> n;
	Complex c(r1, i1);
	cout << "c=" << c - n << endl;
	system("pause");
	system("cls");
}

void fChengs()   //复数乘实数
{
	double r1, i1, n;
	cout << "请输入复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	cout << "请输入一个实数" << endl;
	cin >> n;
	Complex c(r1, i1);
	cout << "c=" << c * n << endl;
	system("pause");
	system("cls");
}

void fChus()   //复数除实数
{
	double r1, i1, n;
	cout << "请输入复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	cout << "请输入一个实数" << endl;
	cin >> n;
	if (n != 0)
	{
		Complex c(r1, i1);
		cout << "c=" << c / n << endl;
	}
	else
	{
		cout << "除数不能为0!" << endl;
	}
	system("pause");
	system("cls");
}

void sJiaf()   //实数加复数
{
	double r1, i1, n;
	cout << "请输入一个实数" << endl;
	cin >> n;
	cout << "请输入复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	Complex c(r1, i1);
	cout << "c=" << n + c << endl;
	system("pause");
	system("cls");
}

void sJianf()   //实数减复数
{
	double r1, i1, n;
	cout << "请输入一个实数" << endl;
	cin >> n;
	cout << "请输入复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	Complex c(r1, i1);
	cout << "c=" << n - c << endl;
	system("pause");
	system("cls");
}

void sChengf()   //实数乘复数
{
	double r1, i1, n;
	cout << "请输入一个实数" << endl;
	cin >> n;
	cout << "请输入复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	Complex c(r1, i1);
	cout << "c=" << n * c << endl;
	system("pause");
	system("cls");
}

void sChuf()   //实数除复数
{
	double r1, i1, n;
	cout << "请输入一个实数" << endl;
	cin >> n;
	cout << "请输入复数的实部和虚部" << endl;
	cin >> r1 >> i1;
	if (r1 == 0 && i1 == 0)
	{
		cout << "除数不能为0!" << endl;
	}
	else
	{
		Complex c(r1, i1);
		cout << "c=" << n / c << endl;
	}
	system("pause");
	system("cls");
}

void Menushow()   //菜单
{
	cout << "1.复数加复数" << "   " << "2.复数减复数" << endl;
	cout << "3.复数乘复数" << "   " << "4.复数除复数" << endl;
	cout << "5.复数加实数" << "   " << "6.复数减实数" << endl;
	cout << "7.复数乘实数" << "   " << "8.复数除实数" << endl;
	cout << "9.实数加复数" << "   " << "10.实数减复数" << endl;
	cout << "11.实数乘复数" << "   " << "12.实数除复数" << endl;
}

int main()
{
	int select;
	while (1)
	{
		Menushow();
		cin >> select;
		switch (select)
		{
		case 1:
			fJiaf();
			break;
		case 2:
			fJianf();
			break;
		case 3:
			fChengf();
			break;
		case 4:
			fChuf();
			break;
		case 5:
			fJias();
			break;
		case 6:
			fJians();
			break;
		case 7:
			fChengs();
			break;
		case 8:
			fChus();
			break;
		case 9:
			sJiaf();
			break;
		case 10:
			sJianf();
			break;
		case 11:
			sChengf();
			break;
		case 12:
			sChuf();
			break;
		}
	}
	return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的复数类可以通过运算符重载实现复数的加减乘除等运算。以下是一个示例代码,演示了如何重载复数类的运算符: ```c++ #include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } Complex operator-(const Complex& other) const { return Complex(real - other.real, imag - other.imag); } Complex operator*(const Complex& other) const { return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real); } Complex operator/(const Complex& other) const { double denominator = other.real * other.real + other.imag * other.imag; return Complex((real * other.real + imag * other.imag) / denominator, (imag * other.real - real * other.imag) / denominator); } friend ostream& operator<<(ostream& os, const Complex& c) { os << c.real << " + " << c.imag << "i"; return os; } }; int main() { Complex c1(1, 2); Complex c2(3, 4); cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl << endl; cout << "c1 + c2 = " << c1 + c2 << endl; cout << "c1 - c2 = " << c1 - c2 << endl; cout << "c1 * c2 = " << c1 * c2 << endl; cout << "c1 / c2 = " << c1 / c2 << endl; return 0; } ``` 在上述代码中,我们重载复数类的加、减、乘、除四个运算符,通过重载运算符实现了复数的运算。同时,我们还重载了输出运算符,用于输出复数对象的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值