链表计算多项式的和,乘积

/******************************************************************************
*计算多项式的和,乘积
*使用链表完成
*核心代码: 合并同类项( joinSameIterm(pnode head) )
*多项式的和: 将两个链表合并成一个链表,对新的链表合并同类项
*多项式的积: 使用新链表记录乘积的结果,对新的链表合并同类项
*author: fangchang
*date:   2016/03/24
*time:   18:11
*********************************************************************************/


#include<stdio.h>
#include<stdlib.h>


typedef struct node {
int coef;                 //多项式系数
int exp;                  //多项式的幂
struct node * next;
}*pnode;


pnode createPoly();            //键盘输出一个多项式,scanf读取
void  printPoly(pnode head);   //打印一个多项式


pnode polyAdd(pnode head1, pnode head2);   //求和
pnode polyMult(pnode head1, pnode head2);  //求积
void  joinSameIterm(pnode head);           //合并同类项,core code


void answer();
int main() {
answer();
fflush(stdin);
getchar();
return 1;
}


void answer() {
printf("please input a poly: \n");
pnode head1 = createPoly();
printf("the poly is:\n");
printPoly(head1);
printf("please input another poly: \n");
pnode head2 = createPoly();
printf("another poly is:\n");
printPoly(head2);
pnode head3 = polyMult(head1,head2);
printf("the mult res is: \n");
printPoly(head3);
head1=polyAdd(head1,head2);
printf("the add res is: \n");
printPoly(head1);
}
pnode createPoly() {
int i,j;
pnode head=(pnode)malloc(sizeof(struct node));
pnode cur,pre;
if(NULL==head) {
return NULL;
}
head->coef=head->exp=0;
head->next=NULL;
pre=head;
while(scanf("%d%d",&i,&j) && i!=0) {
cur = (pnode)malloc(sizeof(struct node));
cur->coef = i;
cur->exp =  j;
cur->next=NULL;
pre->next=cur;
pre=cur;
}
return head;
}


void  printPoly(pnode head) {
if(head==NULL) {
return;
}
pnode p=head->next;
while(p) {
if(p->coef!=0) {
printf("%dx%d ",p->coef,p->exp);
}
p=p->next;
}
printf("\n\n");
}


pnode polyAdd(pnode head1, pnode head2) {            //修改了head1的内容,如果不想修改head1,可以使用head1的副本当此函数的参数
if(NULL==head1) {
return head2;
}
if(NULL==head2) {
return head1;
}
pnode p1=head1->next;
pnode p2=head2->next;
while(p1->next) {                 //找到p1的最后一个结点
p1=p1->next; 
}
p1->next=p2;                      //把p2合并在p1的尾端
printf("after add:\n");
printPoly(head1);
joinSameIterm(head1);             //合并同类项,得到最终结果
return head1;
}




pnode polyMult(pnode head1, pnode head2) {
if( !head1 || !head1->next || !head2 || !head2->next) {
return NULL;
}
pnode p1=head1->next;
pnode p2=head2->next;
pnode p,cur;
pnode head3 = (pnode) malloc(sizeof(struct node));       //使用新的链表(head3)保存乘积
if(!head3) {
return NULL;
}
head3->coef=head3->exp=0;
head3->next=NULL;
p=head3;
while(p1 && p1->coef) {                          //p1不为空,且系数不是0
p2=head2->next;
while(p2 && p2->coef) {
cur = (pnode) malloc(sizeof(struct node));
cur->coef=p1->coef * p2->coef;
cur->exp=p1->exp + p2->exp;
cur->next=NULL;
p->next = cur;
p = cur;
p2=p2->next;
}
p1=p1->next;
}
printf("after mult:\n");
printPoly(head3);
joinSameIterm(head3);
return head3;
}


void  joinSameIterm(pnode head) {                                       //core code  合并同类项
if(NULL==head || NULL==head->next || NULL==head->next->next ) {
return;
}
pnode p1=head->next;
pnode p2;
while(p1) {
p2=p1->next;
while(p2) {
if(p1->exp==p2->exp) {
p1->coef+=p2->coef;
p2->coef = 0;                                             //被合并的同类型,系数变为0(隐形的删除)
}
p2 = p2->next;
}
p1=p1->next;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值