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

const double PI = 3.14159265358979323846;

class ComplexTrig {
private:
    double modulus, angle; // модуль и угол в радианах
    
public:
    ComplexTrig(double mod = 0, double ang = 0) : modulus(mod), angle(ang) {}
    
    // Сложение
    ComplexTrig operator+(const ComplexTrig& other) const {
        // Преобразуем в алгебраическую форму для сложения
        double real1 = modulus * cos(angle);
        double imag1 = modulus * sin(angle);
        double real2 = other.modulus * cos(other.angle);
        double imag2 = other.modulus * sin(other.angle);
        
        double realSum = real1 + real2;
        double imagSum = imag1 + imag2;
        
        return ComplexTrig(sqrt(realSum*realSum + imagSum*imagSum), atan2(imagSum, realSum));
    }
    
    // Вычитание
    ComplexTrig operator-(const ComplexTrig& other) const {
        double real1 = modulus * cos(angle);
        double imag1 = modulus * sin(angle);
        double real2 = other.modulus * cos(other.angle);
        double imag2 = other.modulus * sin(other.angle);
        
        double realDiff = real1 - real2;
        double imagDiff = imag1 - imag2;
        
        return ComplexTrig(sqrt(realDiff*realDiff + imagDiff*imagDiff), atan2(imagDiff, realDiff));
    }
    
    // Умножение
    ComplexTrig operator*(const ComplexTrig& other) const {
        return ComplexTrig(modulus * other.modulus, angle + other.angle);
    }
    
    // Деление
    ComplexTrig operator/(const ComplexTrig& other) const {
        if (other.modulus == 0) throw invalid_argument("Деление на ноль");
        return ComplexTrig(modulus / other.modulus, angle - other.angle);
    }
    
    // Возведение в степень (^)
    ComplexTrig operator^(int power) const {
        return ComplexTrig(pow(modulus, power), angle * power);
    }
    
    // Извлечение квадратного корня
    friend ComplexTrig sqrt(const ComplexTrig& c) {
        return ComplexTrig(sqrt(c.modulus), c.angle / 2);
    }
    
    void print() const {
        cout << modulus << "·(cos(" << angle << ") + i·sin(" << angle << "))" << endl;
    }
    
    friend ostream& operator<<(ostream& os, const ComplexTrig& c);
};

ostream& operator<<(ostream& os, const ComplexTrig& c) {
    os << c.modulus << "·(cos(" << c.angle << ") + i·sin(" << c.angle << "))";
    return os;
}

int main() {
    SetConsoleOutputCP(65001);
    
    cout << "=== Задача 3.5 - Комплексное число (тригонометрическая форма) ===" << endl;
    
    ComplexTrig c1(10, PI/6);  // 10·(cos(π/6) + i·sin(π/6))
    ComplexTrig c2(5, PI/4);   // 5·(cos(π/4) + i·sin(π/4))
    
    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    
    ComplexTrig sum = c1 + c2;
    cout << "c1 + c2 = " << sum << endl;
    
    ComplexTrig product = c1 * c2;
    cout << "c1 * c2 = " << product << endl;
    
    ComplexTrig quotient = c1 / c2;
    cout << "c1 / c2 = " << quotient << endl;
    
    ComplexTrig power = c1 ^ 3;
    cout << "c1 ^ 3 = " << power << endl;
    
    ComplexTrig root = sqrt(c1);
    cout << "sqrt(c1) = " << root << endl;
    
    return 0;
}
Соседние файлы в папке Лаба3