/
NeonBite
/
cpp-oop-2
Обзор
Документация
Войти
/
NeonBite
/
cpp-oop-2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
compleks.h
124 строки
2 KB
NeonBite
complex sources added
24 фев 2026, 08:43
24 фев 2026, 08:43
bbf64d5
Код
Авторство
О чём код?
#ifndef COMPLEKS_H #define COMPLEKS_H #include <cmath> #include <string> #include <sstream> using std::string; using std::stringstream; class Complex { double re; double im; public: Complex() { re = 0; im = 0; } Complex(double x, double y) { this->re = x; this->im = y; } void set_re(double re) { this->re = re; } void set_im(double im) { this->im = im; } double get_re() { return re; } double get_im() { return im; } double abs() { return sqrt(re * re + im * im); } double arg() { if (re > 0 && im > 0 || re > 0 && im < 0) return atan(im / re); else if (re < 0 && im > 0 || re < 0 && im < 0) return atan(im / re) + M_PI; else if (re == 0 && im > 0) return M_PI / 2; else if (re == 0 && im < 0) return M_PI / -2; else if (re > 0 && im == 0) return 0; else return M_PI; } string to_string() { stringstream ss; ss << re; if (im != 0) ss << ((im > 0) ? " + " : " - ") << "i * " << fabs(im); return ss.str(); } string to_string_trig() { stringstream ss; double phi = arg(); ss << "cos(" << phi << ")" << " + i*sin(" << phi << ")"; return ss.str(); } string to_string_exp() { stringstream ss; double phi = arg(); double ro = abs(); ss << ro << "*e^" << ((phi > 0) ? "i*" : "-i*") << phi; return ss.str(); } Complex add(Complex z) { Complex res(re + z.get_re(), im + z.get_im()); return res; } Complex sub(Complex z) { Complex res(re - z.get_re(), im + z.get_im()); return res; } Complex mult(Complex z) { Complex res { re * z.re - im * z.im, re * z.im + im * z.re }; return res; } Complex div(Complex z) { Complex res { (re * z.re + im * z.im) / (z.re * z.re + z.im * z.im), (-re * z.im + im * z.re) / (z.re * z.re + z.im * z.im) }; return res; } }; #endif