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();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值