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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个示例: ```c++ #include <iostream> using namespace std; struct MyStruct { int a, b; MyStruct(int a = 0, int b = 0) : a(a), b(b) {} // 加法运算符重载 MyStruct operator+(const MyStruct& other) const { return MyStruct(a + other.a, b + other.b); } // 减法运算符重载 MyStruct operator-(const MyStruct& other) const { return MyStruct(a - other.a, b - other.b); } // 乘法运算符重载 MyStruct operator*(const MyStruct& other) const { return MyStruct(a * other.a, b * other.b); } // 除法运算符重载 MyStruct operator/(const MyStruct& other) const { return MyStruct(a / other.a, b / other.b); } // 赋值函数 MyStruct& operator=(const MyStruct& other) { a = other.a; b = other.b; return *this; } // 拷贝函数 MyStruct(const MyStruct& other) { a = other.a; b = other.b; } // 友元函数 friend ostream& operator<<(ostream& os, const MyStruct& obj) { os << "a: " << obj.a << ", b: " << obj.b; return os; } }; int main() { MyStruct a(1, 2), b(3, 4), c; // 测试加法运算符重载 c = a + b; cout << "a + b = " << c << endl; // 测试减法运算符重载 c = a - b; cout << "a - b = " << c << endl; // 测试乘法运算符重载 c = a * b; cout << "a * b = " << c << endl; // 测试除法运算符重载 c = a / b; cout << "a / b = " << c << endl; // 测试赋值函数 MyStruct d = a; cout << "d = " << d << endl; // 测试拷贝函数 MyStruct e(a); cout << "e = " << e << endl; return 0; } ``` 输出结果: ``` a + b = a: 4, b: 6 a - b = a: -2, b: -2 a * b = a: 3, b: 8 a / b = a: 0, b: 0 d = a: 1, b: 2 e = a: 1, b: 2 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值