Advertisement
Five_NT

source.cpp

Mar 9th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include "header.h"
  4.  
  5. using namespace std;
  6.  
  7. Complex::~Complex() {
  8.     delete x;
  9.     delete y;
  10.     x = 0;
  11.     y = 0;
  12. }
  13.  
  14. Complex::Complex() {
  15.     x = new int;
  16.     y = new int;
  17.     this->x[0] = 0;
  18.     this->y[0] = 0;
  19. }
  20.  
  21. Complex::Complex(int real, const char* im) {
  22.     x = new int;
  23.     y = new int;
  24.     this->x[0] = real;
  25.  
  26.     char imag[16];
  27.     strcpy(imag, im);
  28.     imag[strlen(imag) - 1] = '\0';
  29.     this->y[0] = atoi(imag);
  30. }
  31.  
  32. Complex::Complex(int real) {
  33.     x = new int;
  34.     y = new int;
  35.     this->x[0] = real;
  36.     this->y[0] = 0;
  37. }
  38.  
  39. Complex::Complex(const char* im) {
  40.     this->x = new int;
  41.     this->y = new int;
  42.     char imag[16];
  43.     strcpy(imag, im);
  44.     imag[strlen(imag) - 1] = '\0';
  45.  
  46.     this->x[0] = 0;
  47.     this->y[0] = atoi(imag);
  48. }
  49.  
  50. /** Constructor de copiere */
  51. Complex::Complex(const Complex &c) {
  52.     x = new int;
  53.     y = new int;
  54.     this->x[0] = c.x[0];// getX();
  55.     this->y[0] = c.y[0];// getY();
  56. }
  57.  
  58. /** Adunarea a doua numere complexe */
  59. Complex Complex::add(Complex b) {
  60.     Complex c;
  61.    
  62.     c.x[0] = this->x[0] + b.x[0];
  63.     c.y[0] = this->y[0] + b.y[0];
  64.  
  65.     return c;
  66. }
  67.  
  68. /** Aduna la partea reala */
  69. Complex Complex::add(int var) {
  70.     Complex c;
  71.     c.x[0] = this->x[0] + var;
  72.     c.y[0] = this->y[0];
  73.  
  74.     return c;
  75. }
  76.  
  77. /** Scaderea a doua numere complexe */
  78. Complex Complex::minus(Complex b) {
  79.     Complex c;
  80.     c.x[0] = this->x[0] - b.x[0];
  81.     c.y[0] = this->y[0] - b.y[0];
  82.  
  83.     return c;
  84. }
  85.  
  86. /** Inmultirea a doua numere complexe */
  87. Complex Complex::mul(Complex b) {
  88.     Complex c;
  89.     c.x[0] = (this->x[0] * b.x[0]) - (this->y[0] * b.y[0]); // x = x1 * y1 - x2 * y2
  90.     c.y[0] = (this->x[0] * b.y[0]) + (b.x[0] * this->y[0]); // y = x1 * y2 + x2 * y1
  91.  
  92.     return c;
  93. }
  94.  
  95. /** Impartirea a doua numere complexe */
  96. Complex Complex::div(Complex b) {
  97.     Complex c;
  98.  
  99.     c.x[0] = (this->x[0] * b.x[0] + this->y[0] * b.y[0]) / (this->y[0] * this->y[0] + b.y[0] * b.y[0]); // (a1a2 + b1b2 ) / (a2a2 + b2b2)
  100.     c.y[0] = (this->y[0] * b.x[0] - this->x[0] * b.y[0]) / (this->y[0] * this->y[0] + b.y[0] * b.y[0]); // (a2b1 - a1b2) / (a2a2 + b2b2)
  101.  
  102.     return c;
  103. }
  104.  
  105. void Complex::print() {
  106.     cout << this->x[0] << " + " << this->y[0] << "i\n";
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement