complex.cpp

来自「该函数可以实现任意行列数double型矩阵的张量乘积 用数组实现」· C++ 代码 · 共 66 行

CPP
66
字号
#include"complex.h"
#include"iostream.h"
#include <cmath>

/******************************************************************************
                            以下定义实现复数类的函数
  ****************************************************************************/
complex operator+(complex &c1,complex & c2)//加
{
 return complex(c1.real+c2.real,c1.imag+c2.imag);//前面的complex不能忽略,注意!!!
}

complex operator-(complex &c1,complex & c2)//减
{
return complex(c1.real-c2.real,c1.imag-c2.imag);
}

complex operator*(complex &c1,complex &c2)//乘
{
	return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}
 
complex operator/(complex &c1,complex &c2)//除
{
	return complex((c1.real*c2.real-c1.imag*c2.imag)/ sqrt (c2.imag*c2.imag+c2.real*c2.real),(c1.real*c2.imag+c1.imag*c2.real)/ sqrt (c2.imag*c2.imag+c2.real*c2.real));

}

complex operator*(complex &c2,double d)//复数和实数的乘
{
	return complex(d*c2.real,d*c2.imag);
}

void complex::display()//复数显示
{
 cout<<"("<<real<<","<<imag<<"i"<<")";//<<endl;
 
}

complex complex::conjugate()//求复数的共轭
{
  return complex(real,-imag);
}
complex complex::negconjugate()//求复数的负共轭
{
	return complex(-real,imag);
}
void complex::setinitial (double r ,double i)//复数对象的赋值
{
	real=r;
	imag=i;
}

double complex::mod()//复数求模
{
	return((double)sqrt((real*real)+(imag*imag)));
}

/**
void complex:: operator=(complex&c1)
{
	complex c2;
	c2.real=c1.real;
	c2.imag=c1.imag;
}
**/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?