-
Notifications
You must be signed in to change notification settings - Fork 3
/
Polynomial.h
41 lines (33 loc) · 1.3 KB
/
Polynomial.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <string>
#include <map>
#include <utility> // for std::pair
class Polynomial {
friend std::ostream& operator<<(std::ostream&, const Polynomial&);
public:
//constructors
Polynomial() {};
explicit Polynomial(const std::map<int, double>& n) : polyTerms{n} {};
explicit Polynomial(std::string);
//delegating constructor
explicit Polynomial(const char* s) : Polynomial{std::string{s}} {};
//arithmetic operators
Polynomial operator+(const Polynomial&) const;
Polynomial operator-(const Polynomial&) const;
Polynomial operator*(const Polynomial&) const;
std::pair<Polynomial, Polynomial> operator/(const Polynomial&) const;
static Polynomial integral(const Polynomial&);
static Polynomial derivative(const Polynomial&);
private:
std::map<int, double> polyTerms;
//utility functions
void preprocessing(std::string&) const;
void tokenize(const std::string&);
bool isEmpty() const {return polyTerms.empty();}
std::pair<int, double> maxTerm() const {return *polyTerms.crbegin();}
static int degree(const Polynomial&);
static Polynomial lead(const Polynomial& p) {return Polynomial{std::map<int, double> {p.maxTerm()}};}
static Polynomial divLeads(const Polynomial&, const Polynomial&);
};
#endif /* POLYNOMIAL_H */