用友元运算符重载函数进行复数运算

#include<iostream>
using namespace std;
class Complex
{
	public:
		Complex(double r=0.0,double i=0.0);
		void print();
		friend Complex operator+(Complex &a,Complex & b);//声明运算符+ 重载函数 
		friend Complex operator-(Complex &a,Complex & b);//声明运算符- 重载函数  
		friend Complex operator*(Complex &a,Complex & b);//声明运算符* 重载函数 
		friend Complex operator/(Complex &a,Complex & b);//声明运算符/ 重载函数 
	private:
		double real;//复数实部 
		double imag;//复数虚部 
};
Complex::Complex(double r,double i)  //构造函数
{
	real=r;
	imag=i;
}
Complex operator+(Complex &a,Complex & b)//定义运算符+重载函数 
{
	Complex temp;
	temp.real=a.real+b.real;
	temp.imag=a.imag+b.imag;
	return temp;
}
Complex operator-(Complex &a,Complex & b)//定义运算符-重载函数 
{
	Complex temp;
	temp.real=a.real-b.real;
	temp.imag=a.imag-b.imag;
	return temp;
}
Complex operator*(Complex &a,Complex & b)//定义运算符*重载函数 
{
	Complex temp;
	temp.real=a.real*b.real-a.imag*b.imag;
	temp.imag=a.real*b.imag+a.imag*b.real;
	return temp;
}
Complex operator/(Complex &a,Complex & b)//定义运算符/重载函数 
{
	Complex temp;
	double t;
	t=1/(b.real*b.real+b.imag*b.imag);
	temp.real=(a.real*b.real+a.imag*b.imag)*t;
	temp.imag=(-b.imag*a.real+a.imag*b.real)*t;
	return temp;
}
void Complex::print()//显示输出复数函数  
{
	cout<<real;
	if(imag>0) 
	cout<<"+";
	if(imag!=0)
	cout<<imag<<"i"<<endl;
}
int main()
{
	Complex A1(2.3,4.6),A2(3.6,2.8),A3,A4,A5,A6;
	A3=A1+A2;
	A4=A1-A2;
	A5=A1*A2;
	A6=A1/A2;
	A1.print();
	A2.print();
	A3.print();
	A4.print();
	A5.print();
	A6.print();
	return 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& 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, imag * c.real + real * c.imag); } Complex operator/(const Complex& c) const { double denominator = c.real * c.real + c.imag * c.imag; return Complex((real * c.real + imag * c.imag) / denominator, (imag * c.real - real * c.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; 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; } ``` 在上面的代码中,我们定义了一个 Complex 类来表示复数,其中包含了实部和虚部两个私有成员变量。我们使用了构造函数来初始化这两个变量。在类的定义中,我们重载了加、减、乘、除等运算符,使得我们可以对复数进行这些运算。此外,我们还使用了一个友元函数来重载流运算符,使得我们可以方便地输出复数的值。在主函数中,我们创建了两个复数对象,然后对它们进行了加、减、乘、除等运算,最后输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值