Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- struct Complex {
- double Re;
- double Im;
- };
- Complex NewComplex(double a, double b) {
- Complex c;
- c.Re = a;
- c.Im = b;
- return c;
- }
- double Module(Complex a) {
- return sqrt(a.Re * a.Re + a.Im * a.Im);
- }
- double Angle(Complex a) {
- return atan2(a.Im, a.Re);
- }
- Complex Add(Complex a, Complex b) {
- return NewComplex(a.Re + b.Re, a.Im + b.Im);
- }
- Complex Sub(Complex a, Complex b) {
- return NewComplex(a.Re - b.Re, a.Im - b.Im);
- }
- Complex Mult(Complex a, Complex b) {
- return NewComplex(a.Re * b.Re - a.Im * b.Im, a.Re * b.Im + a.Im * b.Re);
- }
- Complex Divide(Complex a, Complex b) {
- return NewComplex((a.Re * b.Re + a.Im * b.Im) / (b.Im * b.Im + b.Re * b.Re), (b.Re * a.Im - a.Re * b.Im) / (b.Im * b.Im + b.Re * b.Re));
- }
- vector<Complex> Root(Complex a, int n) {
- vector<Complex> roots;
- double r = Module(a);
- double fi = Angle(a);
- for (int i = 0; i < n; i++) {
- Complex Root = NewComplex(pow(r, 1.0 / n) * cos((fi + 2 * 3.14 * i) / n), pow(r, 1.0 / n) * sin((fi + 2 * 3.14 * i) / n));
- roots.push_back(Root);
- }
- return roots;
- }
- Complex Power(Complex a, int n) {
- return NewComplex(pow(Module(a), n) * cos(n * Angle(a)), pow(Module(a), n) * sin(n * Angle(a)));
- }
- string ComplexToString(Complex a) {
- string out;
- out += to_string(a.Re);
- out += ((a.Im >= 0) ? "+" : "-");
- out += to_string(abs(a.Im))+"i";
- return out;
- }
- int main()
- {
- setlocale(0, "ru");
- Complex a = NewComplex(1, 5);
- Complex b = NewComplex(4, 3);
- cout << "a: " << ComplexToString(a) << endl;
- cout << "b: " << ComplexToString(b) << endl;
- cout << "Сложение: " << ComplexToString(Add(a, b)) << endl;
- cout << "Вычитание: " << ComplexToString(Sub(a, b)) << endl;
- cout << "Произведение: " << ComplexToString(Mult(a, b)) << endl;
- cout << "Деление: " << ComplexToString(Divide(a, b)) << endl;
- cout << "3 степень: " << ComplexToString(Power(a, 3)) << endl;
- cout << "Корень 3 степени: " << endl;
- vector<Complex> roots = Root(a, 3);
- for (int i = 0; i < 3; i++) {
- cout << i << ": " << ComplexToString(roots[i]) << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement