Advertisement
dmitryokh

Untitled

Dec 6th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Complex {
  7. public:
  8. double real, imag;
  9.  
  10. Complex(double r, double i) {
  11. real = r;
  12. imag = i;
  13. }
  14.  
  15. Complex(double r) {
  16. real = r;
  17. imag = 0;
  18. }
  19.  
  20. Complex operator + (const Complex& c) {
  21. return Complex (real + c.real, imag + c.imag);
  22. }
  23.  
  24. Complex operator - (Complex& c) {
  25. return Complex (real - c.real, imag - c.imag);
  26. }
  27.  
  28. Complex operator * (Complex& c) {
  29. return Complex (real * c.real - imag * c.imag, imag * c.real + real * c.imag);
  30. }
  31.  
  32. Complex operator / (Complex& c) {
  33. return Complex ((real * c.real + imag * c.imag) / (c.real * c.real + c.imag * c.imag),
  34. (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
  35. }
  36.  
  37. Complex operator + () {
  38. return Complex (real, imag);
  39. }
  40.  
  41. Complex operator - () {
  42. return Complex (-real, -imag);
  43. }
  44.  
  45. double Re() {
  46. return real;
  47. }
  48.  
  49. double Im() {
  50. return imag;
  51. }
  52.  
  53. bool operator == (Complex& c) {
  54. return (real == c.real && imag == c.imag);
  55. }
  56.  
  57. bool operator != (Complex& c) {
  58. return (real != c.real || imag != c.imag);
  59. }
  60. };
  61.  
  62. double abs(Complex& c) {
  63. return sqrt (c.real * c.real + c.imag * c.imag);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement