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

📄 complex.cpp

📁 斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》的课件
💻 CPP
字号:
// This is the implementation file for the Complex class#include <iostream>#include <cmath>#include "complex.h"using namespace std;///////////////////////////////// Definitions of operations /////////////////////////////////double Complex::Tol = 1e-15;// Output operatorostream& operator<<( ostream& out, const Complex& z ){	// Output z in form a+bi or a-bi, where a is the	// real part and b is the absolute value of the	// imaginary part	double re = z.Re();	// if re or im is too small, declare it zero	if ( abs(re) < Complex::Tol )		re = 0.0;	double im = z.Im();	if ( abs(im) < Complex::Tol )		im = 0.0;	// if re is zero, don't print it,	// unless im is also zero	if ( re != 0.0 || im == 0.0 )		out << re;	// if no imaginary part, we're done!	if ( im == 0.0 )		return out;	// print a + if there was a real part,	// and imaginary part is positive	if ( im > 0.0 && re != 0.0 )		out << "+";	// if imaginary part is negative, the 	// minus will be output naturally, but	// if it's 1 or -1, we don't want to 	// output it at all, just the i	if ( im == -1.0 )		out << "-";	else if ( im != 1.0 )		out << im;	out << "i";	return out;}// Input operatoristream& operator>>( istream& in, Complex& z ){	// Read real part	double re;	in >> re;	// Check if we can continue	if ( in.bad() ) 		return in;	// Read + or -	char sign;	do {		in >> sign;		if ( in.bad() )			return in;	} while ( isspace(sign) );	// If we don't have a + or -, just take	// the preceding double as the real part,	// and put the invalid input back.  We	// assume that only a real number was given.	if ( sign != '+' && sign != '-' )	{		Complex z_in( re, 0.0 );		z = z_in;		in.putback(sign);		return in;	}	// Read imaginary part	double im;	in >> im;	if ( in.bad() ) 		return in;	// We read the + or - earlier, so a - is	// not part of the imaginary part.  The	// number im we just read is always positive.	if ( sign == '-' )		im = -im;	// Finally, read the i or I that should	// follow the imaginary part.	char i;	in >> i;	if ( in.bad() ) 		return in;	// If we read an i or I, then reading is	// successful and we can set the value of	// the argument z.  Otherwise, it's up to us	// to signal that the input stream is in a	// bad state.	if ( i == 'I' || i == 'i' ) {		Complex z_in( re, im );		z = z_in;	}	else		in.setstate( ios::failbit );	return in;}// ConstructorsComplex::Complex( double re, double im ){	// Create a complex number object from	// given real and imaginary parts	m_re = re;	m_im = im;}Complex::Complex( const Complex& z ){	// Copy constructor	m_re = z.m_re;	m_im = z.m_im;}// DestructorComplex::~Complex(){	// The only data in this object are the	// real and imaginary parts, and they will	// be de-allocated automatically, so we	// don't have to do anything here}// Operationsdouble Complex::Re() const{	return m_re;}double Complex::Im() const{	return m_im;}double Complex::Abs() const{	return sqrt( m_re * m_re + m_im * m_im );}double Complex::Arg() const{	return atan2( m_im, m_re );}Complex Complex::Conj() const{	Complex z( m_re, -m_im );	return z;}// Overloaded arithmetic operatorsComplex Complex::operator-() const{	Complex z( -m_re, -m_im );	return z;}Complex Complex::operator+( const Complex& z ) const{	Complex sum( m_re + z.m_re, m_im + z.m_im );	return sum;}Complex Complex::operator-( const Complex& z ) const{	Complex diff( m_re - z.m_re, m_im - z.m_im );	return diff;}Complex Complex::operator*( const Complex& z ) const{	Complex prod( m_re * z.m_re - m_im * z.m_im, m_re * z.m_im + m_im * z.m_re );	return prod;}Complex Complex::operator/( const Complex& z ) const{	Complex quot( m_re * z.m_re + m_im * z.m_im, -m_re * z.m_im + m_im * z.m_re );	double modz = z.Abs();	double den = modz * modz;	quot.m_re /= den;	quot.m_im /= den;	return quot;}// Arithmetic with right operand of doubleComplex Complex::operator+( double x ) const{	Complex sum( m_re + x, m_im );	return sum;}Complex Complex::operator-( double x ) const{	Complex diff( m_re - x, m_im * x );	return diff;}Complex Complex::operator*( double x ) const{	Complex prod( m_re * x, m_im * x );	return prod;}Complex Complex::operator/( double x ) const{	Complex quot( m_re / x, m_im / x );	return quot;}// Overloaded assignment operatorsComplex& Complex::operator=( const Complex& z ){	m_re = z.m_re;	m_im = z.m_im;	return *this;}Complex& Complex::operator+=( const Complex& z ){	m_re += z.m_re;	m_im += z.m_im;	return *this;}Complex& Complex::operator-=( const Complex& z ){	m_re -= z.m_re;	m_im -= z.m_im;	return *this;}Complex& Complex::operator*=( const Complex& z ){	double re = m_re;	double im = m_im;	m_re = re * z.m_re - im * z.m_im;	m_im = re * z.m_im + im * z.m_re;	return *this;}Complex& Complex::operator/=( const Complex& z ){	double re = m_re;	double im = m_im;	m_re = re * z.m_re + im * z.m_im;	m_im = -re * z.m_im + im * z.m_re;	double modz = z.Abs();	double den = modz * modz;	m_re /= den;	m_im /= den;	return *this;}// Assignment operators with double valueComplex& Complex::operator=( double x ){	m_re = x;	m_im = 0.0;	return *this;}Complex& Complex::operator+=( double x ){	m_re += x;	return *this;}Complex& Complex::operator-=( double x ){	m_re -= x;	return *this;}Complex& Complex::operator*=( double x ){	m_re *= x;	m_im *= x;	return *this;}Complex& Complex::operator/=( double x ){	m_re /= x;	m_im /= x;	return *this;}// Conversion to double (useful in case Complex// object has zero imaginary part)Complex::operator double() const{	return m_re;}// Arithmeic Operators with left operand of doubleComplex operator+( double x, const Complex& z ){	Complex sum( x + z.Re(), z.Im() );	return sum;}Complex operator-( double x, const Complex& z ){	Complex diff( x - z.Re(), -z.Im() );	return diff;}Complex operator*( double x, const Complex& z ){	Complex prod( x * z.Re(), x * z.Im() );	return prod;}Complex operator/( double x, const Complex& z ){	double modz = z.Abs();	double den = modz * modz;	Complex quot( x * z.Re() / den, -x * z.Im() / den );	return quot;}

⌨️ 快捷键说明

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