您的位置:首页 > 其它

用List 完成多项式的四则运算

2009-04-12 11:59 211 查看
#include <stdio.h>
#include <conio.h>


#define SIZE1 4
#define SIZE2 4


typedef struct node
{
int Coefficient;
int Exponent;
struct node *link;
}Node, *List;


void InitList(List *p)
{
List q;
q =(List)malloc(sizeof(Node));
q->link=NULL;
*p = q;
return;
}


void InsertList(List H,int i,int coe, int exp)
{ int j=1;
List p,q;
p = H;
while(p->link!=NULL && j<i)
{
p = p->link;
j++;
}


q =(List)malloc(sizeof(Node));
q->Coefficient=coe;
q->Exponent=exp;
q->link=p->link;
p->link=q;
return;
}
void TravList(List H)
{
List p;
p = H->link->link;
printf("%dX^%d",H->link->Coefficient, H->link->Exponent);
while(p!=NULL)
{
if(p->Coefficient>0)
printf("+");
printf("%d",p->Coefficient );
if(p->Exponent!=0)
printf("X^%d",p->Exponent);


p=p->link;
}
printf("/n");
return;
}
void DeleteList(List H,int i)
{ int j=1;
List p,q;
p = H;
while(p->link!=NULL && j<i)
{ p = p->link;j++;}
q = p->link;
p->link=p->link->link;
free(q);
return;
}


void muliPolynomial(List L1, List L2, List L3)
{
List p1,p2,p3;
int i=1,power,coe,add=0;
p1=L1->link;
p2=L2->link;
InsertList(L3,i,p1->Coefficient * p2->Coefficient,p1->Exponent+p2->Exponent); /*get the first element of the new list*/
p2=p2->link;
p3=L3->link;


while(p1 != NULL)
{
while(p2 != NULL)
{
power=p1->Exponent+p2->Exponent;
coe= p1->Coefficient * p2->Coefficient;


while(p3!=NULL)
{
if(p3->Exponent==power)
{
p3->Coefficient+=coe;
if(p3->Coefficient==0)
{
DeleteList(L3,i);
i--;
}
add=1;
break;
}
i++;
p3=p3->link;
}
if(add!=1)
{
InsertList(L3,i,coe,power);


}
add=0;
p2=p2->link;
i=1;
p3=L3->link;


}
p2=L2->link;
p1=p1->link;
add=0;
}
return;
}
void addPolynomial(List L1, List L2, List L3)
{
List p1,p2,p3,p;
int i=1;
p1=L1->link;
p2=L2->link;
p3=L3;


while(p1!=NULL&&p2!=NULL)
{
/*if the exponent is the same, add and put the result to the new list*/
if(p1->Exponent==p2->Exponent)
{
if(p1->Coefficient+p2->Coefficient!=0)
InsertList(L3,i,p1->Coefficient+p2->Coefficient,p1->Exponent) ;
p1=p1->link;
p2=p2->link;


}
/*if not the same, put the higher one into the new list
,and and move the pointer to the next and compare again*/
else if(p1->Exponent>p2->Exponent)
{
InsertList(L3,i,p1->Coefficient,p1->Exponent);
p1=p1->link;
}
else if(p1->Exponent<p2->Exponent)
{
InsertList(L3,i,p2->Coefficient,p2->Exponent);
p2=p2->link;
}
i++;
p3=p3->link;
}
/*if one list is empty and the other is not, put the rest into the new list*/
if(p1!=NULL)
p3->link=p1;
else if(p2!=NULL)
p3->link=p2;


return;
}
/*在这个程序中,我们假设拥有高阶的项在链表中始终在排在低阶项之前*/
void main()
{
List list1,list2,list3,list4;
int i;
/*int content1[4] ={1,2,-1,0}; */
int content1[SIZE1] ={1,2,-1,0}; /*位数从1算起,奇数位为系数,偶数位为指数*/
int content2[SIZE2] ={1,2,1,0};


InitList(&list1);
InitList(&list2);
InitList(&list3);
InitList(&list4);


for(i=0;i<SIZE1;i=i+2)
{
InsertList(list1,i/2+1,content1[i],content1[i+1]);
}
for(i=0;i<SIZE2;i=i+2)
{
InsertList(list2,i/2+1,content2[i],content2[i+1]);
} /**/
TravList(list1);
TravList(list2);
printf("The result of multiplying two polynomials:/n");
muliPolynomial(list1, list2, list4) ;
addPolynomial(list1,list2,list3);
TravList(list3);
printf("/nThe result of adding two polynomials:/n");
TravList(list4);
getch();
return;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