C++ Complex类 重载运算符 + - * /

设两个复数为 a = x_1 + y_1i, b = x_2 + y_2i

公式如下:

a + b = (x_1 + x_2) + (y_1 + y_2)i

a - b = (x_1 - x_2) + (y_1 - y_2)i;

a * b = (x_1 + y_1i) * (x_2 + y_2i) = (x_1*x_2 - y_1*y_2) + (x_1 * y_2 - y_1 * x_2)i;

a / b = (x_1 + y_1i) / (x_2 + y_2i) = (x_1 * x_2 + y_1 * y_2) / (x_2 * x_2 + y_2 * y_2) + [(x_2 * y_1 - x_1 * y_2) / (x_2 * x_2 + y_2 * y_2)]i;

程序如下:

#include <iostream>

using namespace std;

class Complex {
public:
	double real, imag;
	Complex():real(0),imag(0) {}//无参构造函数
	Complex(double r, double i):real(r),imag(i){}// 用参数初始化表对其数据成员初始化
	void display();
};

//重载运算符+ (重载为普通函数)
Complex operator+ (const Complex& a, const Complex& b)
{
	return Complex(a.real + b.real, a.imag + b.imag); //返回一个临时对象
}

//重载运算符- (重载为普通函数)
Complex operator- (const Complex& a, const Complex& b)
{
	return Complex(a.real - b.real, a.imag - b.imag); 	//返回一个临时对象
}

//重载运算符* (重载为普通函数)
Complex operator*(const Complex& a, const Complex& b)
{
	Complex c;
	c.real = a.real * b.real - a.imag * b.imag;
	c.imag = a.real * b.imag + a.imag * b.real;
	return c;
}

//重载运算符/ (重载为普通函数)
Complex operator/(const Complex& a, const Complex& b)
{
	Complex c;
	c.real = (a.real * b.real + a.imag * b.imag) / (b.real * b.real + b.imag * b.imag);
	c.imag = (b.real * a.imag - a.real * b.imag) / (b.real * b.real + b.imag * b.imag);
	return c;
}

void Complex::display()
{
	if (imag < 0) {
		cout << real << " - " << -imag << "i" << endl;
	}
	else {
		cout << real << " + " << imag << "i" << endl;
	}
}

int main()
{
	Complex a(3, 4), b(5, -10), c1, c2, c3, c4;

	c1 = a + b;
	c2 = a - b;
	c3 = a * b;
	c4 = a / b;

	cout << "c1 = a + b = ";c1.display();

	cout << "c2 = a - b = "; c2.display();

	cout << "c3 = a * b = "; c3.display();

	cout << "c4 = a / b = "; c4.display();

	return 0;
}

 重载为成员函数

class Complex {
public:
	double real, imag;
	Complex():real(0),imag(0) {}//无参构造函数
	Complex(double r, double i):real(r),imag(i){}//用参数初始化表对其数据成员初始化
	Complex operator+(const Complex& a){
		return Complex(real + a.real, imag + a.imag);
	}
	Complex operator-(const Complex& a) {
		return Complex(real - a.real, imag - a.imag);
	}
	Complex operator*(const Complex& a) {
		return Complex(real * a.real - imag * a.imag, real * a.imag + imag * a.real);
	}
	Complex operator/(const Complex& a) {
		Complex c;
		c.real = (real * a.real + imag * a.imag) / (a.real * a.real + a.imag * a.imag);
		c.imag = (a.real * imag - real * a.imag) / (a.real * a.real + a.imag * a.imag);
		return c;
	}
	void display() {
		if (imag < 0) {
			cout << real << " - " << -imag << "i" << endl;
		}
		else {
			cout << real << " + " << imag << "i" << endl;
		}
	}
};

 运行结果

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个需要编写代码的问题,以下是一个可能的实现: ```c++ #include <iostream> using namespace std; class Complex { private: double real, imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 构造函数 ~Complex() {} // 析构函数 Complex operator+(const Complex &c) const { // 重载运算符+ return Complex(real + c.real, imag + c.imag); } Complex operator-(const Complex &c) const { // 重载运算符- return Complex(real - c.real, imag - c.imag); } Complex operator*(const Complex &c) const { // 重载运算符* return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); } Complex operator/(const Complex &c) const { // 重载运算符/ double r = c.real * c.real + c.imag * c.imag; return Complex((real * c.real + imag * c.imag) / r, (imag * c.real - real * c.imag) / r); } Complex operator-() const { // 重载运算符- return Complex(-real, -imag); } bool operator==(const Complex &c) const { // 重载运算符== return real == c.real && imag == c.imag; } friend Complex operator+(const Complex &c, double r) { // 重载运算符+,复数加实数 return Complex(c.real + r, c.imag); } friend Complex operator+(double r, const Complex &c) { // 重载运算符+,实数加复数 return Complex(c.real + r, c.imag); } friend ostream &operator<<(ostream &os, const Complex &c) { // 重载运算符<<,输出复数 os << c.real << " + " << c.imag << "i"; return os; } }; int main() { Complex c1(1, 2), c2(3, 4); cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << 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; cout << "-c1 = " << -c1 << endl; cout << "c1 + 1.2 = " << c1 + 1.2 << endl; cout << "1.2 + c1 = " << 1.2 + c1 << endl; cout << "c1 == c2 ? " << (c1 == c2 ? "true" : "false") << endl; cout << "c1 == c1 ? " << (c1 == c1 ? "true" : "false") << endl; return 0; } ``` 运行结果: ``` c1 = 1 + 2i c2 = 3 + 4i c1 + c2 = 4 + 6i c1 - c2 = -2 - 2i c1 * c2 = -5 + 10i c1 / c2 = 0.44 + 0.08i -c1 = -1 - 2i c1 + 1.2 = 2.2 + 2i 1.2 + c1 = 2.2 + 2i c1 == c2 ? false c1 == c1 ? true ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值