-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial_addition.cpp
165 lines (155 loc) · 4.4 KB
/
polynomial_addition.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
using namespace std;
template <typename T>
struct Node {
T value;
Node<T>* next;
Node() : value(0), next(nullptr) {}
Node(T value, Node* next = nullptr) : value(value), next(next) {}
};
template <typename T>
class Queue {
protected:
Node<T>*front, *rear;
int count;
public:
Queue() : front(nullptr), rear(nullptr), count(0) {}
Queue(const Queue& that) : front(nullptr), rear(nullptr), count(0) { *this = that; }
~Queue() { clear(); }
T getfront() const { return front->value; }
T getrear() const { return rear->value; }
int size() const { return count; }
bool empty() const { return size() == 0; }
void append(const T& item) {
Node<T>* new_node = new Node<T>(item);
if (empty()) {
front = rear = new_node;
} else {
rear->next = new_node;
rear = new_node;
}
++count;
}
void serve() {
if (!empty()) {
Node<T>* old_front = front;
front = front->next;
if (front == nullptr) {
rear = nullptr;
}
delete[] old_front;
--count;
}
}
void clear() {
while (!empty()) {
serve();
}
}
Queue& operator=(const Queue& rhs) {
Node<T>*new_front, *new_rear;
if (rhs.empty()) {
new_front = new_rear = nullptr;
} else {
Node<T>* new_node = new Node<T>(rhs.front->value);
new_front = new_node;
Node<T>* currentPtr = rhs.front;
while (currentPtr->next) {
currentPtr = currentPtr->next;
new_node->next = new Node<T>(currentPtr->value);
new_node = new_node->next;
}
new_rear = currentPtr;
clear();
front = new_front;
rear = new_rear;
count = rhs.size();
}
return *this;
}
};
struct Term {
double coefficient;
int index;
Term(double coefficient = 0, int index = 0) : coefficient(coefficient), index(index) {}
};
class Polynomial : private Queue<Term> {
public:
Polynomial() : Queue<Term>() {}
int degree() const {
if (empty()) {
return -1;
}
return getfront().index;
}
int terms() const { return size(); }
friend ostream& operator<<(ostream& os, const Polynomial& data);
void print() const {
Node<Term>* print_node = front;
while (print_node) {
cout << (print_node->value).coefficient << " " << (print_node->value).index << endl;
print_node = print_node->next;
}
}
void read() { // ????????????
clear();
int num_of_terms;
cin >> num_of_terms;
for (int i = 0; i < num_of_terms; i++) {
double co;
int exp;
cin >> co >> exp;
Term currentTerm(co, exp);
append(currentTerm);
}
}
Polynomial operator+(Polynomial rhs) const {
Polynomial result, lhs = *this;
while (!lhs.empty() || !rhs.empty()) {
if (lhs.degree() > rhs.degree()) {
result.append(lhs.getfront());
lhs.serve();
} else if (lhs.degree() < rhs.degree()) {
result.append(rhs.getfront());
rhs.serve();
} else {
Term left_term = lhs.getfront(), right_term = rhs.getfront();
if (left_term.coefficient + right_term.coefficient != 0) {
Term result_term(left_term.coefficient + right_term.coefficient, lhs.degree());
result.append(result_term);
}
lhs.serve();
rhs.serve();
}
}
return result;
}
};
ostream& operator<<(ostream& os, const Polynomial& data) {
Node<Term>* print_node = data.front;
if (print_node == nullptr) {
os << "0";
}
while (print_node) {
os << (print_node->value).coefficient;
os << "x^";
os << (print_node->value).index;
os << " ";
print_node = print_node->next;
}
return os;
}
int main() {
int t;
cin >> t;
while (t--) {
Polynomial p, q;
p.read();
q.read();
Polynomial result = p + q;
cout << result.terms() << endl;
result.print();
}
system("pause");
return 0;
}