task3

发布时间:2022-07-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了task3脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

task3 Complex.hpp 代码

#include<iostream>
#include<;math.h>
using namespace std;
class Complex{
	public:
		Complex();
		Complex(double real1,double imag1);
		Complex(double real1);
		Complex(const Complex &amp;r);   //复制构造函数 
		
		double get_real() const;   //返回函数实部 
		double get_imag() const;   //返回函数虚部 
		void show() const;   //输出结果 
		void add(Complex &m);   //一个复数加上本身 
		
		friend Complex add(const Complex &c1,const Complex &c2);   //实现两个复数相加 
		friend bool is_equal(const Complex &c1,const Complex &c2);   //bool型判断两个复数是否相同 
		friend double abs(const Complex &x1);   //计算复数的取模运算 
		
		PRivate:
			double real,imag;
	
	
};
Complex::Complex():real(0),imag(0){
}
Complex::Complex(double real1)
{
	real=real1;
	imag=0;
//	cout<<real<<endl;
}
Complex::Complex(double real1,double imag1)
{
	real=real1;
	imag=imag1;
}
Complex::Complex(const Complex &r)
{
	real=r.real;
	imag=r.imag;
}
double Complex::get_real() const
{
	//cout<<real<<endl;
	return real;

}
double Complex::get_imag() const
{
	return imag;
}
void Complex::add(Complex &m)
{
	real=real+m.real;
	imag=imag+m.imag;
}
void Complex::show() const
{
	double n1,n2;
	n1=get_real();
	//cout<<n1<<endl;
	n2=get_imag();
	if(n1==0&&n2==0)
	{
		cout<<n1;
	}
	else {
		if(n1==0)
		{
			cout<<n2<<"i";
		}
		else {
			if(n2==0)
			{
				cout<<n1;
			}
			else {
				if(n2>0)
				 cout<<n1<<"+"<<n2<<"i";
				else cout<<n1<<n2<<"i";
			}
		}
	}
}

Complex add(const Complex &c1,const Complex &c2)
{
	Complex c3;
	c3.real=c1.real+c2.real;
	c3.imag=c1.imag+c2.imag;
	return c3;
}
double abs(const Complex &x1)
{
	double x;
	x=sqrt(x1.real*x1.real+x1.imag*x1.imag);

	return x;
}
bool is_equal(const Complex &c1,const Complex &c2)
{
	if(c1.real==c2.real&&c1.imag==c2.imag)
	  return true;
	else return false;
}

  

更换输入数据后运行结果如下:

task3

 

脚本宝典总结

以上是脚本宝典为你收集整理的task3全部内容,希望文章能够帮你解决task3所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。