Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> //exercise 16
- #include <vector> //realize functions for complex numbers: input&output in algebraic and trigonometric form,
- #include <math.h> //determination of modulus and argument of number, addition, subtraction, multiplication and division of complex numbers, involution
- #define PI 3.14159265 //a + bi == re + im * i
- using namespace std;
- struct complex
- {
- complex();
- double re; //real part of complex number
- double im; //imaginary part
- double r; //modulus
- double u; //the angle between the representing number vector and the positive direction of the Ox(argument)
- complex(double re, double im, double r, double u) : re(re), im(im), r(r), u(u) {};
- };
- vector<complex> c;
- void printAlg(complex c)
- {
- cout.setf(ios::showpos);//explicit sign identification
- cout << c.re << c.im << "i\n";
- }
- void printTrig(complex c)
- {
- cout << c.r << "(cos(" << c.u * 180 / PI << " dgr) + sin(" << c.u * 180 / PI << " dgr)i)\n";//degree-form makes human perception much easier, i think
- }
- void modArg(complex& c)//modulus and argument
- {
- c.r = sqrt(c.re * c.re + c.im * c.im);
- if (c.re == 0)
- {
- if (c.im >= 0)
- c.u = PI / 2;
- else
- c.u = PI * 3 / 2;
- }
- else
- {
- double ccos = c.re / c.r;
- if (ccos >= 0)
- c.u = atan(c.im / c.re);
- else
- c.u = PI + atan(c.im / c.re);
- }
- }
- void algForm(complex& c)
- {
- c.re = c.r * cos(c.u);
- c.im = c.r * sin(c.u);
- }
- void input()
- {
- bool inputForm;
- cout << "If you want to input comlex number in algebraic form, press 1;\nIf in trigonometric, press 0.\n";
- cin >> inputForm;
- if (inputForm)
- {
- cout << "Algebraic form: a + bi. Enter a and b.\n";
- double a, b;
- cin >> a >> b;
- c.push_back(complex(a, b, 0, 0));
- modArg(c.back());
- }
- else
- {
- cout << "Trigonometric form: r(cos u + sin u. Enter r and u(an angle should be expressed in degrees).\n";
- double r, u;
- cin >> r >> u;
- u = u * PI / 180; //trigonometric functions work with radian-format
- c.push_back(complex(0, 0, r, u));
- algForm(c.back());
- }
- }
- void addition(complex a, complex b)//+
- {
- c.push_back(complex(a.re + b.re, a.im + b.im, 0, 0));
- modArg(c.back());
- cout << "\nAddition\n";
- printAlg(c.back());
- printTrig(c.back());
- }
- void subtraction(complex a, complex b)//-
- {
- c.push_back(complex(a.re - b.re, a.im - b.im, 0, 0));
- modArg(c.back());
- cout << "\nSubtraction\n";
- printAlg(c.back());
- printTrig(c.back());
- }
- void multiplication(complex a, complex b)//*
- {
- c.push_back(complex(a.re * b.re - a.im * b.im, a.im * b.re + a.re * b.im, 0, 0));
- modArg(c.back());
- cout << "\nMultiplication\n";
- printAlg(c.back());
- printTrig(c.back());
- }
- void division(complex a, complex b)//:
- {
- c.push_back(complex((a.re * b.re + a.im * b.im) / (b.re * b.re + b.im * b.im), (a.im * b.re - a.re * b.im) / (b.re * b.re + b.im * b.im), 0, 0));
- modArg(c.back());
- cout << "\nDivision\n";
- printAlg(c.back());
- printTrig(c.back());
- }
- void involution(complex a)//a^x
- {
- cout << "\nFor involution enter the degree number: ";
- int n;
- cin >> n;
- double r = 1;
- for(int i = 0; i < n; i++)
- r *= a.r;
- c.push_back(complex(0, 0, r, n * a.u));
- cout << "\nInvolution\n";
- printTrig(c.back());
- }
- int main()
- {
- cout << "For some operations we need to take 2 complex numbers, so you should enter 2 complex numbers.\n";
- input();
- input();
- addition(c[0], c[1]);
- subtraction(c[0], c[1]);
- multiplication(c[0], c[1]);
- cout << "\nDevide the first number be the second.";
- division(c[0], c[1]);
- cout << "\nInvolution the first number.";
- involution(c[0]);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement