Advertisement
kokokozhina

work_w/_complex_numbers

Dec 11th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <iostream> //exercise 16
  2. #include <vector> //realize functions for complex numbers: input&output in algebraic and trigonometric form,
  3. #include <math.h> //determination of modulus and argument of number, addition, subtraction, multiplication and division of complex numbers, involution
  4.  
  5. #define PI 3.14159265 //a + bi == re + im * i
  6.  
  7. using namespace std;
  8.  
  9. struct complex
  10. {
  11.     complex();
  12.     double re; //real part of complex number
  13.     double im; //imaginary part
  14.     double r; //modulus
  15.     double u; //the angle between the representing number vector and the positive direction of the Ox(argument)
  16.     complex(double re, double im, double r, double u) : re(re), im(im), r(r), u(u) {};
  17. };
  18.  
  19. vector<complex> c;
  20.  
  21. void printAlg(complex c)
  22. {
  23.     cout.setf(ios::showpos);//explicit sign identification
  24.     cout << c.re << c.im << "i\n";
  25. }
  26.  
  27. void printTrig(complex c)
  28. {
  29.     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
  30. }
  31.  
  32. void modArg(complex& c)//modulus and argument
  33. {
  34.     c.r = sqrt(c.re * c.re + c.im * c.im);
  35.     if (c.re == 0)
  36.     {
  37.         if (c.im >= 0)
  38.             c.u = PI / 2;
  39.         else
  40.             c.u = PI * 3 / 2;
  41.     }
  42.     else
  43.     {
  44.         double ccos = c.re / c.r;
  45.         if (ccos >= 0)
  46.             c.u = atan(c.im / c.re);
  47.         else
  48.             c.u = PI + atan(c.im / c.re);
  49.     }
  50. }
  51.  
  52. void algForm(complex& c)
  53. {
  54.     c.re = c.r * cos(c.u);
  55.     c.im = c.r * sin(c.u);
  56. }
  57.  
  58.  
  59. void input()
  60. {
  61.     bool inputForm;
  62.     cout << "If you want to input comlex number in algebraic form, press 1;\nIf in trigonometric, press 0.\n";
  63.     cin >> inputForm;
  64.     if (inputForm)
  65.     {
  66.         cout << "Algebraic form: a + bi. Enter a and b.\n";
  67.         double a, b;
  68.         cin >> a >> b;
  69.         c.push_back(complex(a, b, 0, 0));
  70.         modArg(c.back());
  71.     }
  72.     else
  73.     {
  74.         cout << "Trigonometric form: r(cos u + sin u. Enter r and u(an angle should be expressed in degrees).\n";
  75.         double r, u;
  76.         cin >> r >> u;
  77.         u = u * PI / 180; //trigonometric functions work with radian-format
  78.         c.push_back(complex(0, 0, r, u));
  79.         algForm(c.back());
  80.     }
  81. }
  82.  
  83. void addition(complex a, complex b)//+
  84. {
  85.     c.push_back(complex(a.re + b.re, a.im + b.im, 0, 0));
  86.     modArg(c.back());
  87.     cout << "\nAddition\n";
  88.     printAlg(c.back());
  89.     printTrig(c.back());
  90. }
  91.  
  92. void subtraction(complex a, complex b)//-
  93. {
  94.     c.push_back(complex(a.re - b.re, a.im - b.im, 0, 0));
  95.     modArg(c.back());
  96.     cout << "\nSubtraction\n";
  97.     printAlg(c.back());
  98.     printTrig(c.back());
  99. }
  100.  
  101. void multiplication(complex a, complex b)//*
  102. {
  103.     c.push_back(complex(a.re * b.re - a.im * b.im, a.im * b.re + a.re * b.im, 0, 0));
  104.     modArg(c.back());
  105.     cout << "\nMultiplication\n";
  106.     printAlg(c.back());
  107.     printTrig(c.back());
  108. }
  109.  
  110. void division(complex a, complex b)//:
  111. {
  112.     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));
  113.     modArg(c.back());
  114.     cout << "\nDivision\n";
  115.     printAlg(c.back());
  116.     printTrig(c.back());
  117. }
  118.  
  119. void involution(complex a)//a^x
  120. {
  121.     cout << "\nFor involution enter the degree number: ";
  122.     int n;
  123.     cin >> n;
  124.     double r = 1;
  125.     for(int i = 0; i < n; i++)
  126.         r *= a.r;
  127.     c.push_back(complex(0, 0, r, n * a.u));
  128.     cout << "\nInvolution\n";
  129.     printTrig(c.back());
  130. }
  131.  
  132. int main()
  133. {
  134.     cout << "For some operations we need to take 2 complex numbers, so you should enter 2 complex numbers.\n";
  135.     input();
  136.     input();
  137.     addition(c[0], c[1]);
  138.     subtraction(c[0], c[1]);
  139.     multiplication(c[0], c[1]);
  140.     cout << "\nDevide the first number be the second.";
  141.     division(c[0], c[1]);
  142.     cout << "\nInvolution the first number.";
  143.     involution(c[0]);
  144.  
  145.     system("pause");
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement