Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -------- Polynomial.h ----------
- #include <iostream>
- using namespace std;
- #pragma once
- class Polynomial {
- public:
- Polynomial(int max_degree = 0);
- Polynomial(double *a, int max_degree);
- ~Polynomial(); // delete dynamic allocation
- int getDegree(bool flag = true);
- void setCoeff(int degree, double coefficient) { this->values[degree] = coefficient; set_final_degree(); }
- static int getMaxDegree() { return biggest_degree_ever; }
- void print_poly() const;
- private:
- const int _max_degree; // 4
- int _final_degree; // to avoid printing empty last values
- double *values; // [1,2,3,4,5] = 1 + 2x + 3x^2 + 4x^3 + 5x^4
- int is_empty() const;
- void set_final_degree();
- static int biggest_degree_ever;
- };
- ostream& operator<<(ostream& out, const Polynomial& p1);
- // -------- Polynomial.cpp ----------
- #include "Polynomial.h"
- int Polynomial::biggest_degree_ever = 0;
- Polynomial::Polynomial(double* a, int max_degree) : _max_degree(max_degree)
- {
- // [1,2,3,4,5] = 1 + 2x + 3x^2 + 4x^3 + 5x^4
- int size = max_degree + 1;
- this->values = new double[size](); // () is the same as calloc()
- // c = [1,2,3,4,5]
- // d = [0,0,0,0,0] -> [1,2,3,4,5]
- for (int i = 0; i < size; i++)
- {
- if (a[i] != 0 && i > biggest_degree_ever)
- biggest_degree_ever = i;
- values[i] = a[i];
- }
- set_final_degree();
- /*if (max_degree > biggest_degree_ever)
- biggest_degree_ever = max_degree;*/
- }
- Polynomial::Polynomial(int max_degree) : _max_degree(max_degree)
- {
- int size = max_degree + 1;
- this->values = new double[size](); // () is the same as calloc()
- set_final_degree();
- }
- Polynomial::~Polynomial()
- {
- delete[] values;
- }
- void Polynomial::print_poly() const
- {
- // add print for empty array: prints only 0.
- if (this->is_empty())
- {
- cout << "0" << endl;
- }
- else
- {
- cout << this->values[0] << " + ";
- for (int i = 1; i < this->_final_degree; i++)
- {
- cout << this->values[i] << "x^" << i << " + ";
- }
- cout << this->values[_final_degree] << "x^" << _final_degree << endl; // styling
- }
- }
- int Polynomial::getDegree(bool flag)
- {
- if (flag == false) return this->_max_degree;
- else
- {
- int position = 0;
- for (int i = 0; i < this->_max_degree + 1; i++)
- {
- if (this->values[i])
- position = i;
- }
- return position;
- }
- }
- int Polynomial::is_empty() const
- {
- for (int i = 0; i < this->_max_degree + 1; i++)
- {
- if (this->values[i] != 0)
- return 0;
- }
- return 1;
- }
- void Polynomial::set_final_degree()
- {
- int temp = 0;
- for (int i = 0; i < this->_max_degree + 1; i++)
- {
- if (this->values[i])
- temp = i;
- }
- this->_final_degree = temp;
- }
- ostream& operator<<(ostream& out, const Polynomial& p1)
- {
- p1.print_poly();
- return out;
- }
- // -------- Source.cpp ----------
- #include <iostream>
- using namespace std;
- #include "Polynomial.h"
- void testPolynomial()
- {
- cout << "----- start testPolynomial ---------" << endl;
- cout << "test0 :" << Polynomial::getMaxDegree() << endl;
- Polynomial p1;
- cout << "test1: " << p1;
- Polynomial p2(6);
- p2.setCoeff(2, 0.4);
- cout << "test2: " << p2 << "degree=" << p2.getDegree(false) << endl;
- double c[5] = { 0.1, 0.2, 0 ,11.5, 1.3 };
- Polynomial p3(c, 4);
- cout << "test3: " << p3;
- p2.setCoeff(2, 0.0);
- cout << "test4: " << p2;
- cout << "test6 :maxDegree = " << Polynomial::getMaxDegree() << endl;
- cout << "______________" << endl;
- }
- void testRational() {
- cout << "----- start testRational ---------" << endl;
- cout << "test7" << endl;
- Rational r1;
- r1.print();
- cout << r1;
- double c[] = { 0,2,2,3,4,5 };
- Polynomial p1(c, 5);
- c[0] = 1.2;
- Polynomial p2(c, 3);
- Rational r2(p1, p2);
- cout << "test8" << endl;
- r2.print();
- cout << endl << r2 << endl;
- cout << "test9" << endl;
- cout << "nom = " << r2.getNom();
- cout << "denom = " << r2.getDenom();
- cout << "test10 :maxDegree = " << Polynomial::getMaxDegree() << endl;
- cout << "__________________________" << endl;
- }
- int main()
- {
- testPolynomial();
- testRational();
- }
Add Comment
Please, Sign In to add comment