Advertisement
dmitryokh

Untitled

Dec 6th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 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) {
  11. real = r;
  12. imag = 0;
  13. }
  14. Complex (double r, double i) {
  15. real = r;
  16. imag = i;
  17. }
  18.  
  19. Complex operator + (Complex& c) {
  20. return Complex (real + c.real, imag + c.imag);
  21. }
  22.  
  23. Complex operator - (Complex& c) {
  24. return Complex (real - c.real, imag - c.imag);
  25. }
  26.  
  27. Complex operator * (Complex& c) {
  28. return Complex (real * c.real - imag * c.imag, imag * c.real + real * c.imag);
  29. }
  30.  
  31. Complex operator / (Complex& c) {
  32. return Complex ((real * c.real + imag * c.imag) / (c.real * c.real + c.imag * c.imag),
  33. (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
  34. }
  35.  
  36. Complex operator + () {
  37. return Complex (abs(real), abs(imag));
  38. }
  39.  
  40. Complex operator - () {
  41. return Complex (abs(real) * (-1), abs(imag) * (-1));
  42. }
  43.  
  44. double Re() {
  45. return real;
  46. };
  47.  
  48. double Im() {
  49. return imag;
  50. }
  51.  
  52. bool operator == (Complex& c) {
  53. return (real == c.real && imag == c.imag);
  54. }
  55.  
  56. bool operator != (Complex& c) {
  57. return (real != c.real || imag != c.imag);
  58. }
  59.  
  60. ostream& operator << (ostream& out)
  61. {
  62. out << "(" << real << ", " << imag << ")";
  63. return out;
  64. }
  65.  
  66. };
  67.  
  68. double abs (Complex& c) {
  69. return sqrt (c.real * c.real + c.imag * c.imag);
  70. }
  71.  
  72. int main() {
  73. Complex a (1, 1), b (2, 2);
  74. cout << a;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement