mramine364

complex.cpp

Jul 4th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #include "complex.h"
  2.  
  3. complex::~complex(){}
  4.  
  5. complex::complex(float x, float y){
  6.     cout << "complex number created.\n";
  7.     this->x = x;
  8.     this->y = y;
  9. }
  10.  
  11. float complex::abs(){
  12.     return sqrt(this->x*this->x + this->y*this->y);
  13. }
  14.  
  15. complex* complex::conjugate(){
  16.    
  17.     complex* ptr = new complex(this->x, -this->y);
  18.     return ptr;
  19. }
  20.  
  21. complex* complex::operator+(const complex& z){
  22.     complex* res = new complex(this->x + z.x, this->y + z.y);
  23.     return res;
  24. }
  25.  
  26. complex* complex::operator-(const complex& z){
  27.     complex* res = new complex(this->x - z.x, this->y - z.y);
  28.     return res;
  29. }
  30.  
  31. complex* complex::operator*(const complex& z){
  32.     complex* res = new complex;
  33.     res->x = this->x * z.x - this->y * z.y;
  34.     res->y = this->x * z.y + this->y * z.x;
  35.     return res;
  36. }
  37.  
  38. complex* complex::operator/(const complex& z){
  39.     complex* ptr = new complex;
  40.     ptr->x = (this->x*z.x + this->y*z.y) / (z.x*z.x + z.y*z.y);
  41.     ptr->y = (-this->x*z.y + this->y*z.x) / (z.x*z.x + z.y*z.y);
  42.     return ptr;
  43. }
  44.  
  45. complex* complex::operator+(float z){
  46.     complex* res = new complex(this->x + z, this->y);
  47.     return res;
  48. }
  49.  
  50. complex* complex::operator-(float z){
  51.     complex* res = new complex(this->x - z, this->y);
  52.     return res;
  53. }
  54.  
  55. complex* complex::operator*(float z){
  56.     complex* res = new complex;
  57.     res->x = this->x * z;
  58.     res->y = this->y * z;
  59.     return res;
  60. }
  61.  
  62. complex* complex::operator/(float z){
  63.     complex* ptr = new complex;
  64.     ptr->x = this->x / z;
  65.     ptr->y = this->y / z;
  66.     return ptr;
  67. }
  68.  
  69. complex* complex::operator+=(const complex& z){
  70.     this->x += z.x;
  71.     this->y += z.y;
  72.     return this;
  73. }
  74.  
  75. complex* complex::operator-=(const complex& z){
  76.     this->x -= z.x;
  77.     this->y -= z.y;
  78.     return this;
  79. }
  80.  
  81. complex* complex::operator*=(const complex& z){
  82.     float x = this->x * z.x - this->y * z.y;
  83.     float y = this->x * z.y + this->y * z.x;
  84.     this->x = x; this->y = y;
  85.     return this;
  86. }
  87.  
  88. complex* complex::operator/=(const complex& z){
  89.     float x = (this->x*z.x + this->y*z.y) / (z.x*z.x + z.y*z.y);
  90.     float y = (-this->x*z.y + this->y*z.x) / (z.x*z.x + z.y*z.y);
  91.     this->x = x; this->y = y;
  92.     return this;
  93. }
  94.  
  95. complex* complex::operator+=(float z){
  96.     this->x += z;
  97.     return this;
  98. }
  99.  
  100. complex* complex::operator-=(float z){
  101.     this->x -= z;
  102.     return this;
  103. }
  104.  
  105. complex* complex::operator*=(float z){
  106.     this->x *= z;
  107.     this->y *= z;
  108.     return this;
  109. }
  110.  
  111. complex* complex::operator/=(float z){
  112.     this->x /= z;
  113.     this->y /= z;
  114.     return this;
  115. }
  116.  
  117. ostream& operator<<(ostream& out, const complex& z){
  118.     out << z.x << "+" << z.y << "i";
  119.     return out;
  120. }
  121.  
  122. istream& operator>>(istream& in, complex& z){
  123.     cout << "x: ";
  124.     in >> z.x;
  125.     cout << "y: ";
  126.     in >> z.y;
  127.     return in;
  128. }
  129.  
  130. complex* complex::e() {
  131.     complex* ptr = new complex;
  132.     ptr->x = (float)exp(x)*(float)cos(y);
  133.     ptr->y = (float)exp(x)*(float)sin(y);
  134.     return ptr;
  135. }
  136.  
  137. complex* complex::sinus() {
  138.     complex* ptr = new complex;
  139.     ptr->x = (float)sin(x)*(float)cosh(y);
  140.     ptr->y = (float)cos(x)*(float)sinh(y);
  141.     return ptr;
  142. }
  143.  
  144. complex* complex::cosinus() {
  145.     complex* ptr = new complex;
  146.     ptr->x = (float)cos(x)*(float)cosh(y);
  147.     ptr->y = -(float)sin(x)*(float)sinh(y);
  148.     return ptr;
  149. }
  150.  
  151. complex* complex::tangent() {
  152.     complex* ptr = sinus();
  153.     complex* ptr2 = cosinus();
  154.     complex* ptr3 = *ptr / *ptr2;
  155.     delete ptr, ptr2;
  156.     return ptr3;
  157. }
  158.  
  159. complex* complex::reciprocal(){
  160.     float scale = x*x + y*y;
  161.     complex* ptr = new complex(x / scale, -y / scale);
  162.     return ptr;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment