Для перехода на новую строку с отступом предыдущей строки
set smartindent
в файле vimrc
set smartindent
в файле vimrc
#include <math.h> namespace test { class Complex { public: // Конструктор по умолчанию Complex():itsRe(0),itsIm(0) { } // Конструктор с параметрами: действительная и мнимая часть Complex(double real, double imagine):itsRe(real),itsIm(imagine) { } // Конструктор копирования Complex(const Complex &); // Деструктор ~Complex() { itsRe = 0; itsIm = 0; } // Арифметические операции Complex operator+ (const Complex &); Complex operator- (const Complex &); Complex operator* (const Complex &); Complex operator/ (const Complex &); void operator+= (const Complex &); void operator-= (const Complex &); void operator*= (const Complex &); void operator/= (const Complex &); // Унарный оператор знака Complex operator- () const; // Операторы присваивания Complex operator= (const Complex &); Complex operator= (const double &); // Функции доступа // Получить действительную часть double GetReal() const { return itsRe; } // Получить мнимую часть double GetImagine() const { return itsIm; } // Модуль комплексного числа double GetAbs() const; // Аргумент комплексного числа double GetArg() const; private: double itsRe; double itsIm; }; }
#include "mycomplex.h" namespace test{ Complex::Complex(const Complex & rhs) { itsRe = rhs.itsRe; itsIm = rhs.itsIm; } Complex Complex::operator+ (const Complex & rhs) { return Complex(itsRe + rhs.itsRe, itsIm + rhs.itsIm); } Complex Complex::operator- (const Complex & rhs) { return *this + (-rhs); } Complex Complex::operator* (const Complex & rhs) { double re = (itsRe * rhs.itsRe) - (itsIm * rhs.itsIm); double im = (itsIm * rhs.itsRe) + (itsRe * rhs.itsIm); return Complex(re, im); } Complex Complex::operator/ (const Complex & rhs) { double c = (rhs.itsRe * rhs.itsRe) + (rhs.itsIm * rhs.itsIm); double re = (itsRe * rhs.itsRe) + (itsIm * rhs.itsIm); double im = (itsIm * rhs.itsRe) - (itsRe * rhs.itsIm); return Complex(re/c, im/c); } void Complex::operator+= (const Complex & rhs) { *this = *this + rhs; } void Complex::operator-= (const Complex & rhs) { *this = *this - rhs; } void Complex::operator*= (const Complex & rhs) { *this = *this * rhs; } void Complex::operator/= (const Complex & rhs) { *this = *this / rhs; } Complex Complex::operator- () const { return Complex(-itsRe, -itsIm); } Complex Complex::operator= (const Complex & rhs) { itsRe = rhs.itsRe; itsIm = rhs.itsIm; return *this; } Complex Complex::operator= (const double & rhs) { itsRe = rhs; itsIm = 0; return *this; } double Complex::GetAbs() const { return sqrt((itsRe * itsRe) + (itsIm * itsIm)); } double Complex::GetArg() const { if (itsRe > 0 || itsIm != 0) return 2 * atan( itsIm / (this->GetAbs() + itsRe) ); else if (itsRe < 0 && itsIm == 0) return M_PI; return 0; } }Пример использования. Для проверки корректности результатов проводится сравнение со встроенным в стандартную библиотеку классом Complex<T>.
#include "mycomplex.h" #include <iostream> #include <complex> int main() { test::Complex n2(2.0, 3.0); test::Complex n3 = n2+(-n2); std::cout << n2.GetReal() << ":" << n2.GetImagine() << "\n"; std::cout << -n2.GetReal() << ":" << -n2.GetImagine() << "\n"; std::cout << n3.GetReal() << ":" << n3.GetImagine() << "\n"; std::cout << n2.GetArg() << ":" << n2.GetAbs() << "\n"; std::complex<double> x(2., 3.); std::cout << std::arg(x) << ":" << std::abs(x) << "\n"; return 0; }