Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <cmath>
  2.  
  3. class Complex {
  4. private:
  5. double re, im;
  6. public:
  7. Complex(double real = 0, double image = 0) {
  8. re = real;
  9. im = image;
  10. }
  11. double Re() const {
  12. return re;
  13. }
  14. double Im() const {
  15. return im;
  16. }
  17. Complex operator+() {
  18. return *this;
  19. }
  20. Complex operator-() {
  21. return {-re, -im};
  22. }
  23. };
  24.  
  25. Complex operator+(const Complex& c1, const Complex& c2) {
  26. return {c1.Re() + c2.Re(), c1.Im() + c2.Im()};
  27. }
  28.  
  29. Complex operator-(const Complex& c1, const Complex& c2) {
  30. return {c1.Re() - c2.Re(), c1.Im() - c2.Im()};
  31. }
  32.  
  33. Complex operator*(const Complex& c1, const Complex& c2) {
  34. return {c1.Re() * c2.Re() - c1.Im() * c2.Im(), c1.Re() * c2.Im() + c1.Im() * c2.Re()};
  35. }
  36.  
  37. Complex operator/(const Complex& c1, const Complex& c2) {
  38. return {(c1.Re() * c2.Re() + c1.Im() * c2.Im()) / (c2.Re() * c2.Re() + c2.Im() * c2.Im()),
  39. (c2.Re() * c1.Im() - c1.Re() * c2.Im()) / (c2.Re() * c2.Re() + c2.Im() * c2.Im())};
  40. }
  41.  
  42. bool operator==(const Complex& c1, const Complex& c2) {
  43. return c1.Re() == c2.Re() && c1.Im() == c2.Im();
  44. }
  45.  
  46. bool operator!=(const Complex& c1, const Complex& c2) {
  47. return c1.Re() != c2.Re() || c1.Im() != c2.Im();
  48. }
  49.  
  50. double abs(const Complex& c) {
  51. return sqrt(c.Re() * c.Re() + c.Im() * c.Im());
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement