olexander_dan

Untitled

Apr 10th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.94 KB | None | 0 0
  1. //LABKA 2-4 v.1 Class implementation
  2. #include <iostream>
  3. #include <fstream>
  4. #include "classes.h"
  5.  
  6. constexpr double eps = 1e-7;
  7.  
  8. Polynom::Polynom(const Polynom& p) {
  9.     for (auto i = p.begin(); i != p.end(); ++i)
  10.         push(*i);
  11. }
  12.  
  13. Polynom::Polynom(Polynom&& p) {
  14.     dispose();
  15.     if (this != &p)
  16.         for (auto i = p.begin(); i != p.end(); ++i)
  17.             push(*i);
  18. }
  19.  
  20. Polynom& Polynom::operator=(const Polynom& p) {
  21.     for (auto i = p.begin(); i != p.end(); ++i)
  22.         push(*i);
  23.     return *this;
  24. }
  25.  
  26. Polynom& Polynom::operator=(Polynom&& p) {
  27.     dispose();
  28.     if (this != &p)
  29.         for (auto i = p.begin(); i != p.end(); ++i)
  30.             push(*i);
  31.     return *this;
  32. }
  33.  
  34. Polynom Polynom::operator+(const Polynom& p) const {
  35.     double coefA, coefB, resultCoef;
  36.     Polynom resultP;
  37.     auto iA = begin();
  38.     auto iB = p.begin();
  39.     while (iA != nullptr && iB != nullptr) {
  40.         Monom& tempA = *iA;
  41.         Monom& tempB = *iB;
  42.         if (tempA.pow < tempB.pow) {
  43.             resultP.push(tempA);
  44.             ++iA;
  45.         } else if (tempA.pow > tempB.pow) {
  46.             resultP.push(tempB);
  47.             ++iB;
  48.         } else {
  49.             coefA = tempA.coef;
  50.             coefB = tempB.coef;
  51.             resultCoef = coefA + coefB;
  52.             Monom resultM{ resultCoef, tempA.pow };
  53.             resultP.push(resultM);
  54.         }
  55.     }
  56.     return resultP;
  57. }
  58.  
  59. Polynom Polynom::operator-(const Polynom& p) const {
  60.     double coefA, coefB, resultCoef;
  61.     Polynom resultP;
  62.     auto iA = begin();
  63.     auto iB = p.begin();
  64.     while (iA != nullptr && iB != nullptr) {
  65.         Monom& tempA = *iA;
  66.         Monom& tempB = *iB;
  67.         if (tempA.pow < tempB.pow) {
  68.             resultP.push(tempA);
  69.             ++iA;
  70.         } else if (tempA.pow > tempB.pow) {
  71.             resultP.push(tempB);
  72.             ++iB;
  73.         } else {
  74.             coefA = tempA.coef;
  75.             coefB = tempB.coef;
  76.             resultCoef = coefA - coefB;
  77.             Monom resultM{ resultCoef, tempA.pow };
  78.             resultP.push(resultM);
  79.         }
  80.     }
  81.     return resultP;
  82. }
  83.  
  84. Polynom& Polynom::operator+=(const Polynom& p) const {
  85.     double coefA, coefB, resultCoef;
  86.     Polynom resultP;
  87.     auto iA = begin();
  88.     auto iB = p.begin();
  89.     while (iA != nullptr && iB != nullptr) {
  90.         Monom& tempA = *iA;
  91.         Monom& tempB = *iB;
  92.         if (tempA.pow < tempB.pow) {
  93.             resultP.push(tempA);
  94.             ++iA;
  95.         } else if (tempA.pow > tempB.pow) {
  96.             resultP.push(tempB);
  97.             ++iB;
  98.         } else {
  99.             coefA = tempA.coef;
  100.             coefB = tempB.coef;
  101.             resultCoef = coefA + coefB;
  102.             Monom resultM{ resultCoef, tempA.pow };
  103.             resultP.push(resultM);
  104.         }
  105.     }
  106.     return resultP;
  107. }
  108.  
  109. Polynom::~Polynom() { dispose(); }
  110.  
  111. void Polynom::push(const Monom& m) {
  112.     if (first == nullptr || first->pow > m.pow) {
  113.         Monom* temp = new Monom{ m.coef, m.pow, m.next };
  114.         first = temp;
  115.     } else {
  116.         Monom* seek = first;
  117.         while (seek->next != nullptr && seek->next->pow <= m.pow)
  118.             seek = seek->next;
  119.         if (seek->pow < m.pow) {
  120.             Monom* temp = new Monom{ m.coef, m.pow, m.next };
  121.             seek->next = temp;
  122.         }
  123.         else if (seek->pow == m.pow)
  124.             seek->coef += m.coef;
  125.     }
  126. }
  127. bool Polynom::pop(Monom& m) {
  128.     if (first == nullptr)
  129.         return false;
  130.     m.coef = first->coef;
  131.     m.pow = first->pow;
  132.     Monom* tempDel = first;
  133.     first = first->next;
  134.     delete tempDel;
  135.     return true;
  136. }
  137. void Polynom::dispose() {
  138.     Monom temp;
  139.     while (pop(temp));
  140. }
  141.  
  142. void Polynom::in(std::istream& ifs, std::string inputStreamType) {
  143.     short counter = -1;
  144.     double tempCoef, tempPow;
  145.     if (inputStreamType == "keyboard") {
  146.         while (true) {
  147.             std::cout << "Coef: ";
  148.             if (!(ifs >> tempCoef)) {
  149.                 ifs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  150.                 break;
  151.             } else {
  152.                 std::cout << "Pow: ";
  153.                 if (!(ifs >> tempPow)) {
  154.                     ifs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  155.                     break;
  156.                 }
  157.             }
  158.             Monom tempMonom{ tempCoef, tempPow };
  159.             push(tempMonom);
  160.             ++counter;
  161.         }
  162.         if (counter == -1)
  163.             throw std::logic_error("Keyboard input is empty!");
  164.     } else
  165.         while (!(ifs.eof())) {
  166.             ifs >> tempCoef >> tempPow;
  167.             Monom tempMonom{ tempCoef, tempPow };
  168.             push(tempMonom);
  169.         }
  170. }
  171.  
  172. void Polynom::out(std::ostream& ofs, std::string outputStreamType) const {
  173.     if (outputStreamType != "console")
  174.         for (auto i = begin(); i != end(); ++i) {
  175.             Monom& tempM = *i;
  176.             if (abs(tempM.coef - 0.) < eps)
  177.                 continue;
  178.             else if (abs(tempM.coef - 1.) < eps && abs(tempM.pow - 0.) > eps)
  179.                 ofs << "x^" << tempM.pow << '+';
  180.             else if (abs(tempM.pow - 0.) < eps)
  181.                 ofs << tempM.coef << '+';
  182.             else if (abs(tempM.pow - 1.) < eps)
  183.                 ofs << tempM.coef << "*x +";
  184.             else
  185.                 ofs << tempM.coef << "*x^" << tempM.pow << '+';
  186.         }
  187.     else
  188.         for (auto i = begin(); i != end(); ++i) {
  189.             Monom& tempM = *i;
  190.             ofs << tempM.coef << ' ' << tempM.pow << std::endl;
  191.         }
  192. }
  193.  
  194. void operator >>(std::istream& ifs, Polynom& p) {
  195.     if (&ifs == &std::cin)
  196.         p.in(ifs, "keyboard");
  197.     else
  198.         p.in(ifs, "file");
  199.     close(ifs);
  200. }
  201. void operator <<(std::ostream& ofs, const Polynom& p) {
  202.     if (&ofs == &std::cout)
  203.         p.out(ofs, "console");
  204.     else
  205.         p.out(ofs, "file");
  206.     close(ofs);
  207. }
  208. void close(std::istream& s) {
  209.     if (&s == &std::cin) {
  210.         s.clear(); s.sync();
  211.     } else
  212.         ((std::ifstream&)s).close();
  213. }
  214. void close(std::ostream& s) {
  215.     if (&s != &std::cout)
  216.         ((std::ofstream&)s).close();
  217. }
  218.  
  219. Polynom::Iter::Iter(Monom* init) : current{init} {}
  220. Polynom::Monom& Polynom::Iter::operator*() {
  221.     if (current != nullptr)
  222.         return *current;
  223.     else
  224.         throw std::out_of_range("Memory access violation due to * operator");
  225. }
  226. Polynom::Iter& Polynom::Iter::operator++() {
  227.     if (current != nullptr)
  228.         current = current->next;
  229.     else
  230.         throw std::out_of_range("Memory access violation due to ++i operator");
  231.     return *this;
  232. }
  233. Polynom::Iter& Polynom::Iter::operator++(int) {
  234.     Iter temp = *this;
  235.     if (current != nullptr)
  236.         current = current->next;
  237.     else
  238.         throw std::out_of_range("Memory access violation due to i++ operator");
  239.     return *this;
  240. }
  241. bool Polynom::Iter::operator==(const Iter& it) { return (current == it.current); }
  242. bool Polynom::Iter::operator!=(const Iter& it) { return (current != it.current); }
  243.  
  244. Polynom::Iter Polynom::begin() const { return Iter(first); }
  245. Polynom::Iter Polynom::end() const { return Iter(nullptr); }
Add Comment
Please, Sign In to add comment