-
Notifications
You must be signed in to change notification settings - Fork 0
/
Term.cpp
68 lines (67 loc) · 1.13 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
#include "Term.hpp"
Term::Term(double coefficient, int power)
{
this->coefficient = coefficient;
this->power = power;
}
double Term::get_coefficient() const
{
return coefficient;
}
int Term::get_power() const
{
return power;
}
std::ostream& operator<< (std::ostream &out, const Term &t)
{
if (t.coefficient == 0) //Don't print zeroes
{
return out;
}
else if (t.coefficient == 1) //1x^3 is redundant, but if the power is zero you need it
{
if (t.power == 0)
{
out << t.coefficient;
}
else if (t.power == 1)
{
out << "x";
}
else
{
out << "x^" << t.power;
}
}
else if (t.coefficient == -1) //1 and -1 have different outputs, so I split them up
{
if (t.power == 0)
{
out << t.coefficient;
}
else if (t.power == 1)
{
out << "-x";
}
else
{
out << "-x^" << t.power;
}
}
else //Anything else
{
if (t.power == 0)
{
out << t.coefficient;
}
else if (t.power == 1)
{
out << t.coefficient << "x";
}
else
{
out << t.coefficient << "x^" << t.power;
}
}
return out;
}