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

📄 complexnum.cpp

📁 This file implements the ComplexNum class member functions.
💻 CPP
字号:
//****************************************************************
// Implementation file (ComplexNum.cpp)
// This file implements the ComplexNum class member functions. 
// Programming Problems No.3 on P472 of textbook. 
//****************************************************************
#include "complexNum.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// Private members of class
//     double  real, imag;
//*****************************************************************

ComplexNum::ComplexNum(/*in*/double r,/*in*/double i)
{
	real = r;
	imag = i;
}

//*****************************************************************
ComplexNum::ComplexNum()
{
	real = 0.0;
	imag = 0.0;
}

//*****************************************************************
void ComplexNum::Set(/*in*/double r, //real part of the complex number
		             /*in*/double i)//imaginary part of the complex number
{
	real = r;
	imag = i;
}

//*****************************************************************
ComplexNum ComplexNum::Cadd(/*in*/ComplexNum other) const	
{
	ComplexNum result;
	result.real = real + other.real;
	result.imag = imag + other.imag;
	return result;
}

//*****************************************************************
ComplexNum ComplexNum::Csub(/*in*/ComplexNum other) const
{
	ComplexNum result;
	result.real = real - other.real;
	result.imag = imag - other.imag;
	return result;
}

//*****************************************************************
ComplexNum ComplexNum::Cmult(/*in*/ComplexNum other) const
{
	ComplexNum result;
	result.real = real*other.real - imag*other.imag;
	result.imag = real*other.imag + imag*other.real;
	return result;
}

//*****************************************************************
ComplexNum ComplexNum::Cdiv(/*in*/ComplexNum other) const
{
	ComplexNum result;
	result.real = ( real * other.real + imag * other.imag )
		          / ( pow( other.real, 2 ) + pow( other.imag, 2 ) );
	result.imag = ( imag * other.real + real * other.imag )
				  / ( pow( other.real, 2 ) + pow( other.imag, 2 ) );
	return result;
}

//*****************************************************************
double ComplexNum::Cabs() const
{
	return sqrt( real * real + imag * imag );
}

//*****************************************************************
void ComplexNum::Print() const
{
	cout<< "(" << real << ", " << imag << ")" << endl;
}

//*****************************************************************
double ComplexNum::RealPart()const
{
  return real;
}

//*****************************************************************
double ComplexNum::ImagPart()const
{
  return imag;
}

⌨️ 快捷键说明

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