olexander_dan

Untitled

Apr 9th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 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.     }
  124. }
  125. bool Polynom::pop(Monom& m) {
  126.     if (first == nullptr)
  127.         return false;
  128.     m.coef = first->coef;
  129.     m.pow = first->pow;
  130.     Monom* tempDel = first;
  131.     first = first->next;
  132.     delete tempDel;
  133.     return true;
  134. }
  135. void Polynom::dispose() {
  136.     Monom temp;
  137.     while (pop(temp));
  138. }
  139.  
  140. void Polynom::in(std::istream& ifs, std::string inputStreamType) {
  141.     short counter = -1;
  142.     double tempCoef, tempPow;
  143.     if (inputStreamType == "keyboard") {
  144.         while (true) {
  145.             std::cout << "Coef: ";
  146.             if (!(ifs >> tempCoef))
  147.                 return;
  148.             else {
  149.                 std::cout << "Pow: ";
  150.                 if (!(ifs >> tempPow))
  151.                     return;
  152.             }
  153.             Monom tempMonom{ tempCoef, tempPow };
  154.             push(tempMonom);
  155.             ++counter;
  156.         }
  157.         if (counter == -1)
  158.             throw std::logic_error("Keyboard input is empty!");
  159.     } else
  160.         while (!(ifs.eof())) {
  161.             ifs >> tempCoef >> tempPow;
  162.             Monom tempMonom{ tempCoef, tempPow };
  163.             push(tempMonom);
  164.         }
  165. }
  166.  
  167. void Polynom::out(std::ostream& ofs, std::string outputStreamType) const {
  168.     if (outputStreamType != "console")
  169.         for (auto i = begin(); i != end(); ++i) {
  170.             Monom& tempM = *i;
  171.             if (abs(tempM.coef - 0.) < eps)
  172.                 continue;
  173.             else if (abs(tempM.coef - 1.) < eps && abs(tempM.pow - 0.) > eps)
  174.                 ofs << "x^" << tempM.pow << '+';
  175.             else if (abs(tempM.pow - 0.) < eps)
  176.                 ofs << tempM.coef << '+';
  177.             else if (abs(tempM.pow - 1.) < eps)
  178.                 ofs << tempM.coef << "*x +";
  179.             else
  180.                 ofs << tempM.coef << "*x^" << tempM.pow << '+';
  181.         }
  182.     else
  183.         for (auto i = begin(); i != end(); ++i) {
  184.             Monom& tempM = *i;
  185.             ofs << tempM.coef << ' ' << tempM.pow << std::endl;
  186.         }
  187. }
  188.  
  189. void operator >>(std::istream& ifs, Polynom& p) {
  190.     if (&ifs == &std::cin)
  191.         p.in(ifs, "keyboard");
  192.     else
  193.         p.in(ifs, "file");
  194.     close(ifs);
  195. }
  196. void operator <<(std::ostream& ofs, const Polynom& p) {
  197.     if (&ofs == &std::cout)
  198.         p.out(ofs, "console");
  199.     else
  200.         p.out(ofs, "file");
  201.     close(ofs);
  202. }
  203. void close(std::istream& s) {
  204.     if (&s == &std::cin) {
  205.         s.clear(); s.sync();
  206.     } else
  207.         ((std::ifstream&)s).close();
  208. }
  209. void close(std::ostream& s) {
  210.     if (&s != &std::cout)
  211.         ((std::ofstream&)s).close();
  212. }
  213.  
  214. Polynom::Iter::Iter(Monom* init) : current{init} {}
  215. Polynom::Monom& Polynom::Iter::operator*() {
  216.     if (current != nullptr)
  217.         return *current;
  218.     else
  219.         throw std::out_of_range("Memory access violation due to * operator");
  220. }
  221. Polynom::Iter& Polynom::Iter::operator++() {
  222.     if (current != nullptr)
  223.         current = current->next;
  224.     else
  225.         throw std::out_of_range("Memory access violation due to ++i operator");
  226.     return *this;
  227. }
  228. Polynom::Iter& Polynom::Iter::operator++(int) {
  229.     Iter temp = *this;
  230.     if (current != nullptr)
  231.         current = current->next;
  232.     else
  233.         throw std::out_of_range("Memory access violation due to i++ operator");
  234.     return *this;
  235. }
  236. bool Polynom::Iter::operator==(const Iter& it) { return (current == it.current); }
  237. bool Polynom::Iter::operator!=(const Iter& it) { return (current != it.current); }
  238.  
  239. Polynom::Iter Polynom::begin() const { return Iter(first); }
  240. Polynom::Iter Polynom::end() const { return Iter(nullptr); }
Add Comment
Please, Sign In to add comment