Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T>
  7. class Polynomial {
  8. private:
  9.     vector<T> coef;
  10.  
  11. public:
  12.     Polynomial(vector<T>& v){
  13.         int index = v.size() - 1;
  14.         while (v[index] == T()) {
  15.             --index;
  16.         }
  17.         coef.resize(index + 1);
  18.         for (int i = 0; i <= index; ++i) {
  19.             coef[i] = v[i];
  20.         }
  21.     }
  22.  
  23.     Polynomial(T x = T()) {
  24.         if (x != T()) {
  25.             coef.resize(1, x);
  26.         }
  27.     }
  28.  
  29.     template<typename It>
  30.     Polynomial(It begin, It end) {
  31.         while (begin != end) {
  32.             coef.push_back((*begin));
  33.             ++begin;
  34.         }
  35.     }
  36.  
  37.     int Degree() const {
  38.         return static_cast<int>(coef.size()) - 1;
  39.     }
  40.  
  41.     T operator [](int i) const {
  42.         if (i > (*this).Degree()) {
  43.             return T();
  44.         }
  45.         return coef[i];
  46.     }
  47.  
  48.     void normal() {
  49.         int index = (*this).Degree();
  50.         if (index == -1) {
  51.             return;
  52.         }
  53.         while (coef[index] == T()) {
  54.             --index;
  55.         }
  56.         vector<T> temp(index + 1);
  57.         for (int i = 0; i <= index; ++i) {
  58.             temp[i] = coef[i];
  59.         }
  60.         (*this) = Polynomial<T>(temp);
  61.     }
  62.  
  63.     bool operator ==(const Polynomial<T>& other) const {
  64.         Polynomial<T> temp1 = (*this);
  65.         Polynomial<T> temp2 = other;
  66.         temp1.normal();
  67.         temp2.normal();
  68.         if (temp1.Degree() != temp2.Degree()) {
  69.             return false;
  70.         }
  71.         for (int i = 0; i < temp2.Degree(); ++i) {
  72.             if (temp1[i] != temp2[i]) {
  73.                 return false;
  74.             }
  75.         }
  76.         return true;
  77.     }
  78.  
  79.     bool operator !=(const Polynomial<T>& other) const {
  80.         return !((*this) == other);
  81.     }
  82.  
  83.     typename vector<T>::const_iterator begin() const {
  84.         return coef.begin();
  85.     }
  86.  
  87.     typename vector<T>::const_iterator end() const {
  88.         return coef.end();
  89.     }
  90.  
  91.     Polynomial<T> operator +(const Polynomial<T>& other) const {
  92.         vector<T> result(max((*this).Degree(), other.Degree()) + 1);
  93.         for (int i = 0; i < min((*this).Degree(), other.Degree()) + 1; ++i) {
  94.             result[i] = coef[i] + other[i];
  95.         }
  96.         if ((*this).Degree() > other.Degree()) {
  97.             for (int i = other.Degree() + 1; i < (*this).Degree() + 1; ++i) {
  98.                 result[i] = coef[i];
  99.             }
  100.         } else {
  101.             for (int i = (*this).Degree() + 1; i < other.Degree() + 1; ++i) {
  102.                 result[i] = other[i];
  103.             }
  104.         }
  105.         auto ret = Polynomial(result);
  106.         ret.normal();
  107.         return ret;
  108.     }
  109.  
  110.     Polynomial<T> operator -(const Polynomial<T>& other) const {
  111.         vector<T> result(max((*this).Degree(), other.Degree()) + 1);
  112.         for (int i = 0; i < min((*this).Degree(), other.Degree()) + 1; ++i) {
  113.             result[i] = coef[i] - other[i];
  114.         }
  115.         if ((*this).Degree() > other.Degree()) {
  116.             for (int i = other.Degree() + 1; i < (*this).Degree() + 1; ++i) {
  117.                 result[i] = coef[i];
  118.             }
  119.         } else {
  120.             for (int i = (*this).Degree() + 1; i < other.Degree() + 1; ++i) {
  121.                 result[i] = T() - other[i];
  122.             }
  123.         }
  124.         auto ret = Polynomial(result);
  125.         ret.normal();
  126.         return ret;
  127.     }
  128.  
  129.     Polynomial<T>& operator +=(const Polynomial<T>& other) {
  130.         (*this) = (*this) + other;
  131.         return (*this);
  132.     }
  133.  
  134.     Polynomial<T>& operator -=(const Polynomial<T>& other) {
  135.         (*this) = (*this) - other;
  136.         return (*this);
  137.     }
  138. };
  139.  
  140. template<typename T>
  141. Polynomial<T> operator +(const T& self, const Polynomial<T>& other) {
  142.     return Polynomial<T>(self) + other;
  143. }
  144.  
  145. template<typename T>
  146. Polynomial<T> operator -(const T& self, const Polynomial<T>& other) {
  147.     return Polynomial<T>(self) - other;
  148. }
  149.  
  150. template<typename T>
  151. bool operator ==(const T& self, const Polynomial<T>& other) {
  152.     return Polynomial<T>(self) == other;
  153. }
  154.  
  155. template<typename T>
  156. bool operator !=(const T& self, const Polynomial<T>& other) {
  157.     return Polynomial<T>(self) != other;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement