Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. struct Complex {
  10. double re, im;
  11.  
  12. Complex(double re, double im)
  13. {
  14. this->re = re;
  15. this->im = im;
  16. }
  17.  
  18. double modulo()
  19. {
  20. return sqrt(this->re*this->re + this->im*this->im);
  21. }
  22.  
  23. Complex conj()
  24. {
  25. return Complex(re, -im);
  26. }
  27.  
  28. void print()
  29. {
  30. printf("[%lf %lf]", re, im);
  31. }
  32.  
  33. Complex* addr()
  34. {
  35. return this;
  36. }
  37. };
  38.  
  39. Complex operator+(Complex x, Complex y)
  40. {
  41. return Complex(x.re +y.re, x.im + y.im);
  42. }
  43.  
  44. Complex operator-(Complex x, Complex y)
  45. {
  46. return Complex(x.re - y.re, x.im - y.im);
  47. }
  48.  
  49. Complex operator*(Complex x, Complex y)
  50. {
  51. return Complex(x.re*y.re - x.im*y.im, x.re*y.im + x.im*y.re);
  52. }
  53.  
  54. Complex operator/(Complex x, Complex y)
  55. {
  56. double znam = y.re*y.re + y.im*y.im;
  57. return Complex((x.re*y.re + x.im*y.im)/znam, (x.re*y.im - x.im*y.re)/znam);
  58. }
  59.  
  60. void print(Complex x)
  61. {
  62. cout << "(" << x.re << " " << x.im << ")";
  63. }
  64.  
  65. ostream& operator<<(ostream& o, Complex c)
  66. {
  67. o << "[" << c.re << " " << c.im << "]";
  68. return o;
  69. }
  70.  
  71. class NewComplex : public Complex
  72. {
  73. public:
  74. NewComplex() : Complex ()
  75. {}
  76.  
  77. NewComplex(re, im) : Complex (re, im)
  78. {}
  79.  
  80. Complex opposit_vector()
  81. {
  82. return Complex(-im, re);
  83. }
  84. };
  85.  
  86.  
  87. int _tmain(int argc, _TCHAR* argv[])
  88. {
  89. Complex x = Complex(3, -5),
  90. y = Complex(1, -2);
  91.  
  92. Complex res = (x + y)/(x - y);
  93.  
  94. cout << " x = " << x << endl;
  95.  
  96. cout << " y = " << y << endl; cout << endl;
  97. cout << " x + y = " << x + y << endl;
  98. cout << " x - y = " << x - y << endl;
  99. cout << " x * y = " << x * y << endl;
  100. cout << " x / y = " << x / y << endl;
  101.  
  102. cout << "(x + y)/(x - y) = (" << x << " + " << y <<")/(" << x << " - " << y << ") = " << res << endl;
  103. cout << "|((x + y)/(x - y)| = |" << res << "| = " << res.modulo() << endl;
  104.  
  105. cout << " res = " << res << endl;
  106. cout << "!res = " << res.conj() << endl;
  107.  
  108. NewComplex nc(1, 2);
  109.  
  110. cout << nc << nc + 10 << " " << nc.opposit_vector() << endl;
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement