Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <sstream>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11. template <typename T>
  12. class Polynomial {
  13. private:
  14.     map<size_t, T> mn;
  15. public:
  16.     void cut_zeroes() {
  17.         for (auto i = mn.begin(); i != mn.end();) {
  18.             if (i->second == T()){
  19.                i = mn.erase(i);
  20.             } else {
  21.                 i++;
  22.             }
  23.         }
  24.     }
  25.  
  26.     Polynomial<T>(const vector<T> &vec) {
  27.         for (size_t i = 0; i < vec.size(); ++i) {
  28.             mn[i] = vec[i];
  29.         }
  30.         cut_zeroes();
  31.     }
  32.  
  33.     Polynomial<T>(const T &zero = T()) {
  34.         mn[0] = (zero);
  35.         cut_zeroes();
  36.     }
  37.  
  38.     template<typename it>
  39.     Polynomial<T>(it f, it l) {
  40.         size_t c = 0;
  41.         while (f != l) {
  42.             mn[c] = *f;
  43.             ++c;
  44.             ++f;
  45.         }
  46.         cut_zeroes();
  47.     }
  48.  
  49.     typename map<size_t, T>::const_iterator begin() const {
  50.         return mn.begin();
  51.     }
  52.  
  53.     typename map<size_t, T>::const_iterator end() const {
  54.         return mn.end();
  55.     }
  56.     typename map<size_t, T>::const_reverse_iterator rbegin() const {
  57.         return mn.rbegin();
  58.     }
  59.  
  60.     typename map<size_t, T>::const_reverse_iterator rend() const {
  61.         return mn.rend();
  62.     }
  63.     T operator[] (size_t a) const {
  64.         if (mn.count(a) == 0) {
  65.             return T();
  66.         }
  67.         return mn.at(a);
  68.     }
  69.     int Degree() const {
  70.         if  (mn.empty()) {
  71.             return -1;
  72.         }
  73.         return (mn.rbegin())->first;
  74.     }
  75.  
  76.     bool operator == (const Polynomial<T>& buff) const {
  77.         return mn == buff.mn;
  78.     }
  79.     bool operator == (const T& buff) const {
  80.         return mn == Polynomial(buff).mn;
  81.     }
  82.     bool operator != (const Polynomial<T>& buff) const {
  83.         return !(mn == buff.mn);
  84.     }
  85.     bool operator != (const T& buff) const {
  86.         return !(mn == Polynomial(buff).mn);
  87.     }
  88.  
  89.     Polynomial& operator += (const Polynomial& sec) {
  90.         for (auto i = sec.mn.begin();
  91.              i != sec.mn.end(); ++i) {
  92.             mn[i->first] += i->second;
  93.         }
  94.         cut_zeroes();
  95.         return *this;
  96.     }
  97.  
  98.     Polynomial& operator -= (const Polynomial& sec) {
  99.         for (auto i = sec.mn.begin();
  100.              i != sec.mn.end(); ++i) {
  101.             mn[i->first] -= i->second;
  102.         }
  103.         cut_zeroes();
  104.         return *this;
  105.     }
  106.     Polynomial& operator *= (const Polynomial& sec) {
  107.         Polynomial<T> ans;
  108.         for (auto i = mn.begin(); i != mn.end(); ++i) {
  109.             for (auto j = sec.mn.begin(); j != sec.mn.end(); ++j) {
  110.                 ans.mn[i->first + j->first] += i->second * j->second;
  111.             }
  112.         }
  113.         *this = ans;
  114.         cut_zeroes();
  115.         return *this;
  116.     }
  117.     T operator() (const T& x) const {
  118.         T ans;
  119.         for (auto it = mn.begin(); it != mn.end(); ++it) {
  120.             T mult = T(1);
  121.             for (int j = 0; j != it->first; j++) {
  122.                 mult *= x;
  123.             }
  124.             ans += it->second * mult;
  125.         }
  126.         return ans;
  127.     }
  128.     Polynomial<T>& operator /= (const Polynomial<T>& sec) {
  129.         Polynomial ans, q(*this);
  130.         if ((*this).Degree() < sec.Degree()) {
  131.             *this = Polynomial<T>();
  132.             return *this;
  133.         }
  134.         while (q.Degree() >= sec.Degree()) {
  135.             int pos = q.Degree() - sec.Degree();
  136.             ans.mn[pos] = q.rbegin()->second / sec.rbegin()->second;
  137.             Polynomial<T> buff(ans);
  138.             buff.mn[pos] *= T(2);
  139.             buff -= ans;
  140.             q -= buff * sec;
  141.         }
  142.         mn = ans.mn;
  143.         cut_zeroes();
  144.         return *this;
  145.     }
  146.  
  147. };
  148. template <typename T>
  149. Polynomial<T> operator % (const Polynomial<T>& f, const Polynomial<T>& s) {
  150.     Polynomial<T> buff(f);
  151.     buff /= s;
  152.     buff *= s;
  153.     return f - buff;
  154. }
  155. template<typename T>
  156. Polynomial<T> operator / (const Polynomial<T>& f, const Polynomial<T>& s) {
  157.     Polynomial<T> ans(f);
  158.     ans /= s;
  159.     return ans;
  160. }
  161. template <typename T>
  162. Polynomial<T> operator , (const Polynomial<T>& f, const Polynomial<T>& s) {
  163.     if (s == T()) {
  164.         return (f / Polynomial<T>(f[f.Degree()]));
  165.     }
  166.     return (s, f % s);
  167. }
  168.  
  169. template <typename T>
  170. Polynomial<T> operator + (const Polynomial<T>& f, const Polynomial<T>& s) {
  171.     Polynomial<T> ans(f);
  172.     ans += s;
  173.     return ans;
  174. }
  175. template <typename T>
  176. Polynomial<T> operator + (const Polynomial<T>& f, const T& s) {
  177.     Polynomial<T> ans(f);
  178.     ans += s;
  179.     return ans;
  180. }
  181. template <typename T>
  182. Polynomial<T> operator + (const T& f, const Polynomial<T>& s) {
  183.     return s + f;
  184. }
  185. template <typename T>
  186. Polynomial<T> operator - (const Polynomial<T>& f, const Polynomial<T>& s) {
  187.     Polynomial<T> ans(f);
  188.     ans -= s;
  189.     return ans;
  190. }
  191. template <typename T>
  192. Polynomial<T> operator - (const Polynomial<T>& f, const T& s) {
  193.     Polynomial<T> ans(f);
  194.     ans -= s;
  195.     return ans;
  196. }
  197. template <typename T>
  198. Polynomial<T> operator - (const T& f, const Polynomial<T>& s) {
  199.     Polynomial<T> ans(f);
  200.     return ans - s;
  201. }
  202. template <typename T>
  203. Polynomial<T> operator * (const Polynomial<T>& f, const Polynomial<T>& s) {
  204.     auto buff = f;
  205.     buff *= s;
  206.     return buff;
  207. }
  208.  
  209. template <typename T>
  210. Polynomial<T> operator * (const Polynomial<T>& f, const T& s) {
  211.     return f * Polynomial<T>(s);
  212. }
  213.  
  214. template <typename T>
  215. Polynomial<T> operator * (const T& f, const Polynomial<T>& s) {
  216.     return Polynomial<T>(f) * s;
  217. }
  218. template <typename T>
  219. ostream& operator << (ostream& stream, const Polynomial<T>& polynomial) {
  220.     if (polynomial.Degree() == -1) {
  221.         stream << 0;
  222.         return stream;
  223.     }
  224.     if (polynomial.rbegin()->first == 0) {
  225.         stream << polynomial[0];
  226.         return stream;
  227.     }
  228.     for (auto it = polynomial.rbegin(); it != polynomial.rend(); ++it) {
  229.         if (it->first == 0) {
  230.             if (it->second < T()) {
  231.                 stream << it->second;
  232.             } else {
  233.                 stream << '+' << it->second;
  234.             }
  235.             continue;
  236.         }
  237.         if (it->second > T()) {
  238.             if (it != polynomial.rbegin()) {
  239.                 stream << '+';
  240.             }
  241.             if (it->second != T(1)) {
  242.                 stream << it->second << '*';
  243.             }
  244.         } else {
  245.             if (it->second == T(-1)) {
  246.                 stream << '-';
  247.             } else {
  248.                 stream << it->second << '*';
  249.             }
  250.         }
  251.         stream << 'x';
  252.         if (it->first != 1) {
  253.             stream << '^' << it->first;
  254.         }
  255.     }
  256.     return stream;
  257. }
  258. template <typename T>
  259. Polynomial<T> operator & (const Polynomial<T>& f, const Polynomial<T>& s) {
  260.     auto ans = Polynomial<T>();
  261.     for (auto it = f.begin(); it != f.end(); it++) {
  262.         Polynomial<T> buff = Polynomial<T>(it->second);
  263.         for (size_t i = 0; i != it->first; ++i) {
  264.             buff *= s;
  265.         }
  266.         ans += buff;
  267.     }
  268.     return ans;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement