Advertisement
Guest User

Untitled

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