-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTerm.cpp
86 lines (70 loc) · 1.67 KB
/
Term.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "Term.h"
Term::Term() {
exponent = 0;
coefficient = 0;
}
Term::Term(const Term& orig) {
exponent = orig.exponent;
coefficient = orig.coefficient;
}
Term::~Term() {
}
std::string Term::print() const {
std::stringstream ss;
if (coefficient == 0) return ss.str(); //nothing to print
if (coefficient > 0) ss << "+";
if (exponent == 0) { //constant term
ss << coefficient;
return ss.str();
}
if (coefficient == -1) ss << "-"; //otherwise ss << coefficient; takes care of this
else if (coefficient != 1 ) ss << coefficient;
ss << "x";
if (exponent == 1) return ss.str(); //no exponent needed
ss << "^" << exponent;
return ss.str(); //full term
}
//getters&&setters
void Term::set_exponent(const int expo){
exponent = expo;
}
void Term::set_coefficient(const int coeff){
coefficient = coeff;
}
int Term::get_exponent() const {
return exponent;
}
int Term::get_coefficient() const {
return coefficient;
}
const Term& Term::operator=(const Term& rhs)
{
exponent = rhs.exponent;
coefficient = rhs.coefficient;
return *this;
}
//Boolean operators
bool Term::operator== (const Term& rhs) const //needed to be const
{
return exponent == rhs.exponent; //simplified
}
bool Term::operator!= (const Term& rhs) const //ditto
{
return exponent != rhs.exponent;
}
bool Term::operator> (const Term& rhs) const //etc
{
return exponent > rhs.exponent;
}
bool Term::operator>= (const Term& rhs) const
{
return exponent >= rhs.exponent;
}
bool Term::operator< (const Term& rhs) const
{
return exponent < rhs.exponent;
}
bool Term::operator<= (const Term& rhs) const
{
return exponent <= rhs.exponent;
}