# Assignment 1 - C++

Apr 12th, 2021 (edited)
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. // -------- Polynomial.h ----------
2.
3.
4. #include <iostream>
5. using namespace std;
6.
7. #pragma once
8. class Polynomial {
9. public:
10.     Polynomial(int max_degree = 0);
11.     Polynomial(double *a, int max_degree);
12.     ~Polynomial(); // delete dynamic allocation
13.
14.     int getDegree(bool flag = true);
15.     void setCoeff(int degree, double coefficient) { this->values[degree] = coefficient; set_final_degree(); }
16.
17.     static int getMaxDegree() { return biggest_degree_ever; }
18.
19.     void print_poly() const;
20.
21.
22. private:
23.     const int _max_degree; // 4
24.     int _final_degree; // to avoid printing empty last values
25.     double *values; // [1,2,3,4,5] = 1 + 2x + 3x^2 + 4x^3 + 5x^4
26.     int is_empty() const;
27.     void set_final_degree();
28.
29.     static int biggest_degree_ever;
30. };
31.
32. ostream& operator<<(ostream& out, const Polynomial& p1);
33.
34.
35. // -------- Polynomial.cpp ----------
36.
37. #include "Polynomial.h"
38.
39. int Polynomial::biggest_degree_ever = 0;
40.
41. Polynomial::Polynomial(double* a, int max_degree) : _max_degree(max_degree)
42. {
43.     // [1,2,3,4,5] = 1 + 2x + 3x^2 + 4x^3 + 5x^4
44.     int size = max_degree + 1;
45.
46.     this->values = new double[size](); // () is the same as calloc()
47.     // c = [1,2,3,4,5]
48.     // d = [0,0,0,0,0] -> [1,2,3,4,5]
49.
50.     for (int i = 0; i < size; i++)
51.     {
52.         if (a[i] != 0 && i > biggest_degree_ever)
53.             biggest_degree_ever = i;
54.
55.         values[i] = a[i];
56.     }
57.
58.     set_final_degree();
59.
60.     /*if (max_degree > biggest_degree_ever)
61.         biggest_degree_ever = max_degree;*/
62. }
63.
64. Polynomial::Polynomial(int max_degree) : _max_degree(max_degree)
65. {
66.     int size = max_degree + 1;
67.     this->values = new double[size](); // () is the same as calloc()
68.
69.     set_final_degree();
70. }
71.
72.
73. Polynomial::~Polynomial()
74. {
75.     delete[] values;
76. }
77.
78.
79. void Polynomial::print_poly() const
80. {
81.     // add print for empty array: prints only 0.
82.
83.     if (this->is_empty())
84.     {
85.         cout << "0" << endl;
86.     }
87.
88.     else
89.     {
90.         cout << this->values[0] << " + ";
91.
92.         for (int i = 1; i < this->_final_degree; i++)
93.         {
94.                 cout << this->values[i] << "x^" << i << " + ";
95.         }
96.
97.         cout << this->values[_final_degree] << "x^" << _final_degree << endl; // styling
98.     }
99. }
100.
101. int Polynomial::getDegree(bool flag)
102. {
103.     if (flag == false) return this->_max_degree;
104.
105.     else
106.     {
107.         int position = 0;
108.
109.         for (int i = 0; i < this->_max_degree + 1; i++)
110.         {
111.             if (this->values[i])
112.                 position = i;
113.         }
114.
115.         return position;
116.     }
117. }
118.
119.
120. int Polynomial::is_empty() const
121. {
122.     for (int i = 0; i < this->_max_degree + 1; i++)
123.     {
124.         if (this->values[i] != 0)
125.             return 0;
126.     }
127.
128.     return 1;
129. }
130.
131.
132. void Polynomial::set_final_degree()
133. {
134.     int temp = 0;
135.
136.     for (int i = 0; i < this->_max_degree + 1; i++)
137.     {
138.         if (this->values[i])
139.             temp = i;
140.     }
141.
142.     this->_final_degree = temp;
143. }
144.
145.
146.
147. ostream& operator<<(ostream& out, const Polynomial& p1)
148. {
149.     p1.print_poly();
150.
151.     return out;
152. }
153.
154.
155. // -------- Source.cpp ----------
156.
157.
158. #include <iostream>
159. using namespace std;
160.
161. #include "Polynomial.h"
162.
163.
164. void testPolynomial()
165. {
166.     cout << "----- start testPolynomial ---------" << endl;
167.     cout << "test0 :" << Polynomial::getMaxDegree() << endl;
168.     Polynomial p1;
169.     cout << "test1: " << p1;
170.     Polynomial p2(6);
171.     p2.setCoeff(2, 0.4);
172.     cout << "test2: " << p2 << "degree=" << p2.getDegree(false) << endl;
173.     double c[5] = { 0.1, 0.2, 0 ,11.5, 1.3 };
174.     Polynomial p3(c, 4);
175.     cout << "test3: " << p3;
176.     p2.setCoeff(2, 0.0);
177.     cout << "test4: " << p2;
178.     cout << "test6 :maxDegree = " << Polynomial::getMaxDegree() << endl;
179.     cout << "______________" << endl;
180. }
181.
182. void testRational() {
183.     cout << "----- start testRational ---------" << endl;
184.     cout << "test7" << endl;
185.     Rational r1;
186.     r1.print();
187.     cout << r1;
188.     double c[] = { 0,2,2,3,4,5 };
189.     Polynomial p1(c, 5);
190.     c[0] = 1.2;
191.     Polynomial p2(c, 3);
192.     Rational r2(p1, p2);
193.     cout << "test8" << endl;
194.     r2.print();
195.     cout << endl << r2 << endl;
196.     cout << "test9" << endl;
197.     cout << "nom = " << r2.getNom();
198.     cout << "denom = " << r2.getDenom();
199.
200.     cout << "test10 :maxDegree = " << Polynomial::getMaxDegree() << endl;
201.     cout << "__________________________" << endl;
202. }
203.
204. int main()
205. {
206.     testPolynomial();
207.     testRational();
208. }
RAW Paste Data