思路
这题是我做数据结构以来,碰到最难的题目了。以往我写的代码都只有几十行,一百来行顶天了。结果这次有将近两百行。 代码量的增加,首先带来的问题就是如何顺利写完。 因此结构化的设计是首要选择,而且必须要有清晰的程序设计思路: 自顶向下 就是先审题,将程序划分为几个独立的模块(函数),然后再一个模块一个模块的写出来。
这种思维我初学python的时候就使用过了,但在这道题中才真正见到了 自顶向下设计思维的强大之处!!
在修改 BUG的时候,可以根据错误的答案,“罪责”到某个具体模块,再到某个具体语句。
有时候调试都不需要,真的是太强大了!!
我觉得老师讲解这道题最精华部分就在于思路解析的过程,至于代码如何实现都是毛毛雨,建议反复观看老师的具体解题思路。
代码
#include<stdio.h>
#include<stdlib.h>
typedef struct PolyNode
*Polynomial
;
struct PolyNode
{
int coef
;
int expon
;
Polynomial link
;
};
void Attach( int c
, int e
, Polynomial
*pRear
)
{
Polynomial P
;
P
= (Polynomial
)malloc(sizeof(struct PolyNode
));
P
->coef
= c
;
P
->expon
= e
;
P
->link
= NULL;
(*pRear
)->link
= P
;
*pRear
= P
;
}
Polynomial
ReadPoly()
{
int N
, c
, e
;
Polynomial P
, Rear
, t
;
scanf("%d", &N
);
P
= (Polynomial
)malloc(sizeof(struct PolyNode
));
P
->link
= NULL;
Rear
= P
;
while( N
-- ){
scanf("%d %d", &c
, &e
);
Attach( c
, e
, &Rear
);
}
t
= P
; P
= P
->link
; free(t
);
return P
;
}
Polynomial
Add( Polynomial P1
, Polynomial P2
)
{
Polynomial temp1
, temp2
, Rear
, P
, temp
;
temp1
= P1
; temp2
= P2
;
P
= (Polynomial
)malloc(sizeof(struct PolyNode
));
P
->link
= NULL;
Rear
= P
;
while( temp1
&& temp2
)
{
if( temp1
->expon
== temp2
->expon
){
if( temp1
->coef
+ temp2
->coef
== 0 ){
temp1
= temp1
->link
;
temp2
= temp2
->link
;
}
else{
Attach( temp1
->coef
+temp2
->coef
, temp1
->expon
, &Rear
);
temp1
= temp1
->link
;
temp2
= temp2
->link
;
}
}
else if( temp1
->expon
< temp2
->expon
){
Attach( temp2
->coef
, temp2
->expon
, &Rear
);
temp2
= temp2
->link
;
}
else{
Attach( temp1
->coef
, temp1
->expon
, &Rear
);
temp1
= temp1
->link
;
}
}
while(temp1
){
Attach( temp1
->coef
, temp1
->expon
, &Rear
);
temp1
= temp1
->link
;
}
while(temp2
){
Attach( temp2
->coef
, temp2
->expon
, &Rear
);
temp2
= temp2
->link
;
}
temp
= P
; P
= P
->link
; free(temp
);
return P
;
}
Polynomial
Mult( Polynomial P1
, Polynomial P2
){
Polynomial temp1
, temp2
, Rear
, P
, t
;
int e
, c
;
if( !P1
|| !P2
) return NULL;
temp1
= P1
; temp2
= P2
;
P
= (Polynomial
)malloc(sizeof(struct PolyNode
));
P
->link
= NULL;
Rear
= P
;
while(temp2
){
Attach( temp1
->coef
*temp2
->coef
, temp1
->expon
+temp2
->expon
, &Rear
);
temp2
= temp2
->link
;
}
temp1
= temp1
->link
;
while(temp1
){
temp2
= P2
;
Rear
= P
;
while(temp2
){
e
= temp1
->expon
+ temp2
->expon
;
c
= temp1
->coef
* temp2
->coef
;
while( Rear
->link
&& Rear
->link
->expon
> e
){
Rear
= Rear
->link
;
}
if( Rear
->link
&& Rear
->link
->expon
== e
){
if(Rear
->link
->coef
+ c
){
Rear
->link
->coef
+= c
;
}
else{
t
= Rear
->link
;
Rear
->link
= t
->link
;
free(t
);
}
}
else{
t
= (Polynomial
)malloc(sizeof(struct PolyNode
));
t
->coef
= c
; t
->expon
= e
;
t
->link
= Rear
->link
;
Rear
->link
= t
;
Rear
= Rear
->link
;
}
temp2
= temp2
->link
;
}
temp1
= temp1
->link
;
}
temp2
= P
; P
= P
->link
; free(temp2
);
return P
;
}
void PrintPoly( Polynomial P
){
int flag
= 0;
if(!P
){
printf("0 0\n");
return;
}
while(P
){
if(!flag
){
flag
= 1;
}
else{
printf(" ");
}
printf("%d %d", P
->coef
, P
->expon
);
P
= P
->link
;
}
printf("\n");
}
int main()
{
Polynomial P1
, P2
, PP
, PS
;
P1
= ReadPoly();
P2
= ReadPoly();
PP
= Mult( P1
, P2
);
PrintPoly( PP
);
PS
= Add( P1
, P2
);
PrintPoly( PS
);
return 0;
}
代码我是基本参考老师写出来的,参考性较强。
最后的忠告
写程序提升最大的时候,不是敲代码那段时间。 而是 构思程序 和 修改BUG 这两段时间!