Last active
June 12, 2024 00:39
-
-
Save martin-minarik/ed9e449e3eac0f5fc5917a076b83a52b to your computer and use it in GitHub Desktop.
Polynomial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "polynomial.h" | |
using namespace std; | |
int main() { | |
{ | |
cout << Polynomial {} << endl; | |
cout << (Polynomial) {-3} << endl; | |
cout << (Polynomial) {-2, 3} << endl; | |
cout << (Polynomial) {5, 4, 7} << endl; | |
} | |
cout << endl; | |
{ | |
vector<double> coefficients_p{4, 5, -1, 7, 0, 3}; | |
vector<double> coefficients_q{-3, 1, 0, -1, 0, 0, 4}; | |
Polynomial polynomial_p(coefficients_p); | |
Polynomial polynomial_q(coefficients_q); | |
Polynomial polynomial_r = polynomial_p + polynomial_q; | |
cout << "P(x) = " << polynomial_p << endl; | |
cout << "Degree of P(x) = " << polynomial_p.get_degree() << endl; | |
cout << "P(5) = " << polynomial_p.calculate_result(5) << endl; | |
cout << "Q(x) = " << polynomial_q << endl; | |
cout << "R(x) = " << polynomial_r << endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "polynomial.h" | |
Polynomial::Polynomial() : coefficients({0}) { | |
} | |
Polynomial::Polynomial(double a0) : coefficients({a0}) { | |
} | |
Polynomial::Polynomial(double a0, double a1) : coefficients({a0, a1}) { | |
} | |
Polynomial::Polynomial(double a0, double a1, double a2) : coefficients({a0, a1, a2}) { | |
} | |
Polynomial::Polynomial(vector<double> &coefficients) : coefficients(coefficients) { | |
} | |
int Polynomial::get_degree() { | |
return coefficients.size() - 1; | |
} | |
double Polynomial::calculate_result(double x) { | |
double result = coefficients[coefficients.size() - 1]; | |
for (int i = coefficients.size() - 2; i >= 0; --i) | |
result = result * x + coefficients[i]; | |
return result; | |
} | |
double Polynomial::get(int n) const { | |
return coefficients.at(n); | |
} | |
Polynomial Polynomial::add(Polynomial &other) { | |
vector<double> &bigger_vector = (this->coefficients.size() > other.coefficients.size()) | |
? this->coefficients : other.coefficients; | |
vector<double> &smaller_vector = (&bigger_vector == &this->coefficients) | |
? other.coefficients : this->coefficients; | |
Polynomial result(bigger_vector); | |
for (int i = 0; i < smaller_vector.size(); ++i) | |
result.coefficients[i] += smaller_vector[i]; | |
return result; | |
} | |
double Polynomial::operator[](int n) const { | |
return this->get(n); | |
} | |
Polynomial Polynomial::operator+(Polynomial &other) { | |
return add(other); | |
} | |
ostream &operator<<(ostream &stream, const Polynomial &polynomial) { | |
for (int i = polynomial.coefficients.size() - 1; i >= 0; --i) { | |
double coefficient = polynomial[i]; | |
if (coefficient != 0 || i == 0) { | |
if (coefficient < 0 && i != 0) | |
stream << '(' << coefficient << ')'; | |
else | |
stream << coefficient; | |
if (i > 0) { | |
stream << " * x^" << i << " + "; | |
} | |
} | |
} | |
return stream; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef POLYNOMIAL_H | |
#define POLYNOMIAL_H | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
class Polynomial { | |
private: | |
vector<double> coefficients; | |
public: | |
Polynomial(); | |
explicit Polynomial(double a0); | |
Polynomial(double a0, double a1); | |
Polynomial(double a0, double a1, double a2); | |
Polynomial(vector<double> &coefficients); | |
int get_degree(); | |
double calculate_result(double x); | |
double get(int n) const; | |
Polynomial add(Polynomial &other); | |
double operator[](int n) const; | |
Polynomial operator+(Polynomial &other); | |
friend ostream &operator<<(ostream &stream, const Polynomial &polynomial); | |
}; | |
#endif //POLYNOMIAL_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment