据结构实习(C语言)一元稀疏多项式的加减乘除四则运算

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一元稀疏多项式的加法运算
【问题描述】
设计一个实现一元稀疏多项式相加运算的演示程序。
【基本要求】
(1)输入并建立两个多项式;
(2)多项式a与b相加,建立和多项式c;
(3)输出多项式a,b,c。输出格式:比如多项式a为:A(x)=c1xe1+ c2xe2+…+ cmxem,其中,ci和ei分别为第i项的系数和指数,且各项按指数的升幂排列,即0≤e1<e2<…<em。多项式b,c类似输出。

程序如下:

/***************************************/
/*数据结构一元多项式的加减乘除四则运算*/
/*IceSnow Studio(冰风铃工作室) */
/*2007.01.30*/
/***********/
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<alloc.h>
struct list
{int zhishu;
int xishu;
struct list *next;
};
typedef struct list node;
typedef node *link;
/*****************************************************/
void free_list(link head)
{link pointer;
while(head!=NULL)
{pointer=head;
head=head->next;
free(pointer);}
}

/*****************************************************/
link rev(link head)
{link p1,p2;
p1=head->next;p2=p1->next;p1->next=NULL;
while(p2!=NULL)
{head->next=p2;
p2=p2->next;
head->next->next=p1;
p1=head->next;
}
return head;
}
/*****************************************************/
link find_list(link head,int key)
{link p0;
p0=head->next;
while(p0!=NULL)
{if(p0->zhishu==key) return p0;
p0=p0->next;
}
return p0;
}
/*****************************************************/
link find2_list(link head,int key)
{link p0;
p0=head;
while(p0!=NULL)
{if((p0->next->zhishu==key)&&(p0->next->xishu!=0))return p0;
p0=p0->next;
}
return p0;
}
/*****************************************************/
link clearzero(link head)
{link p0,pp;
p0=head;
while(p0!=NULL)
{if((p0->next->xishu==0)&&(p0->next->zhishu!=0))
{pp=p0->next;p0->next=pp->next;free(pp);}
p0=p0->next;
}
return head;
}
/*****************************************************/
int max_list(link head1,link head2)
{link p1;
link p2;
int temp=-10000;
p1=head1->next;
p2=head2->next;
while(p1!=NULL)
{if(p1->zhishu>temp)temp=p1->zhishu;p1=p1->next;};
while(p2!=NULL)
{if(p2->zhishu>temp)temp=p2->zhishu;p2=p2->next;};
return temp;
}
/*****************************************************/
link add_list(link head1,link head2)
{
link pointer1;
link pointer2;
link pointer3;
link new1;
link new2;
link head3;
int a=0;
int i=0;
head3=(link)malloc(sizeof(node));
if(head3==NULL)
{printf("Memory allocate Failure!\n");}
else
{head3->next=NULL;head3->zhishu=0;head3->xishu=0;};
pointer1=head1->next;pointer2=head2->next;pointer3=head3;

if((pointer1->zhishu)>=(pointer2->zhishu)) {i=pointer2-

>zhishu;}
else {i=pointer1->zhishu;};
while(1)
{
if(i>max_list(head1,head2))break;
a=0;
if(f
ind_list(head1,i)!=NULL){new2=find_list(head1,i);a=a+new2->xishu;};
if(find_list(head2,i)!=NULL){new2=find_list(head2,i);a=a+new2->xishu;};
if(a!=0)
{new1=(link)malloc(sizeof(node));
new1->zhishu=i;
new1->xishu=a;
new1->next=NULL;
pointer3->next=new1;
pointer3=new1;
};
i=i+1;
};
return head3;

}
/*****************************************************/
link init_list(link head)
{head=(link)malloc(sizeof(node));
if(head==NULL)
printf("Memory allocate Failure!\n");
else
{head->next=NULL;head->zhishu=0;head->xishu=0;}
return head;
}
/*****************************************************/
link jianfa(link head11,link head22)
{link p2;
p2=head22->next;
while(p2!=NULL)
{p2->xishu=(-1)*(p2->xishu);p2=p2->next;}
return add_list(head11,head22);
}

