📄 complex.h
字号:
// Complex.h
// --------------------------------------
// Copyright (c) Yau-Zen Chang (2002.01-)
// All Rights Reserved.
// 本程式版权属于 张耀仁
// 使用时必需声明.
// --------------------------------------
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::ios;
#include <math>
#include <algorithm>
// class declaration
class Complex
{
friend void Display(const Complex&);
friend inline float Abs (const Complex&);
friend inline float Ang (const Complex&);
friend inline float Real (const Complex&);
friend inline float Imag (const Complex&);
private:
float *Z;
public:
// 建构函数
Complex(float = 0.0, float = 0.0);
// 宣告复制建构函数
Complex(const Complex&);
// 解构函数
~Complex();
// 宣告指派运算子 operator = ()
Complex& operator = (const Complex&);
// 宣告加法运算子 operator + ()
inline Complex operator + (const Complex&);
// 宣告乘法运算子 operator * ()
inline Complex operator * (const Complex&);
// 宣告除法运算子 operator / ()
Complex operator / (const Complex&);
};
// 解构函数的定义
Complex::~Complex()
{delete [] Z; cout << "呼叫解构函数"<< endl;}
// 建构函数的定义
Complex::Complex(float x, float y)
{
Z= new float [2];
Z[0]=x;
Z[1]=y;
return;
}
// 复制建构函数的定义
Complex::Complex(const Complex& OldZ)
{
Z = new float [2];
for (int i=0; i<2; i++)
Z[i]= OldZ.Z[i];
return;
}
// 定义指派运算子 operator = ()
Complex& Complex::operator=(const Complex& Z2)
{
for (int i=0; i<2; i++)
Z[i] = Z2.Z[i];
return *this;
}
// 定义加法运算子 operator + ()
Complex Complex::operator+(const Complex& Z2)
{
return Complex(Z[0]+Z2.Z[0], Z[1]+Z2.Z[1]);
}
// 定义乘法运算子 operator * ()
Complex Complex::operator*(const Complex& Z2)
{
return Complex (Z[0]*Z2.Z[0]-Z[1]*Z2.Z[1],
Z[0]*Z2.Z[1]+Z[1]*Z2.Z[0]);
}
// 定义除法运算子 operator / ()
Complex Complex::operator/(const Complex & Z2)
{
Complex Z1(Z[0],Z[1]);
float r , im , Z1_r , Z1_im , Temp ;
r=Z2.Z[0];
im=Z2.Z[1];
Temp = r*r+im*im;
Z1_r=Z1.Z[0] ; Z1_im=Z1.Z[1] ;
return Complex ((Z1_r*r+Z1_im*im)/Temp,
(Z1_im*r-Z1_r*im)/Temp );
}
// 定义 friend 函数 Display()
void Display(const Complex& Z1)
{
float f, a;
char Sign;
f=Imag(Z1);
if (f>=0) Sign= '+'; else Sign='-';
cout << setw(10)
<< setiosflags(ios::fixed)
<< setiosflags(ios::right)
<< setiosflags(ios::showpoint)
<< setprecision(5)
<< Z1.Z[0] << " " << Sign << " j "
<< fabs(f) << endl;
return;
}
// 定义 friend 函数 Abs()
float Abs(const Complex& Z1)
{return sqrt(Z1.Z[0]*Z1.Z[0]+Z1.Z[1]*Z1.Z[1]);}
// 定义 friend 函数 Ang()
float Ang(const Complex& Z1)
{return atan2( Imag( Z1 ) , Real( Z1 ) );}
// 定义 friend 函数 Real()
float Real(const Complex& Z1)
{ return Z1.Z[0];}
// 定义 friend 函数 Imag()
float Imag(const Complex& Z1)
{ return Z1.Z[1];}
// 定义 friend 函数 DisplayPhasor()
void DisplayPhasor(const Complex& Z1)
{
float f, a;
char Sign;
cout << "In Phasor form:\n";
cout
<< setw(10)
<< setiosflags(ios::fixed)
<< setiosflags(ios::right)
<< setiosflags(ios::showpoint)
<< setprecision(5)
<< Abs(Z1) << " phase " << Ang(Z1) << endl;
return;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -