-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0414polynomialLinked.cpp
More file actions
58 lines (58 loc) · 1.58 KB
/
Copy path0414polynomialLinked.cpp
File metadata and controls
58 lines (58 loc) · 1.58 KB
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
#include <iostream>
using namespace std;
template <class T> class Polynomial;
template <class T>
class PolyTerm{ //all member of term are public by default
friend class Polynomial<T>;
private:
int coef;
int exp;
PolyTerm<T> *link;
public:
PolyTerm(int c, int e, PolyTerm<T> *l=NULL):coef(c), exp(e), link(l) {}
};
template <class T>
class Polynomial {
private:
PolyTerm<T> * first;
PolyTerm<T> * last;
public:
Polynomial(PolyTerm<T> *a=NULL, PolyTerm<T> * b=NULL): first(a), last(b) {}
Polynomial operator+(const Polynomial & b) const;
void InsertPolyTerm(int coefValue, int expValue);
void InsertPolyTerm(int coefValue, int expValue);
void DelPolyTerm(int delExp);
void InsertBack(int coefValue, int expValue);
void Print();
int Length();
};
template <class T>
Polynomial Polynomial<T>::operator+(const Polynomial& b) const {
//다항식 *this(a)와 b를 더해서 그 합을 반환한다
Term temp;
Chain<Term>::ChainIterator ai = poly.begin(), bi = b.poly.begin();
Polynomial c;
while(ai && bi) { //현재 노드들은 NULL이 아님
if(ai->exp == b->exp) {
int sum = ai->coef+bi->coef;
if(sum) c.poly.InsertBack(temp.Set(sum, ai->exp));
a++; b++; //다음 항으로 이동
}
else if(ai->exp<b->exp) {
c.poly.InsertBack(temp.Set(bi->coef, bi->exp));
b++; //b의 다음 항
}
else {
c.poly.InsertBack(temp.Set(ai->coef, bi->exp));
a++; //a의 다음 항
}
}
while(ai) { //a의 나머지를 복사
c.poly.InsertBack(temp.Set(ai->coef, ai->exp));
a++;
}
while(bi) { //b의 나머지를 복사
c.poly.InsertBack(temp.Set(bi->coef, bi->exp));
b++;
}
}