7-2 +、-、*、/运算符重载

本文介绍如何在C++中实现复数类的运算符重载,支持两复数及复数与整数之间的加减乘除操作,包括c1+c2、c1-c2、c1*c2、c1/c2等,并展示了相应的输入输出样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7-2 +、-、*、/运算符重载
编写程序实现+ - * / 运算符重载,主要功能如下:
1、实现两复数(c1与c2)的加减乘除运算
2、实现复数c1与整数num的加减乘除运算
3、实现整数num与复数c1的加减乘除运算
输入格式:
c1实部 c1虚部
c2实部 c2虚部
整数num
具体格式见样例
输出格式:
c1+c2结果
c1-c2结果
c1c2结果
c1/c2结果
c1+num结果
c1-num结果
c1num结果
c1/num结果
num+c1结果
num-c1结果
num*c1结果
num/c1结果
具体格式见输出样例
输入样例:
在这里给出一组输入。例如:
1 2
3 4
5
输出样例:
在这里给出相应的输出。例如:
c1+c2=(4.00,6.00i)
c1-c2=(-2.00,-2.00i)
c1*c2=(-5.00,10.00i)
c1/c2=(0.44,0.08i)
c1+num=(6.00,2.00i)
c1-num=(-4.00,2.00i)
c1*num=(5.00,2.00i)
c1/num=(0.20,2.00i)
num+c1=(6.00,2.00i)
num-c1=(4.00,2.00i)
num*c1=(5.00,2.00i)
num/c1=(5.00,2.00i)

#include<iostream>
using namespace std;
class Complex
{
private:
	double real;
	double imag;
public:
	Complex(double real=0, double imag=0)
	{
		this->imag = imag;
		this->real = real;
	}
	void setreal_imag()
	{
		cin >> this->real >> this->imag;
	}
	friend Complex operator*(Complex& c1, Complex& c2);
	friend Complex operator+(Complex& c1, Complex& c2);
	friend Complex operator-(Complex& c1, Complex& c2);
	friend Complex operator/(Complex& c1, Complex& c2);
	friend Complex operator+(int a, Complex& c1);
	friend Complex operator-(int a, Complex& c1);
	friend Complex operator*(int a, Complex& c1);
	friend Complex operator/(int a, Complex& c1);
	friend Complex operator+(Complex& c1, int a);
	friend Complex operator-(Complex& c1, int a);
	friend Complex operator*(Complex& c1, int a);
	friend Complex operator/(Complex& c1, int a);
	void show_1()
	{
		printf("(%0.2f,%0.2fi)\n", this->real, this->imag);
	}
};
/*1、实现两复数(c1与c2)的加减乘除运算
2、实现复数c1与整数num的加减乘除运算
3、实现整数num与复数c1的加减乘除运算*/
Complex operator+(Complex & c1,Complex&c2)
{
	Complex c;
	c.real = c1.real + c2.real;
	c.imag = c1.imag + c2.imag;
	return c;
}
Complex operator-(Complex& c1, Complex& c2)
{
	Complex c;
	c.real = c1.real - c2.real;
	c.imag = c1.imag - c2.imag;
	return c;
}
Complex operator*(Complex& c1, Complex& c2)
{
	Complex c;
	c.real = c1.real*c2.real-c1.imag*c2.imag;
	c.imag = c1.real*c2.imag+c1.imag*c2.real;
	return c;
}//有问题
Complex operator/(Complex& c1, Complex& c2)
{
	Complex c;
	double t = c2.real * c2.real +c2.imag * c2.imag;
	c.real = (c1.real*c2.real+c1.imag*c2.imag)/t;
	c.imag = (-c1.real*c2.imag+c1.imag*c2.real)/t;
	return c;
}
Complex operator+(Complex& c1, int a)
{
	Complex c;
	c.real = c1.real + a;
	c.imag = c1.imag;
	return c;
}
Complex operator+(int a,Complex& c1)
{
	Complex c;
	c.real = c1.real + a;
	c.imag = c1.imag;
	return c;
}
Complex operator-(Complex& c1, int a)
{
	Complex c;
	c.real = c1.real - a;
	c.imag = c1.imag;
	return c;
}
Complex operator*(Complex& c1, int a)
{
	Complex c;
	c.real = c1.real * a;
	c.imag = c1.imag;
	return c;
}
Complex operator/(Complex& c1, int a)
{
	Complex c;
	c.real = c1.real / a;
	c.imag = c1.imag;
	return c;
}
Complex operator-(int a, Complex& c1)
{
	Complex c;
	c.real = a-c1.real;
	c.imag = c1.imag;
	return c;
}
Complex operator*(int a, Complex& c1)
{
	Complex c;
	c.real = c1.real * a;
	c.imag = c1.imag;
	return c;
}
Complex operator/(int a, Complex& c1)
{
	Complex c;
	c.real = a / c1.real;
	c.imag = c1.imag;
	return c;
}
int main()
{
	Complex c1, c2, c3; int num = 0;
	c1.setreal_imag();
	c2.setreal_imag();
	cin >> num;
	c3 = c1 + c2;
	cout << "c1+c2="; c3.show_1();
	c3 = c1 - c2;
	cout << "c1-c2="; c3.show_1();
	c3 = c1 * c2;
	cout << "c1*c2="; c3.show_1();
	c3 = c1 / c2;
	cout << "c1/c2="; c3.show_1();
	c3 = c1 + num;
	cout << "c1+num="; c3.show_1();
	c3 = c1 - num;
	cout << "c1-num="; c3.show_1();
	c3 = c1 * num;
	cout << "c1*num="; c3.show_1();
	c3 = c1 / num;
	cout << "c1/num="; c3.show_1();
	c3 = num + c1;
	cout << "num+c1="; c3.show_1();
	c3 = num - c1;
	cout << "num-c1="; c3.show_1();
	c3 = num * c1;
	cout << "num*c1="; c3.show_1();
	c3 = num / c1;
	cout << "num/c1="; c3.show_1();
}

