【id:58】【20分】C. 复数运算(友元函数)

2023-05-16

时间限制
1s
内存限制
128MB
题目描述

复数类的声明如下:

class Complex
{
private:
double real; // 实部
double imag; // 虚部
public:
Complex();
Complex(double r, double i);
// 友元函数,复数c1 + c2(二参数对象相加)
friend Complex addCom(const Complex& c1, const Complex& c2);
// 友元函数,输出类对象c的有关数据(c为参数对象)
friend void outCom(const Complex& c);
};

要求如下:

  1. 实现复数类和友元函数addCom和outCom。
    
  2. 参考addCom函数为复数类增加一个友元函数minusCom,用于实现两个复数的减法

  3. 在main函数中,通过友元函数,实现复数的加减法和复数的输出。
    

输入

第1行:第1个复数的实部和虚部

第2行:需进行运算的次数,注意:是连续运算。具体结果可参考样例

第3行开始,每行输入运算类型,以及参与运算的复数的实部与虚部。“+”表示复数相加,“-”表示复数相减。

输出

每行输出复数运算后的结果,复数输出格式为“(实部,虚部)”。

#include<iostream>
using namespace std;
class Complex
{
private:
	double real; // 实部
	double imag; // 虚部
public:
	Complex()
	{
		real = 0;
		imag = 0;
	}
	Complex(double r, double i)
	{
		real = r;
		imag = i;
	}
	Complex(const Complex& p)
	{
		real = p.real;
		imag = p.imag;
	}
	friend Complex addCom(const Complex& c1, const Complex& c2);
	friend Complex subCom(const Complex& c1, const Complex& c2);
	// 友元函数,输出类对象c的有关数据(c为参数对象)
	friend void outCom(const Complex& c);
	friend double geti(const Complex& c1, const Complex& c2);
	friend double getr(const Complex& c1, const Complex& c2);
	friend double sgeti(const Complex& c1, const Complex& c2);
	friend double sgetr(const Complex& c1, const Complex& c2);
	
};
Complex addCom(const Complex& c1, const Complex& c2)// 友元函数,复数c1 + c2(二参数对象相加)
{
	return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
Complex subCom(const Complex& c1, const Complex& c2)// 友元函数,复数c1 + c2(二参数对象相加)
{
	return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
 double geti(const Complex& c1, const Complex& c2)
{
	return c1.imag + c2.imag;
}
 double getr(const Complex& c1, const Complex& c2)
{
	return c1.real + c2.real;
}
 double sgeti(const Complex& c1, const Complex& c2)
{
	return c1.imag - c2.imag;
}
 double sgetr(const Complex& c1, const Complex& c2)
{
	return c1.real - c2.real;
}
 void outCom(const Complex& c)// 友元函数,输出类对象c的有关数据(c为参数对象)
 {
	 cout << "(" << c.real << "," <<c.imag << ")" << endl;
}
 int main()
 {
	 double real,imag;
	 cin >> real >> imag;
	 Complex c1(real, imag);
	 int t;
	 double r, i;
	 r = i = 0;
	 cin >> t;
	for(int j=1;j<=t;j++)
	 {
		 char c;
		 cin >> c;
		 Complex C(r, i);
		 if (j==1)
		 {
			 if (c == '+')
			 {
				 cin >> real >> imag;
				 Complex c2(real, imag);
				 addCom(c1, c2);
				 r = getr(c1, c2);
				 i = geti(c1, c2);
			 }
			 else if (c == '-')
			 {
				 cin >> real >> imag;
				 Complex c2(real, imag);
				 addCom(c1, c2);
				 r = sgetr(c1, c2);
				 i = sgeti(c1, c2);
			 }
			 Complex a1(r, i);
			 outCom(a1);
		 }
		 else if (j>1)
		 {
			 if (c == '+')
			 {
				 cin >> real >> imag;
				 Complex c2(real, imag);
				 addCom(C, c2);
				 r = getr(C, c2);
				 i = geti(C, c2);
			 }
			 else if (c == '-')
			 {
				 cin >> real >> imag;
				 Complex c2(real, imag);
				 addCom(C, c2);
				 r = sgetr(C, c2);
				 i = sgeti(C, c2);
				 
			 }
			 Complex b1(r, i);
			 outCom(b1);
		 }
		 
		
	 }
 }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【id:58】【20分】C. 复数运算(友元函数) 的相关文章

随机推荐