/*****************************************************/
link chufa(link head1,link head2)
{link head3=NULL,tp,new1,p3,p2,new2;
link temp=NULL;
head3=(link)malloc(sizeof(node)); head3->next=NULL;head3->zhishu=0;head3->xishu=0;
temp=(link)malloc(sizeof(node)); temp->next=NULL;temp->zhishu=0;temp->xishu=10;
init_list(head3);init_list(temp);
p3=head3;
while(1)
{if((head1->next->zhishu)<(head2->next->zhishu))
{p3->next=head1;break;};
new1=(link)malloc(sizeof(node));
new1->zhishu=(head1->next->zhishu)-(head2->next->zhishu);
new1->xishu=(head1->next->xishu)/(head2->next->xishu);
new1->next=NULL;p3->next=new1;p3=new1;

tp=temp;p2=head2->next;
while(p2!=NULL)
{ if((p2->xishu==0)&&(p2->zhishu==0)){break;};
new2=(link)malloc(sizeof(node));
new2->zhishu=p2->zhishu+p3->zhishu ;
new2->xishu=(p2->xishu)*(p3->xishu) ;
new2->next=NULL;
tp->next=new2;
tp=new2;
p2=p2->next;
};
head1=jianfa(rev(head1),rev(temp));
head1=rev(head1);
free_list(temp->next); temp->next=NULL;
};
return head3 ;
}
/*****************************************************/
link chengfa(link head1,link head2)
{link p1,p2,p3,new1,p,tp,p31;
link head3=NULL;
head3=(link)malloc(sizeof(node));
if(head3==NULL)
{printf("Memory allocate Failure!\n");}
else
{head3->next=NULL;head3->zhishu=0;head3->xishu=0;};
p1=head1->next;p2=head2->next;p3=head3;
while(p1!=NULL)
{p2=head2->next;
while(p2!=NULL)
{new1=(link)malloc(sizeof(node));
new1->zhishu=p1->zhishu+p2->zhishu;
new1->xishu=(p1->xishu)*(p2->xishu);
new1->next=NULL;
p3->next=new1;
p3=new1;
p2=p2->next;
}
p1=p1->next;
}
p3=head3->next;
while(p3!=NULL)
{p=p3;p31=p3;
while(p31!=NULL)
{if(find2_list(p31,p->zhishu)!=NULL)
{new1=find2_list(p31,p->zhishu);
p->xishu=p->xishu+new1->next->xishu;
tp=new1->next;new1->next=tp-&g

t;next;free(tp);
};
p31=p31->next;
};
p3=p3->next;
};
return clearzero(head3);
}
/*****************************************************/
void print_list(link head)
{link pointer;
pointer=head->next;
printf(" ");
while(pointer!=NULL)
{printf("%d x^%d ",pointer->xishu,pointer->zhishu);
if(pointer->next!=NULL)printf("+");
pointer=pointer->next;
}
printf("\n");}
/*****************************************************/
/*void print_list(link head)
{link pointer;
pointer=head->next;
printf("(zhishu , xishu)\n");
while(pointer!=NULL)
{printf("%d , %d\n",pointer ->zhishu,pointer->xishu);
pointer=pointer->next;
}
printf("\n"); }
/*****************************************************/
link create_list(link head)
{link new1;
link pointer;
head=(link)malloc(sizeof(node));
if(head==NULL)
printf("Memory allocate Failure!\n");
else
{head->next=NULL;head->zhishu=0;head->xishu=0;
pointer=head;
while(1)
{
new1=(link)malloc(sizeof(node));
printf("\ninput zhishu and xishu:");
scanf("%d %d",&(new1->zhishu),&(new1->xishu));
if (new1->xishu==0)
{printf(" ***** One linked list is ok! *****\n\n");break;};
new1->next=NULL;
pointer->next=new1;pointer=new1;
}/*while*/
}/*else*/
return head;
}
/*****************************************************/
void main()
{ int q=0;
printf("\nHello,The program starts!\n");
printf("***************************************************\n");
printf("说明:为链表输入数据,系数值为零时退出!\n");
printf("The zhishu of the polynomial is from high to low!\n");
printf("Finished when the xishu is zero!\n");
printf("The zhishu and xishu are all integer type!\n");
printf("***************************************************\n");

while(1){q=0;
printf("\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("Continiue to input datas !\n");
printf("(1)jia fa!\n");
printf("(2)jian fa!\n");
printf("(3)chu fa!\n");
printf("(4)cheng fa!\n");
printf("(0)Exit the system!\n");
scanf("%d",&q);
if(q!=0&&(q==1||q==2||q==3||q==4))
{
link head1=NULL;
link head2=NULL;
link head3=NULL;
head1=create_list(head1);
head2=create_list(head2);
if(head1!=NULL&&head2!=NULL)
{printf("The first polynomial:\n");
print_list(head1);
printf("The second polynomial:\n");
print_list(head2);
printf("Now the result is:\n");
switch(q){
case 1:head3=rev(add_list(rev(head1),rev(head2)));break;
case 2:head3=jianfa(head1,head2);break;
case 3:head3=chufa(head1,head2);break;
case 4:head3=chengfa(head1,head2);break;
def

ault:break;}
printf("The finished polynomial:\n");
print_list(head3);
printf("That is over!\n");
free_list(head1);
free_list(head2);
free_list(head3);
}/*if*/
if(head1==NULL&&head2==NULL){printf("\nThe list is empty!!!\n");};
}/*if*/
if(q==0){break;};
}/*while*/}


相关文档
最新文档