Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //LABKA 2-4 v.1 Class implementation
- #include <iostream>
- #include <fstream>
- #include "classes.h"
- constexpr double eps = 1e-7;
- Polynom::Polynom(const Polynom& p) {
- for (auto i = p.begin(); i != p.end(); ++i)
- push(*i);
- }
- Polynom::Polynom(Polynom&& p) {
- dispose();
- if (this != &p)
- for (auto i = p.begin(); i != p.end(); ++i)
- push(*i);
- }
- Polynom& Polynom::operator=(const Polynom& p) {
- for (auto i = p.begin(); i != p.end(); ++i)
- push(*i);
- return *this;
- }
- Polynom& Polynom::operator=(Polynom&& p) {
- dispose();
- if (this != &p)
- for (auto i = p.begin(); i != p.end(); ++i)
- push(*i);
- return *this;
- }
- Polynom& Polynom::operator+(const Polynom& p) const {
- double coefA, coefB, resultCoef;
- Polynom resultP;
- auto iA = begin();
- auto iB = p.begin();
- while (iA != nullptr && iB != nullptr) {
- Monom& tempA = *iA;
- Monom& tempB = *iB;
- if (tempA.pow < tempB.pow) {
- resultP.push(tempA);
- ++iA;
- } else if (tempA.pow > tempB.pow) {
- resultP.push(tempB);
- ++iB;
- } else {
- coefA = tempA.coef;
- coefB = tempB.coef;
- resultCoef = coefA + coefB;
- Monom resultM{ resultCoef, tempA.pow };
- resultP.push(resultM);
- }
- }
- return resultP;
- }
- Polynom& Polynom::operator-(const Polynom& p) const {
- double coefA, coefB, resultCoef;
- Polynom resultP;
- auto iA = begin();
- auto iB = p.begin();
- while (iA != nullptr && iB != nullptr) {
- Monom& tempA = *iA;
- Monom& tempB = *iB;
- if (tempA.pow < tempB.pow) {
- resultP.push(tempA);
- ++iA;
- } else if (tempA.pow > tempB.pow) {
- resultP.push(tempB);
- ++iB;
- } else {
- coefA = tempA.coef;
- coefB = tempB.coef;
- resultCoef = coefA - coefB;
- Monom resultM{ resultCoef, tempA.pow };
- resultP.push(resultM);
- }
- }
- return resultP;
- }
- Polynom& Polynom::operator+=(const Polynom& p) const {
- double coefA, coefB, resultCoef;
- Polynom resultP;
- auto iA = begin();
- auto iB = p.begin();
- while (iA != nullptr && iB != nullptr) {
- Monom& tempA = *iA;
- Monom& tempB = *iB;
- if (tempA.pow < tempB.pow) {
- resultP.push(tempA);
- ++iA;
- } else if (tempA.pow > tempB.pow) {
- resultP.push(tempB);
- ++iB;
- } else {
- coefA = tempA.coef;
- coefB = tempB.coef;
- resultCoef = coefA + coefB;
- Monom resultM{ resultCoef, tempA.pow };
- resultP.push(resultM);
- }
- }
- return resultP;
- }
- Polynom::~Polynom() { dispose(); }
- void Polynom::push(const Monom& m) {
- if (first == nullptr || first->pow > m.pow) {
- Monom* temp = new Monom{ m.coef, m.pow, m.next };
- first = temp;
- } else {
- Monom* seek = first;
- while (seek->next != nullptr && seek->next->pow <= m.pow)
- seek = seek->next;
- if (seek->pow < m.pow) {
- Monom* temp = new Monom{ m.coef, m.pow, m.next };
- seek->next = temp;
- }
- }
- }
- bool Polynom::pop(Monom& m) {
- if (first == nullptr)
- return false;
- m.coef = first->coef;
- m.pow = first->pow;
- Monom* tempDel = first;
- first = first->next;
- delete tempDel;
- return true;
- }
- void Polynom::dispose() {
- Monom temp;
- while (pop(temp));
- }
- void Polynom::in(std::istream& ifs, std::string inputStreamType) {
- short counter = -1;
- double tempCoef, tempPow;
- if (inputStreamType == "keyboard") {
- while (true) {
- std::cout << "Coef: ";
- if (!(ifs >> tempCoef))
- return;
- else {
- std::cout << "Pow: ";
- if (!(ifs >> tempPow))
- return;
- }
- Monom tempMonom{ tempCoef, tempPow };
- push(tempMonom);
- ++counter;
- }
- if (counter == -1)
- throw std::logic_error("Keyboard input is empty!");
- } else
- while (!(ifs.eof())) {
- ifs >> tempCoef >> tempPow;
- Monom tempMonom{ tempCoef, tempPow };
- push(tempMonom);
- }
- }
- void Polynom::out(std::ostream& ofs, std::string outputStreamType) const {
- if (outputStreamType != "console")
- for (auto i = begin(); i != end(); ++i) {
- Monom& tempM = *i;
- if (abs(tempM.coef - 0.) < eps)
- continue;
- else if (abs(tempM.coef - 1.) < eps && abs(tempM.pow - 0.) > eps)
- ofs << "x^" << tempM.pow << '+';
- else if (abs(tempM.pow - 0.) < eps)
- ofs << tempM.coef << '+';
- else if (abs(tempM.pow - 1.) < eps)
- ofs << tempM.coef << "*x +";
- else
- ofs << tempM.coef << "*x^" << tempM.pow << '+';
- }
- else
- for (auto i = begin(); i != end(); ++i) {
- Monom& tempM = *i;
- ofs << tempM.coef << ' ' << tempM.pow << std::endl;
- }
- }
- void operator >>(std::istream& ifs, Polynom& p) {
- if (&ifs == &std::cin)
- p.in(ifs, "keyboard");
- else
- p.in(ifs, "file");
- close(ifs);
- }
- void operator <<(std::ostream& ofs, const Polynom& p) {
- if (&ofs == &std::cout)
- p.out(ofs, "console");
- else
- p.out(ofs, "file");
- close(ofs);
- }
- void close(std::istream& s) {
- if (&s == &std::cin) {
- s.clear(); s.sync();
- } else
- ((std::ifstream&)s).close();
- }
- void close(std::ostream& s) {
- if (&s != &std::cout)
- ((std::ofstream&)s).close();
- }
- Polynom::Iter::Iter(Monom* init) : current{init} {}
- Polynom::Monom& Polynom::Iter::operator*() {
- if (current != nullptr)
- return *current;
- else
- throw std::out_of_range("Memory access violation due to * operator");
- }
- Polynom::Iter& Polynom::Iter::operator++() {
- if (current != nullptr)
- current = current->next;
- else
- throw std::out_of_range("Memory access violation due to ++i operator");
- return *this;
- }
- Polynom::Iter& Polynom::Iter::operator++(int) {
- Iter temp = *this;
- if (current != nullptr)
- current = current->next;
- else
- throw std::out_of_range("Memory access violation due to i++ operator");
- return *this;
- }
- bool Polynom::Iter::operator==(const Iter& it) { return (current == it.current); }
- bool Polynom::Iter::operator!=(const Iter& it) { return (current != it.current); }
- Polynom::Iter Polynom::begin() const { return Iter(first); }
- Polynom::Iter Polynom::end() const { return Iter(nullptr); }
Add Comment
Please, Sign In to add comment