-
Notifications
You must be signed in to change notification settings - Fork 0
/
term-friends.cpp
133 lines (94 loc) · 2.4 KB
/
term-friends.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "term.h"
//term operator+(const term &x, const term &y)
//{
// //disabled cuz polynoimials can add
//}
//term operator-(const term &x, const term &y)
//{
// // //disabled cuz polynoimials can add/minus
//}
term operator*(const term &x, const term &y)
{
term temp(x.coeff*y.coeff, x.power + y.power);
return temp;
}
term operator/(const term &x, const term &y)
{
term temp(x.coeff/y.coeff, x.power - y.power);
return temp;
}
bool operator==(const term &x, const term &y)
{
return x.power == y.power && x.coeff == y.coeff;
}
bool operator!=(const term &x, const term &y)
{
return x.power != y.power || x.coeff != y.coeff;
}
bool operator%=(const term &x, const term &y)
{
return x.power == y.power;
}
bool operator>=(const term &x, const term &y)
{
return !(x < y);
}
bool operator<=(const term &x, const term &y)
{
return !(x > y);
}
bool operator>(const term &x, const term &y)
{
if(x.power == y.power)
return x.coeff > y.coeff;
else
return x.power > y.power;
}
bool operator<(const term &x, const term &y)
{
if(x.power == y.power)
return x.coeff < y.coeff;
else
return x.power < y.power;
}
std::ostream& operator<<(std::ostream& out, const term& t) //TESTED
{
if(t.coeff != 1)
out<<t.coeff;
if(t.power != 0)
out<<t.var;
if(t.power != 1 && t.power != 0)
out<<'^'<<t.power;
return out;
}
std::istream& operator>>(std::istream& in, term& t) //TESTED
{
/* the idea here is to look for ^
* and using string functions to find the
* coeff, var, and power.
* stringstream is going to be used very carefully...
*/
//assuming there will only be 3 types of
//inputs: 3/2x^2, x^5, 5
fraction coeff(1), power(0);
// std::string term_line;
// std::getline(in, term_line);
// std::cout<<term_line<<std::endl;
char var = 'x', junk;
// std::cout<<"##condition:"<<(isdigit(in.peek()) || in.peek()=='+' || in.peek()=='-')<<std::endl;
// std::cout<<"##peek:"<<in.peek()<<std::endl;
if(isdigit(in.peek()) || in.peek()=='+' || in.peek()=='-') { //[2]x^2
in>>coeff;
}
if(isalpha(in.peek())) { //[x]^2
in>>var;
power.setValue(1);
}
if(in.peek()=='^') { //x[^]2
in>>junk>>power;
}
if(in.peek()=='\n') //I guess??
in.ignore();
t.setTerm(coeff, power, var);
return in;
}