Advertisement
Guest User

tcomplex

a guest
Apr 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef TCOMPLEX_H
  2. #define TCOMPLEX_H
  3. #include "tanumber.h"
  4.  
  5. class TComplex : public TANumber
  6. {
  7. public:
  8.     explicit TComplex() : real(0), imaginary(0) {}
  9.  
  10.     TComplex(double real, double imaginary) : real(real), imaginary(imaginary)
  11.     {}
  12.  
  13.     TComplex(std::string complexString)
  14.     {
  15.         try {
  16.             size_t pos = complexString.find("+i*");
  17.             int k = 1;
  18.             if (pos == std::string::npos) {
  19.                 pos = complexString.find("-i*");
  20.                 k = -1;
  21.             }
  22.             if (pos == std::string::npos) {
  23.                 real = std::stod(complexString);
  24.                 imaginary = 0;
  25.             }
  26.             else {
  27.                 real = std::stod(complexString.substr(0, pos));
  28.                 imaginary = std::stod(complexString.substr(pos + 3)) * k;
  29.             }
  30.         }
  31.         catch (...) {
  32.             real = 0;
  33.             imaginary = 0;
  34.         }
  35.     }
  36.  
  37.     ~TComplex()
  38.     {}
  39.  
  40.     TComplex* summ(TComplex *firstComplex, TComplex *secondComplex);
  41.     TComplex* substract(TComplex firstComplex, TComplex secondComplex) override;
  42.     TComplex* mult(TComplex firstComplex, TComplex secondComplex) override;
  43.     TComplex* divide(TComplex firstComplex, TComplex secondComplex) override;
  44.  
  45.  
  46.     TComplex square(TComplex complex);
  47.     TComplex reverse(TComplex complex);
  48.     TComplex power(int index);
  49.  
  50.  
  51.     std::string getNumberAsString() override;
  52.  
  53. private:
  54.     double real;
  55.     double imaginary;
  56. };
  57.  
  58. #endif // TCOMPLEX_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement