forked from zhuxinquan/Data-Structure-And-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
一元多项式运算器.c
220 lines (211 loc) · 5.28 KB
/
一元多项式运算器.c
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*************************************************************************
> File Name: 一元多项式运算器.c
> Author: zhuxinquan
> Mail: [email protected]
> Created Time: 2015年09月20日 星期日 22时47分56秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
typedef struct Polynomial{
float coef; //系数
int expn; //指数
struct Polynomial * next;
}polyn;
polyn * Creatpolyn() //创建多项式
{
polyn * head, * tail, *s;
float c;
int e; //c == coefficient, e == exponent
tail = head = (polyn *)malloc(sizeof(polyn));
printf("input c, e (End: c = 0, Separated by space):\n");
scanf("%f,%d", &c, &e);
while(c != 0)
{
s = (polyn *)malloc(sizeof(polyn));
s->coef = c;
s->expn = e;
tail->next = s;
tail = s;
scanf("%f,%d", &c, &e);
}
tail->next = NULL;
return head;
}
void print_polyn(polyn * p) //输出多项式
{
polyn * q = p->next;
int flag = 1;
if(!q)
{
putchar('0');
printf("\n");
return;
}
while(q)
{
if(q->coef > 0 && flag != 1)
{
putchar('+');
}
if(q->coef != 1 && q->coef != -1)
{
printf("%g", q->coef);
if(q->expn == 1)
{
putchar('X');
}
else if(q->expn)
{
printf("X^%d", q->expn);
}
}
else
{
if(q->coef == 1)
{
if(!q->expn) putchar('1');
else if(q->expn == 1) putchar('X');
else printf("X^%d", q->expn);
}
if(q->coef == -1)
{
if(!q->expn) printf("-1");
else if(q->expn == 1) printf("X");
else printf("X^%d", q->expn);
}
}
q = q->next;
flag++;
}
printf("\n");
}
polyn * add_polyn(polyn * pa, polyn * pb) //两个多项式相加
{
polyn * qa = pa->next;
polyn * qb = pb->next;
polyn * headc, *pc, *qc;
pc = (polyn *)malloc(sizeof(polyn));
pc->next = NULL; //pc用作headc的尾指针
headc = pc;
while(qa != NULL && qb != NULL)
{
qc = (polyn *)malloc(sizeof(polyn));
if(qa->expn < qb->expn)
{
qc->coef = qa->coef;
qc->expn = qa->expn;
qa = qa->next;
}
else if(qa->expn == qb->expn)
{
qc->coef = qa->coef + qb->coef;
qc->expn = qa->expn;
qa = qa->next;
qb = qb->next;
}
else
{
qc->coef = qb->coef;
qc->expn = qb->expn;
qb = qb->next;
}
if(qc->coef != 0)
{
qc->next = NULL; //qc->next = pc->next
pc->next = qc;
pc = qc;
}
else free(qc);
}
while(qa != NULL) //qa还有剩余项
{
qc = (polyn *)malloc(sizeof(polyn));
qc->coef = qa->coef;
qc->expn = qa->expn;
qa = qa->next;
qc->next = NULL;
pc->next = qc;
pc = qc;
}
while(qb != NULL) //qb还有剩余项
{
qc = (polyn *)malloc(sizeof(polyn));
qc->coef = qb->coef;
qc->expn = qb->expn;
qb = qb->next;
qc->next = NULL;
pc->next = qc;
pc = qc;
}
return headc;
}
polyn * subtraction_polyn(polyn * pa, polyn * pb) //多项式相减
{
polyn * head = pb;
polyn * p = pb->next;
polyn * pd; //pd保存相减得到的新的结果
while(p)
{
p->coef *= -1; //转换为和负数相加
p = p->next;
}
pd = add_polyn(pa, head);
for(p = head->next; p; p = p->next) //还原
{
p->coef *= -1;
}
return pd;
}
void sort_link(polyn * head)
{
int e, te;
int coef;
polyn * p, * q;
if(head->next == NULL || head->next->next == NULL)
{
return ;
}
for(p = head->next; ; p = p->next)
{
e = p->expn;
for(q = p->next; ; q = q->next)
{
if(q->expn < e)
{
e = q->expn;
te = p->expn;
coef = p->coef;
p->expn = q->expn;
p->coef = q->coef;
q->coef = coef;
q->expn = te;
}
if(!q->next){
break;
}
}
if(p->next->next == NULL){
break;
}
}
}
int main(void)
{
polyn * pa, *pb, *padd, *psub;
printf("please input polynomial pa(like 1,1 and 0,0 is over ):\n");
pa = Creatpolyn();
printf("please input polynomial pb(like 1,1 and 0,0 is over ):\n");
pb = Creatpolyn();
sort_link(pa);
sort_link(pb);
printf("pa=");
print_polyn(pa);
printf("pb=");
print_polyn(pb);
padd = add_polyn(pa, pb);
psub = subtraction_polyn(pa, pb);
printf("pa+pb=");
print_polyn(padd);
printf("pa-pb=");
print_polyn(psub);
}