Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include "MyComplex.h"
  2. #include <ostream>
  3.  
  4. Complex::Complex() {}
  5. Complex::Complex(double a, double b) : real(a), imag(b) {}
  6. Complex::~Complex(){}
  7.  
  8. Complex& Complex::add(Complex& other) {
  9.     double rez_real = real + other.real;
  10.     double rez_imag = imag + other.imag;
  11.     return Complex(rez_real, rez_imag);
  12. }
  13.  
  14. Complex& Complex::sub(Complex& other) {
  15.     double rez_real = real - other.real;
  16.     double rez_imag = imag - other.imag;
  17.     return Complex(rez_real, rez_imag);
  18. }
  19.  
  20. Complex& Complex::mul(Complex& other) {
  21.     double rez_real = real * other.real - imag * other.imag;
  22.     double rez_imag = real * other.real + imag * other.imag;
  23.     return Complex(rez_real, rez_imag);
  24. }
  25.  
  26. Complex& Complex::mul(double other) {
  27.     double rez_real = real * other - imag * other;
  28.     double rez_imag = real * other + imag * other;
  29.     return Complex(rez_real, rez_imag);
  30. }
  31.  
  32. Complex& Complex::conj() {
  33.     return Complex(real, -imag);
  34. }
  35.  
  36. double Complex::getReal() const{
  37.     return real;
  38. }
  39. double Complex::getImag() const{
  40.     return imag;
  41. }
  42.  
  43. Complex& Complex::operator+(Complex& other)
  44. {
  45.     return this->add(other);
  46. }
  47.  
  48. Complex & Complex::operator-(Complex& other)
  49. {
  50.     return this->sub(other);
  51. }
  52.  
  53. Complex & Complex::operator*(Complex& other)
  54. {
  55.     return this->mul(other);
  56. }
  57.  
  58. Complex& Complex::operator*(double other) {
  59.     return this->mul(other);
  60. }
  61.  
  62. Complex& Complex::operator~()
  63. {
  64.     return this->conj();
  65. }
  66.  
  67. void Complex::setImag(double a) {
  68.     imag = a;
  69. }
  70.  
  71. void Complex::setReal(double a) {
  72.     real = a;
  73. }
  74.  
  75. std::ostream& operator<<(std::ostream& os,const Complex& c)
  76. {
  77.     os << c.getReal() << "+" << c.getImag() << "i";
  78.     return os;
  79. }
  80.  
  81. std::istream& operator>>(std::istream& is, Complex& c)
  82. {  
  83.     double rea;
  84.     double ima;
  85.     is >> rea;
  86.     is >> ima;
  87.     c.setReal(rea);
  88.     c.setImag(ima);
  89.     return is;
  90. }
  91.  
  92. Complex & operator*(double c,const Complex& comp)
  93. {
  94.     return Complex(c*comp.getReal(), c*comp.getImag());
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement