⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complex_def.hpp

📁 复数类
💻 HPP
字号:
#pragma once
#include "Complex.hpp"

template< typename Type >
Complex::Complex( Type const& a, Type const& b )
{
	real= static_cast<double>(a);
	imag= static_cast<double>(b);
}


template< typename Type >
Complex::Complex ( Type const& Item )
{
	real= static_cast<double>(Item);
	imag= 0;
}

Complex::Complex ()
{
	real= 0;
	imag= 0;
}

double Complex::GetReal() const 
{
	return real;
}


double Complex::GetImag() const 
{
	return imag;
}


void  Complex::SetReal ( double Item ) 
{
	real= Item;
}


void Complex::SetImag ( double Item )
{
	imag= Item;
}


std::ostream& operator << (std::ostream& output, Complex const &Item )
{
	output << "( " << Item.real << " , " << Item.imag << ")";

	return output;
}


template< typename Type >
Complex operator+ ( Type const& a, Complex const& b )
{
	Complex temp;

	temp.real= static_cast<double>(a)+ b.real;
	temp.imag= b.imag ;

	return temp;
}


template< typename Type >
Complex operator- ( Type  a, Complex  b )
{
	Complex temp;

	temp.real= static_cast<double>(a)- b.real;
	temp.imag= -b.imag;

	return temp;
}


template< typename Type >
Complex operator* ( Type const& a, Complex const& b )
{
	Complex temp;

	temp.real= static_cast<double>(a)* b.real;
	temp.imag= static_cast<double>(a)* b.imag;

	return temp;
}


template< typename Type >
Complex operator/ ( Type const& a, Complex const& b )
{
	Complex temp;

	if ( b.real== 0 && b.imag== 0 )
	{
		std::cerr << "除数不能0!!" << std::endl;
		exit(0);
	}

	temp.real= static_cast<double>(a)* b.imag/
		         ( b.real* b.real+ b.imag* b.imag );
	temp.imag= -static_cast<double>(a)* b.real/
		         ( b.imag* b.imag+ b.real* b.real );

	return temp;
}


template< typename Type >
Complex operator+ ( Complex const& a, Type const& b )
{
	return b+ a;
}


template< typename Type >
Complex operator- ( Complex const& a, Type const& b )
{
	Complex temp;

	temp.real= a.real- b;
	temp.imag= a.imag;

	return temp;
}


template< typename Type >
Complex operator* ( Complex const& a, Type const& b )
{
	return b* a;
}



template< typename Type >
Complex operator/ ( Complex const& a, Type const& b )
{
	Complex temp;

	if ( b== 0 )
	{
		std::cerr << "除数不能为0!!" << std::endl;
		exit(0);
	}

	temp.real= a.real/ b;
	temp.imag= a.imag/ b;

	return temp;
}



Complex operator+ ( Complex const& a, Complex const& b )
{
	Complex temp;

	temp.real= a.real+ b.real;
	temp.imag= a.imag+ b.imag;

	return temp;
}



Complex operator- ( Complex const& a, Complex const& b )
{
	Complex temp;

	temp.real= a.real- b.real;
	temp.imag= a.imag- b.imag;

	return temp;
}



Complex operator* ( Complex const& a, Complex const& b )
{
	Complex temp;

	temp.real= a.imag* b.imag- a.real* b.real;
	temp.imag= a.real* b.imag+ a.imag* b.real;

	return temp;
}



Complex operator/ ( Complex const& a, Complex const& b )
{
	Complex temp;
	double  t= b.imag* b.imag- b.real* b.real;

	if ( t== 0 )
	{
		std::cerr << "除数为零。无法计算!!!!" << std::endl;
		exit(0);
	}

	temp.real= -( a.real* b.real+ a.imag* b.imag )/ t;
	temp.imag= ( a.imag* b.real- a.real* b.imag )/ t;

	return temp;
}

⌨️ 快捷键说明

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