-
Notifications
You must be signed in to change notification settings - Fork 14
/
02-线性结构2 一元多项式的乘法与加法运算
196 lines (172 loc) · 2.74 KB
/
02-线性结构2 一元多项式的乘法与加法运算
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
typedef int element;
typedef struct Node
{
element coef;
element exp;
struct Node * Next;
}node;
using namespace std;
node * Read();
node * Add(node *L1, node *L2);
node * Mult(node *L1, node *L2);
void Print(node *L);
node * Insert(node *p, element c, element e);
int main()
{
node *L1, *L2, *L3, *L4;
L1 = Read();
L2 = Read();
L4 = Mult(L1, L2);
L3 = Add(L1, L2);
Print(L4);
Print(L3);
return 0;
}
node * Read()
{
node *p;
node * phead = new node;
phead->Next = NULL;
p = phead;
int n, i = 0;
cin >> n;
while (i < n)
{
node *pNew = new node;
cin >> pNew->coef >> pNew->exp;
pNew->Next = NULL;
p->Next = pNew;
p = pNew;
i++;
}
return phead;
}
node * Add(node *L1, node *L2)
{
node *phead = new node;
node *p1 = L1->Next;
node *p2 = L2->Next;
node *p3 = phead;
phead->Next = NULL;
while (p1 && p2)
{
node *t1, *t2;
if (p1->exp == p2->exp)
{
t1 = p1;
t2 = p2;
t1->coef = p1->coef + p2->coef; // list L1 as the new list L3's template;
if (t1->coef != 0)
{
p3->Next = t1;
p3 = t1;
p1 = p1->Next;
p2 = p2->Next;
p3->Next = NULL;
delete t2;
}
else
{
p1 = p1->Next;
p2 = p2->Next;
delete t1;
delete t2;
}
}
else if (p1->exp > p2->exp)
{
p3->Next = p1;
p3 = p1;
p1 = p1->Next;
p3->Next = NULL;
}
else
{
p3->Next = p2;
p3 = p2;
p2 = p2->Next;
p3->Next = NULL;
}
}
if (p1)
p3->Next = p1;
if (p2)
p3->Next = p2;
L1->Next = NULL;
L2->Next = NULL;
return phead;
}
void Print(node *L)
{
node *p = L->Next;
if (!p)
cout << "0 0" << endl;
while (p)
{
if (p->Next != NULL)
cout << p->coef << ' ' << p->exp << ' ';
else
cout << p->coef << ' ' << p->exp << endl;
p = p->Next;
}
return;
}
node * Mult(node *L1, node *L2) //key: insert list p3 by every node;
{
element c, e;
node *phead = new node;
phead->Next = NULL;
node *p1 = L1->Next;
node *p2 = L2->Next;
node *p3 = phead;
if (!p1 || !p2)
return phead; //not 'NULL';
while (p2)
{
node *pNew = new node;
p3 = Insert(p3, p1->coef * p2->coef, p1->exp + p2->exp);
p2 = p2->Next;
}
p1 = p1->Next;
node *t;
while (p1)
{
p2 = L2->Next;
p3 = phead->Next;
while (p2)
{
c = p1->coef * p2->coef;
e = p1->exp + p2->exp;
while (p3->Next && p3->Next->exp > e)
p3 = p3->Next;
if (p3->Next && p3->Next->exp == e)
{
if (p3->Next->coef + c != 0)
p3->Next->coef += c;
else
{
t = p3->Next;
p3->Next = t->Next;
delete t;
}
}
else
{
p3 = Insert(p3, c, e);
}
p2 = p2->Next;
}
p1 = p1->Next;
}
return phead;
}
node * Insert(node *p, element c, element e)
{
node *t = new node;
t->coef = c;
t->exp = e;
t->Next = p->Next;
p->Next = t;
return t;
}