#pragma once #include #include #include class CMyComplex { public: // コンストラクタ CMyComplex(void) : Re(1.0), Im(0.0), Length(1.0), Theta(0.0) { } CMyComplex(const double re, const double im) { setComplexReIm(re, im); } // デストラクタ virtual ~CMyComplex(void){}; private: // エラーチェック inline bool ZeroLength() { if (Length < FLT_EPSILON) { std::cout << "ZeroLength: Length <= 0!" << std::endl; // Lengthが0以下の場合はどうする? Re = 0; Im = 0; Length = 0; Theta = 0; return true; } return false; } public: double Re, Im, Length, Theta; // 代入演算子 void operator=(const CMyComplex& comp1) { Re = comp1.Re; Im = comp1.Im; Length = comp1.Length; Theta = comp1.Theta; } // 加算演算子 CMyComplex operator+(const CMyComplex& comp1) { CMyComplex comp2; comp2.setComplexReIm(Re+comp1.Re, Im+comp1.Im); return comp2; } // 減算演算子 CMyComplex operator-(const CMyComplex& comp1) { CMyComplex comp2; comp2.setComplexReIm(Re-comp1.Re, Im-comp1.Im); return comp2; } // 乗算演算子 CMyComplex operator*(const CMyComplex& comp1) { CMyComplex comp2; comp2.setComplexLengthTheta(Length*comp1.Length, Theta+comp1.Theta); return comp2; } // 乗算演算子その2(スカラー倍) CMyComplex operator*(const double scale) { CMyComplex comp2; comp2.setComplexReIm(Re*scale, Im*scale); return comp2; } // 除算演算子 CMyComplex operator/(const CMyComplex& comp1) { CMyComplex comp2; comp2.setComplexLengthTheta(Length/comp1.Length, Theta-comp1.Theta); return comp2; } // 設定関数 inline double setComplexLengthTheta(const double r, const double theta) { Length = r; Theta = theta; Re = Length*cos(Theta); Im = Length*sin(Theta); ZeroLength(); return Length; } inline double setComplexReIm(const double re, const double im) { Re = re; Im = im; Length = sqrt(re*re+im*im); if (ZeroLength()) { return Length; } Theta = atan2(Im, Re); return Length; } // データ表示 inline void print() { std::cout << "Re=" << Re << ", Im=" << Im << ", Length=" << Length << ", Theta=" << Theta << std::endl; } };