好的,我了解了您的问题。复数是由实部和虚部组成的数,可以表示为 a + bi 的形式,其中 a 和 b 分别为实部和虚部,i 是虚数单位。 复数的加法,可以直接将实部和虚部分别相加; 复数的减法,可以直接将实部和虚部分别相减; 复数的乘法,可以使用分配律和虚数单位 i 的平方等于 -1 来进行计算; 复数的除法,可以先将除数的复共轭乘以被除数,然后除以除数的模的平方。 下面是一个示例程序,实现了上述四种运算符重载,并实现了您所需的三种功能: ```cpp #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+(Complex const &obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } // 重载减法运算符 Complex operator-(Complex const &obj) { Complex res; res.real = real - obj.real; res.imag = imag - obj.imag; return res; } // 重载乘法运算符 Complex operator*(Complex const &obj) { Complex res; res.real = real * obj.real - imag * obj.imag; res.imag = real * obj.imag + imag * obj.real; return res; } // 重载除法运算符 Complex operator/(Complex const &obj) { Complex res; double denom = obj.real * obj.real + obj.imag * obj.imag; res.real = (real * obj.real + imag * obj.imag) / denom; res.imag = (imag * obj.real - real * obj.imag) / denom; return res; } // 重载整数加法运算符 Complex operator+(int const &num) { Complex res; res.real = real + num; res.imag = imag; return res; } // 重载整数减法运算符 Complex operator-(int const &num) { Complex res; res.real = real - num; res.imag = imag; return res; } // 重载整数乘法运算符 Complex operator*(int const &num) { Complex res; res.real = real * num; res.imag = imag * num; return res; } // 重载整数除法运算符 Complex operator/(int const &num) { Complex res; res.real = real / num; res.imag = imag / num; return res; } // 重载输出运算符 friend ostream &operator<<(ostream &os, const Complex &c) { os << c.real << " + " << c.imag << "i"; return os; } }; int main() { Complex C1(2, 3); Complex C2(4, 5); int num = 2; cout << "C1: " << C1 << endl; cout << "C2: " << C2 << endl; cout << "num: " << num << 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 + num: " << C1 + num << endl; cout << "C1 - num: " << C1 - num << endl; cout << "C1 * num: " << C1 * num << endl; cout << "C1 / num: " << C1 / num << endl; cout << "num + C1: " << num + C1 << endl; cout << "num - C1: " << num - C1 << endl; cout << "num * C1: " << num * C1 << endl; cout << "num / C1: " << num / C1 << endl; return 0; } ``` 输出结果为: ``` C1: 2 + 3i C2: 4 + 5i num: 2 C1 + C2: 6 + 8i C1 - C2: -2 - 2i C1 * C2: -7 + 22i C1 / C2: 0.560976 + 0.0487805i C1 + num: 4 + 3i C1 - num: 0 + 3i C1 * num: 4 + 6i C1 / num: 1 + 1.5i num + C1: 4 + 3i num - C1: 0 - 3i num * C1: 4 + 6i num / C1: 0.307692 - 0.461538i ``` 希望这个程序能够满足您的需求。如果您有任何问题,欢迎随时提出。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值