勉勉强强总算是把乘法的功能给实现了,不够有一个巨大的收获就是理解了严蔚敏书中的一些深层的意义,比我的屌丝方法要好了无数倍,不够既然已经完成了明天还是改成对串的使用来让自己放松一下,今后有机会还会按照严蔚敏的思想来将串的计算完善起来。
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct Term
{
int coef; // 系数
int expn; // 指数
struct Term *next;
} *PolynList;
// 创建一段多项式
Status CreatePolyn(PolynList *P, int n);
// 根据需要输入多项式
Status PressPolyn(PolynList *P);
// 销毁多项式
Status DestroyPolyn(PolynList *P);
// 显示多项式
Status PrintPolyn(PolynList P);
// 获取链表长度
int PolynLength(PolynList P);
// 多项式相加
PolynList AddPolyn(PolynList PA, PolynList PB);
// 多项式相剪
PolynList SubPolyn(PolynList PA, PolynList PB);
// 多项式相乘
PolynList MutPolyn(PolynList PA, PolynList PB);
// 在多项式中插入一项
Status InsertTerm(PolynList *P, PolynList N);
int num[10][2] ={1, 23,
2, 1,
4, 6,
5, 1,
9, 100,
11, 1,
12, 1,
13, 2,
18, 1,
45, 2};
int main(void)
{
system("pause");
}
Status CreatePolyn(PolynList *P, int n)
{
if( n < 1 )
return ERROR;
(*P) = (PolynList)malloc(sizeof(PolynList));
(*P)->expn = 0-n;
PolynList q = (*P);
int i = 0;
while(i<n)
{
q->next = (PolynList)malloc(sizeof(PolynList));
q = q->next;
q->coef = num[i][1];
q->expn = num[i][0];
i++;
}
q->next = NULL;
return OK;
}
Status PressPolyn(PolynList *P)
{
int num = 0, coef = 0;
int i;
(*P) = (PolynList)malloc(sizeof(PolynList));
PolynList q = (*P);
printf("*******************************************************************************\n");
printf("* 请输入你需要多项式的长度:\n* ");
scanf("%d", &num);
for(i=0; i<num; i++)
{
printf("* ");
scanf("%d",&coef);
if( coef != 0 )
{
q->next = (PolynList)malloc(sizeof(PolynList));
q = q->next;
q->coef = coef;
q->expn = i;
}
}
q->next = NULL;
if(!(*P)->next)
return ERROR;
printf("* 多项式创建成功");
printf("*******************************************************************************\n");
return OK;
}
Status PrintPolyn(PolynList P)
{
PolynList q;
if(!P->next)
return ERROR;
q = P->next;
printf("*******************************************************************************\n");
printf("* 该多项式为:");
while(q->next)
{
printf(" %dA%d +", q->coef, q->expn);
q = q->next;
}
printf(" %dA%d;\n", q->coef, q->expn);
printf("*******************************************************************************\n");
return OK;
}
Status DestroyPolyn(PolynList *P)
{
PolynList q,s;
q = (*P)->next;
while(q)
{
s = q->next;
free(q);
q = s;
}
(*P)->next = NULL;
return OK;
}
int PolynLength(PolynList P)
{
return -P->expn;
}
PolynList AddPolyn(PolynList PA, PolynList PB)
{
PolynList q, s, r;
if( (!PA->next) || (!PB->next) )
return ERROR;
q = PA->next;
s = PB->next;
PolynList PC = (PolynList)malloc(sizeof(PolynList));
PC->expn = -99;
r = PC;
while( q && s )
{
if( q->expn == s->expn )
{
// 两数相加
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = q->expn;
r->coef = q->coef + s->coef;
q = q->next;
s = s->next;
}else if( q->expn < s->expn )
{
// 将A中对应节点加入到多项式中
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = q->expn;
r->coef = q->coef;
q = q->next;
}else
{
// 将B中对应的节点加入到多项式中
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = s->expn;
r->coef = s->coef;
s = s->next;
}
}
if(s)
q = s;
while(q)
{
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = q->expn;
r->coef = q->coef;
q = q->next;
}
r->next = NULL;
return PC;
}
// 相减的其实差不多只不过在插入数值的时候还要考虑正负号,其实还有一个方法就是先遍历一个多项式然后变号。
PolynList SubPolyn(PolynList PA, PolynList PB)
{
PolynList q, s, r;
if( (!PA->next) || (!PB->next) )
return ERROR;
q = PA->next;
s = PB->next;
PolynList PC = (PolynList)malloc(sizeof(PolynList));
PC->expn = -99;
r = PC;
while( q && s )
{
if( q->expn == s->expn )
{
// 两数相加
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = q->expn;
r->coef = q->coef - s->coef;
q = q->next;
s = s->next;
}else if( q->expn < s->expn )
{
// 将A中对应节点加入到多项式中
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = q->expn;
r->coef = q->coef;
q = q->next;
}else
{
// 将B中对应的节点加入到多项式中
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = s->expn;
r->coef = -s->coef;
s = s->next;
}
}
while(q)
{
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = q->expn;
r->coef = q->coef;
q = q->next;
}
while(s)
{
r->next = (PolynList)malloc(sizeof(PolynList));
r = r->next;
r->expn = s->expn;
r->coef = -s->coef;
s = s->next;
}
r->next = NULL;
return PC;
}
PolynList MutPolyn(PolynList PA, PolynList PB)
{
PolynList PC = (PolynList)malloc(sizeof(PolynList));
PolynList q = PC, s = PA,r = PB, t;
while(s)
{
s = s->next;
while(r)
{
r = r->next;
t = (PolynList)malloc(sizeof(PolynList));
t->next = NULL;
t->coef = r->coef * s->coef;
t->expn = r->expn + s->expn;
InsertTerm(&PC, t);
}
}
return PC;
}
Status InsertTerm(PolynList *P, PolynList N)
{
PolynList q = (*P), s;
if(!q->next)
{
// 相当于创建链表,直接插入
s = (PolynList)malloc(sizeof(PolynList));
q->next = s;
s->expn = N->expn;
s->coef = N->coef;
s->next = NULL;
return OK;
}
while(q->next)
{
// 不断循环,分为插入节点的直接插入两种
if( q->expn == N->expn )
{
// 次数相同,直接写入
q->coef = N->coef;
q->expn = N->expn;
return OK;
}else if( (q->expn < N->expn) && (q->next->expn > N->expn) )
{
// 需要插入一个新的节点
s = (PolynList)malloc(sizeof(PolynList));
s->expn = N->expn;
s->coef = N->coef;
s->next = q->next;
q->next = s;
}
q = q->next;
}
// 如果是在结尾插入,那么再判断是写入还是插入
if(q->expn == N->expn)
{
q->coef += N->coef;
}else
{
s = (PolynList)malloc(sizeof(PolynList));
s->next = NULL;
s->coef = N->coef;
s->expn = N->expn;
q->next = s;
}
return OK;
}
此外,在我上一次的方案中有一种想法就是利用头节点的expn字段来用负数表示链表的长度,但是由于当时没有考虑系数也可以是负的因此此方法被废弃了,但是当系数全是正数时还是非常不错的。