| Код | #include <iostream.h> // это нужно для cout #include <iomanip.h> // это нужно для endl #include <math.h> // это нужно для fabs //----------------------- class Complex { public: double real; // вещественная часть double imaginary; // мнимая часть void Init(double _real, double _imaginary); // Метод инициализации void Print(); // Метод печати на экран Complex operator+(const Complex x) const; // Сложение Complex operator-(const Complex x) const; // Вычитание Complex operator*(const Complex x) const; // Умножение Complex operator/(const Complex x) const; // Деление };
void Complex::Init(double _real, double _imaginary) { real = _real; imaginary = _imaginary; }
void Complex::Print() { cout << real ; if (imaginary > 0) { cout << " + i " << fabs(imaginary) << " ";; } if (imaginary < 0) { cout << " - i " << fabs(imaginary) << " ";; } }
Complex Complex::operator+(const Complex x) const { Complex result; result.real = real + x.real; result.imaginary = imaginary + x.imaginary; return result; }
Complex Complex::operator-(const Complex x) const { Complex result; result.real = real - x.real; result.imaginary = imaginary - x.imaginary; return result; } Complex Complex::operator*(const Complex x) const { Complex result; // (a+ib)(c+id) = (ac-bd)+i(ad+bc) result.real = real * x.real - imaginary * x.imaginary; result.imaginary = real * x.imaginary + imaginary * x.real; return result; } Complex Complex::operator/(const Complex x) const { Complex result; if ( x.real == 0 && x.imaginary == 0) { cout << "*** Ошибка! Попытка деления на ноль! ***"; return result; } double znam; if ( fabs(x.real) >= fabs(x.imaginary) ) { znam = x.real + x.imaginary * x.imaginary / x.real; result.real = (real + imaginary * x.imaginary / x.real) / znam; result.imaginary = (imaginary - real * x.imaginary / x.real ) / znam; } else{ znam = x.imaginary + x.real * x.real / x.imaginary; result.real = (imaginary + real * x.real / x.imaginary) / znam; result.imaginary = (imaginary * x.real / x.imaginary - real ) / znam; } return result; } //----------------------------- void Testing (Complex z1, Complex z2) { cout << "z1 = "; z1.Print(); cout << endl; cout << "z2 = "; z2.Print(); cout << endl; Complex z3; z3 = z1 + z2; cout << "z1 + z2 = "; z3.Print(); cout << endl; cout << "z1 - z2 = "; z3 = z1 - z2; z3.Print(); cout << endl; cout << "z1 * z2 = "; z3 = z1 * z2; z3.Print(); cout << endl; cout << "z1 / z2 = "; z3 = z1 / z2; z3.Print(); cout << endl; } //----------------------------- int main() { Complex z1; // первый объект класса Complex z1.Init(-5.55, 45.8); Complex z2; // второй объект класса Complex z2.real = 1.5; z2.imaginary = -3.99;
Testing (z1, z2); cout << endl; z1.Init(0, 0); Testing (z1, z2); cout << endl; z1.Init(-0.6, -14.8); z2.Init(1, 0); Testing (z1, z2); cout << endl; z2.Init(0, 3); Testing (z1, z2); cout << endl; z2.Init(0, 0); Testing (z1, z2); cout << endl; return 0; }
|
только потести хорошенько
Модератор: выбирайте подсветку кода http://forum.vingrad.ru/index.php?showtopic=126445 |