Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- template<typename T>
- class Polynomial {
- private:
- vector<T> coef;
- public:
- Polynomial(vector<T>& v){
- int index = v.size() - 1;
- while (v[index] == T()) {
- --index;
- }
- coef.resize(index + 1);
- for (int i = 0; i <= index; ++i) {
- coef[i] = v[i];
- }
- }
- Polynomial(T x = T()) {
- if (x != T()) {
- coef.resize(1, x);
- }
- }
- template<typename It>
- Polynomial(It begin, It end) {
- while (begin != end) {
- coef.push_back((*begin));
- ++begin;
- }
- }
- int Degree() const {
- return static_cast<int>(coef.size()) - 1;
- }
- T operator [](int i) const {
- if (i > (*this).Degree()) {
- return T();
- }
- return coef[i];
- }
- void normal() {
- int index = (*this).Degree();
- if (index == -1) {
- return;
- }
- while (coef[index] == T()) {
- --index;
- }
- vector<T> temp(index + 1);
- for (int i = 0; i <= index; ++i) {
- temp[i] = coef[i];
- }
- (*this) = Polynomial<T>(temp);
- }
- bool operator ==(const Polynomial<T>& other) const {
- Polynomial<T> temp1 = (*this);
- Polynomial<T> temp2 = other;
- temp1.normal();
- temp2.normal();
- if (temp1.Degree() != temp2.Degree()) {
- return false;
- }
- for (int i = 0; i < temp2.Degree(); ++i) {
- if (temp1[i] != temp2[i]) {
- return false;
- }
- }
- return true;
- }
- bool operator !=(const Polynomial<T>& other) const {
- return !((*this) == other);
- }
- typename vector<T>::const_iterator begin() const {
- return coef.begin();
- }
- typename vector<T>::const_iterator end() const {
- return coef.end();
- }
- Polynomial<T> operator +(const Polynomial<T>& other) const {
- vector<T> result(max((*this).Degree(), other.Degree()) + 1);
- for (int i = 0; i < min((*this).Degree(), other.Degree()) + 1; ++i) {
- result[i] = coef[i] + other[i];
- }
- if ((*this).Degree() > other.Degree()) {
- for (int i = other.Degree() + 1; i < (*this).Degree() + 1; ++i) {
- result[i] = coef[i];
- }
- } else {
- for (int i = (*this).Degree() + 1; i < other.Degree() + 1; ++i) {
- result[i] = other[i];
- }
- }
- auto ret = Polynomial(result);
- ret.normal();
- return ret;
- }
- Polynomial<T> operator -(const Polynomial<T>& other) const {
- vector<T> result(max((*this).Degree(), other.Degree()) + 1);
- for (int i = 0; i < min((*this).Degree(), other.Degree()) + 1; ++i) {
- result[i] = coef[i] - other[i];
- }
- if ((*this).Degree() > other.Degree()) {
- for (int i = other.Degree() + 1; i < (*this).Degree() + 1; ++i) {
- result[i] = coef[i];
- }
- } else {
- for (int i = (*this).Degree() + 1; i < other.Degree() + 1; ++i) {
- result[i] = T() - other[i];
- }
- }
- auto ret = Polynomial(result);
- ret.normal();
- return ret;
- }
- Polynomial<T>& operator +=(const Polynomial<T>& other) {
- (*this) = (*this) + other;
- return (*this);
- }
- Polynomial<T>& operator -=(const Polynomial<T>& other) {
- (*this) = (*this) - other;
- return (*this);
- }
- };
- template<typename T>
- Polynomial<T> operator +(const T& self, const Polynomial<T>& other) {
- return Polynomial<T>(self) + other;
- }
- template<typename T>
- Polynomial<T> operator -(const T& self, const Polynomial<T>& other) {
- return Polynomial<T>(self) - other;
- }
- template<typename T>
- bool operator ==(const T& self, const Polynomial<T>& other) {
- return Polynomial<T>(self) == other;
- }
- template<typename T>
- bool operator !=(const T& self, const Polynomial<T>& other) {
- return Polynomial<T>(self) != other;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement