HICONT

Compnum.h

Mar 31st, 2022 (edited)
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <locale>
  5. #include <cmath>
  6. using namespace std;
  7. void string_split_smart(string str, string razd, string*& result, int& count) {
  8.     for (int i = 0; i < str.length(); i++) {
  9.         int l = razd.find(str[i]);
  10.         if (l < 0 || l > razd.length()) {
  11.             result[count] += str[i];
  12.         }
  13.         else {
  14.             if (str[i] == ' ') {
  15.                 if (result[count] != "") {
  16.                     result[++count] = "";
  17.                 }
  18.             }
  19.             else {
  20.                 result[++count] = str[i];
  21.             }
  22.         }
  23.     }
  24. }
  25.  
  26. class Complex {
  27. public:
  28.     Complex(int _real = 0, int _im = 0) {
  29.         real = _real;
  30.         im = _im;
  31.  
  32.         real_f = (double)real;
  33.         im_f = (double)im;
  34.  
  35.         mod = sqrt(real_f * real_f + im_f * im_f);
  36.     }
  37.  
  38.     Complex(double _real, double _im = 0.0) {
  39.         real_f = _real;
  40.         im_f = _im;
  41.  
  42.         real = (int)real_f;
  43.         im = (int)im_f;
  44.  
  45.         mod = sqrt(real_f * real_f + im_f * im_f);
  46.     }
  47.  
  48.     Complex(int _real, double _im = 0.0) {
  49.         real_f = (double)_real;
  50.         im_f = _im;
  51.  
  52.         real = _real;
  53.         im = (int)im_f;
  54.  
  55.         mod = sqrt(real_f * real_f + im_f * im_f);
  56.     }
  57.  
  58.     Complex(double _real, int _im = 0) {
  59.         real_f = _real;
  60.         im_f = (double)_im;
  61.  
  62.         real = (int)real_f;
  63.         im = _im;
  64.  
  65.         mod = sqrt(real_f * real_f + im_f * im_f);
  66.     }
  67.  
  68.     int GetReal() {
  69.         return this->real;
  70.     }
  71.  
  72.     int GetIm() {
  73.         return this->im;
  74.     }
  75.  
  76.     double GetMod() {
  77.         mod = sqrt(real_f * real_f + im_f * im_f);
  78.         return this->mod;
  79.     }
  80.  
  81.     void SetReal(int rl) {
  82.         real = rl;
  83.         real_f = (double)real;
  84.         mod = sqrt(real_f * real_f + im_f * im_f);
  85.     }
  86.  
  87.     void SetIm(int iml) {
  88.         im = iml;
  89.         im_f = (double)im;
  90.         mod = sqrt(real_f * real_f + im_f * im_f);
  91.     }
  92.  
  93.     void SetReal(double rl) {
  94.         real_f = rl;
  95.         real = (int)real_f;
  96.         mod = sqrt(real_f * real_f + im_f * im_f);
  97.     }
  98.  
  99.     void SetIm(double iml) {
  100.         im_f = iml;
  101.         im = (int)im_f;
  102.         mod = sqrt(real_f * real_f + im_f * im_f);
  103.     }
  104.  
  105.     ~Complex(void) {
  106.         real = 0;
  107.         im = 0;
  108.         real_f = 0.0;
  109.         im_f = 0.0;
  110.         mod = 0;
  111.     };
  112.  
  113.     Complex operator+(Complex comp) {
  114.         return Complex(real + comp.real, im + comp.im);
  115.     }
  116.  
  117.     Complex operator-(Complex comp) {
  118.         return Complex(real - comp.real, im - comp.im);
  119.     }
  120.  
  121.     Complex operator*(Complex comp) {
  122.         return Complex(real * comp.real - im * comp.im, real * comp.im + comp.real * im);
  123.     }
  124.  
  125.     Complex operator/(Complex comp) {
  126.         double sum_sqrt = comp.real * comp.real + comp.im * comp.im;
  127.  
  128.         if (sum_sqrt == 0.f) {
  129.             cout << "Деление на 0 невозможно, попробуйте ввести другое число" << endl;
  130.             return Complex();
  131.         }
  132.         else {
  133.             double new_real = (double)(real * comp.real + comp.im * im);
  134.             double new_im = (double)(im * comp.real - real * comp.im);
  135.             return Complex(new_real / sum_sqrt, new_im / sum_sqrt);
  136.         }
  137.  
  138.     }
  139.  
  140.     friend ostream& operator<<(ostream& out, const Complex& comp) {
  141.  
  142.         string reals;
  143.         string ims;
  144.  
  145.         if (ceil(comp.real_f) > comp.real) {
  146.             reals = to_string(comp.real_f);
  147.         }
  148.         else {
  149.             reals = to_string(comp.real);
  150.         }
  151.  
  152.         if (ceil(comp.im_f) > comp.im) {
  153.             ims = to_string(comp.im_f);
  154.         }
  155.         else {
  156.             ims = to_string(comp.im);
  157.         }
  158.         if (comp.real_f != 0.0) {
  159.             if (comp.im_f > 1.0) {
  160.                 out << reals << "+" << ims << "i";
  161.             }
  162.             else if (comp.im_f < -1.0) {
  163.                 out << reals << ims << "i";
  164.             }
  165.             else {
  166.                 if (comp.im_f > 0.0) {
  167.                     out << reals << "+i";
  168.                 }
  169.                 else if (comp.im_f < 0.0) {
  170.                     out << reals << "-i";
  171.                 }
  172.                 else {
  173.                     out << reals;
  174.                 }
  175.             }
  176.         }
  177.         else {
  178.  
  179.             if (abs(comp.im_f) == 1.0) {
  180.                 if (comp.im_f > 0.0) {
  181.                     out << "i";
  182.                 }
  183.                 else {
  184.                     out << "-i";
  185.                 }
  186.             }
  187.             else {
  188.                 if (comp.im_f == 0.0) {
  189.                     out << 0;
  190.                 }
  191.                 else {
  192.                     out << ims << "i";
  193.                 }
  194.             }
  195.         }
  196.  
  197.         return out;
  198.     }
  199.  
  200.     friend istream& operator>>(istream& in, Complex& comp) {
  201.         comp.SetReal(0);
  202.         comp.SetIm(0);
  203.  
  204.         string* w = new string[3];
  205.         string str;
  206.         int k = 0;
  207.  
  208.         in >> str;
  209.  
  210.         string_split_smart(str, " +-", w, k);
  211.  
  212.         for (int i = 0; i < k + 1; i++) {
  213.  
  214.             int l = w[i].find("i");
  215.  
  216.             if (l >= 0 && l < str.length()) {
  217.                 w[i].erase(l, 1);
  218.  
  219.                 if (w[i].length() == 0 || w[i] == "-" || w[i] == "+") {
  220.                     w[i] += "1";
  221.                 }
  222.  
  223.                 double im = stod(w[i]);
  224.  
  225.                 comp.SetIm(im);
  226.             }
  227.             else {
  228.  
  229.                 if (w[i] != "") {
  230.                     double real = stod(w[i]);
  231.  
  232.                     comp.SetReal(real);
  233.                 }
  234.             }
  235.         }
  236.  
  237.         return in;
  238.     }
  239. private:
  240.     int real;
  241.     int im;
  242.  
  243.     double real_f;
  244.     double im_f;
  245.     double mod;
  246. };
Add Comment
Please, Sign In to add comment