Добавил:
Рад, если кому-то помог Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
01.11.2025
Размер:
3.14 Кб
Скачать
#include <iostream>
#include <cmath>
#include <windows.h>
using namespace std;

class ComplexAlg {
private:
    double real, imag;
    
public:
    ComplexAlg(double r = 0, double i = 0) : real(r), imag(i) {}
    
    // Сложение
    ComplexAlg operator+(const ComplexAlg& other) const {
        return ComplexAlg(real + other.real, imag + other.imag);
    }
    
    // Вычитание
    ComplexAlg operator-(const ComplexAlg& other) const {
        return ComplexAlg(real - other.real, imag - other.imag);
    }
    
    // Умножение
    ComplexAlg operator*(const ComplexAlg& other) const {
        return ComplexAlg(real * other.real - imag * other.imag,
                         real * other.imag + imag * other.real);
    }
    
    // Деление
    ComplexAlg operator/(const ComplexAlg& other) const {
        double denominator = other.real * other.real + other.imag * other.imag;
        if (denominator == 0) throw invalid_argument("Деление на ноль");
        
        return ComplexAlg((real * other.real + imag * other.imag) / denominator,
                         (imag * other.real - real * other.imag) / denominator);
    }
    
    // Возведение в степень (^)
    ComplexAlg operator^(int power) const {
        ComplexAlg result(1, 0);
        ComplexAlg base = *this;
        
        for (int i = 0; i < power; i++) {
            result = result * base;
        }
        return result;
    }
    
    // Извлечение квадратного корня
    friend ComplexAlg sqrt(const ComplexAlg& c) {
        double modulus = sqrt(c.real * c.real + c.imag * c.imag);
        double realPart = sqrt((modulus + c.real) / 2);
        double imagPart = (c.imag >= 0 ? 1 : -1) * sqrt((modulus - c.real) / 2);
        
        return ComplexAlg(realPart, imagPart);
    }
    
    void print() const {
        cout << real;
        if (imag >= 0) cout << " + " << imag << "i";
        else cout << " - " << -imag << "i";
        cout << endl;
    }
    
    friend ostream& operator<<(ostream& os, const ComplexAlg& c);
};

ostream& operator<<(ostream& os, const ComplexAlg& c) {
    os << c.real;
    if (c.imag >= 0) os << " + " << c.imag << "i";
    else os << " - " << -c.imag << "i";
    return os;
}

int main() {
    SetConsoleOutputCP(65001);
    
    cout << "=== Задача 3.6 - Комплексное число (алгебраическая форма) ===" << endl;
    
    ComplexAlg c1(3, 4);  // 3 + 4i
    ComplexAlg c2(1, -2); // 1 - 2i
    
    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    
    ComplexAlg sum = c1 + c2;
    cout << "c1 + c2 = " << sum << endl;
    
    ComplexAlg product = c1 * c2;
    cout << "c1 * c2 = " << product << endl;
    
    ComplexAlg quotient = c1 / c2;
    cout << "c1 / c2 = " << quotient << endl;
    
    ComplexAlg power = c1 ^ 2;
    cout << "c1 ^ 2 = " << power << endl;
    
    ComplexAlg root = sqrt(c1);
    cout << "sqrt(c1) = " << root << endl;
    
    return 0;
}
Соседние файлы в папке Лаба3