其他分享
首页 > 其他分享> > task3

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 &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;
}

  

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

 

标签:real,task3,const,double,imag,Complex,real1
来源: https://www.cnblogs.com/yang-0909/p/15431254.html