数据结构——一元多项式的乘法与加法运算

一元多项式的乘法与加法运算

解题关键:将两个多项式相乘的方法

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
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
int c;
int e;
struct Node *next;
}PNode, *Poly;

Poly ReadPoly();
void Attach(int, int, Poly*);
Poly Add(Poly, Poly);
Poly Mult(Poly, Poly);
void PrintPoly(Poly);

int main()
{
Poly P1 = ReadPoly();
Poly P2 = ReadPoly();
Poly PP = Mult(P1, P2);
PrintPoly(PP);
Poly PS = Add(P1, P2);
PrintPoly(PS);

return 0;
}

Poly ReadPoly()
{
Poly P = (Poly)malloc(sizeof(PNode));
P->next = NULL;
Poly Rear = P;
int c, e, n;
scanf("%d", &n);
while(n--)
{
scanf("%d %d", &c, &e);
Attach(c, e, &Rear); // 注意&Rear传地址
}
Poly t = P;
P = P->next;
free(t);
return P;
}

void Attach(int c, int e, Poly *Rear) // Rear为指针的指针
{
Poly s = (Poly)malloc(sizeof(PNode));
s->c = c;
s->e = e;
s->next = NULL;
(*Rear)->next = s;
*Rear = s;
return;
}

Poly Add(Poly P1, Poly P2)
{
Poly P = (Poly)malloc(sizeof(PNode));
P->next = NULL;
Poly Rear = P;
while(P1 && P2)
{
if(P1->e == P2->e)
{
if(P1->c + P2->c) // 系数非0
{
Attach(P1->c + P2->c, P1->e, &Rear);
}
P1 = P1->next;
P2 = P2->next;
}
else if(P1->e > P2->e)
{
Attach(P1->c, P1->e, &Rear);
P1 = P1->next;
}
else
{
Attach(P2->c, P2->e, &Rear);
P2 = P2->next;
}
}
while(P1)
{
Attach(P1->c, P1->e, &Rear);
P1 = P1->next;
}
while(P2)
{
Attach(P2->c, P2->e, &Rear);
P2 = P2->next;
}
Poly t = P;
P = P->next;
free(t); // 释放空头结点
return P;
}

Poly Mult(Poly P1, Poly P2)
{
if(!P1 || !P2) // 至少有一个为空
{
return NULL;
}
Poly t1 = P1, t2 = P2;
Poly P = (Poly)malloc(sizeof(PNode));
P->next =NULL;
Poly Rear = P;
while(t2) // 获取初始结果多项式
{
if(t1->c * t2->c)
{
Attach(t1->c * t2->c, t1->e + t2->e, &Rear);
}
t2 = t2->next;
}
t1 = t1->next;
while(t1) // 逐项插入
{
t2 = P2;
while(t2)
{
int e = t1->e + t2->e;
int c = t1->c * t2->c;
Poly Pre;
for(Pre = P;Pre->next && Pre->next->e > e; Pre = Pre->next); // 获取插入位置前驱
if(Pre->next && Pre->next->e == e)
{
if(c + Pre->next->c) // 系数非0
{
Pre->next->c += c;
}
else // 系数为0删除
{
Poly s = Pre->next;
Pre->next = s->next;
free(s);
}
}
else
{
if(c)
{
Poly s = (Poly)malloc(sizeof(PNode));
s->c = c;
s->e = e;
s->next = Pre->next;
Pre->next = s;
}
}
t2 = t2->next;
}
t1 = t1->next;
}
Poly t = P;
P = P->next;
free(t); // 释放空头结点
return P;
}

void PrintPoly(Poly P)
{
if(!P) // 为空
{
printf("0 0\n");
return;
}
while(P)
{
printf("%d %d", P->c, P->e);
if(P->next)
{
printf(" ");
}
P = P->next;
}
printf("\n");
return;
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2020-2021 zhangguoliu
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信