码迷,mamicode.com
首页 > 其他好文 > 详细

多项式ADT

时间:2015-06-27 01:10:29      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
/*删除表的正确方法*/
/*assume header*/
void
DeleteList(List L)
{
    Position p,Tmp;
    p=L->Next;
    while(p != NULL)
    {
        Tmp=p->Next;
        free(p);
        p = Tmp;
    }
}//先free掉,然后再判断这个结点的Next是不是NULL,是就不做了
/*多项式ADT*/
/*多项式数组实现的类型声明*/
/*一个结构就可以表示。一个多项式*/
typedef struct{
    CoeffArray[MaxDegree+1];
    int HighPower;//表示这个数组的最高次
} * Polynomial;
/*将多项式初始化为0*/
void
ZeroPolynomial(Polynomial Poly)
{
    for(i=0;i <= MaxDegree ;i++)
    {
        Poly->CoeffArray[i]=0;
    }
    Poly->HighPower = 0;
}
/*两个多项式相加的过程*/
void
AddPolynomial(Polynomial Poly1,Polynomial Poly2,Polynomial PolySum)
{
    ZeroPolynomial(PolySum);
    MaxPower=Max2(Poly1->HighPower,Poly2->HighPower);
    for(i=0;i<=MaxPower;i++)
    {
        PolySum->CoeffArray[i] = Poly1->CoeffArray[i] + Poly2->CoeffAyyay[i];
    }
    PolySum->HighPower=MaxPower;
}
/*两个多项式相乘的过程*/
void
MultPolynomial(Polynomial Poly1,Polynomial Poly2,Polynomial PolyProd)
{
    ZeroPolynomial(PolyProd);
    PolyProd->HighPower = Poly1->HighPower * Poly2->HighPower;
    for(int i=0; i <= Poly1->HighPower; i++)
    {
        for(int j=0; j <= Poly2->HighPower; j++)
        {
            PolyProd->CoeffArray[i+j] += Poly1->CoeffArray[i] * Poly2->CoeffArray[j];
        }
    }
}
View Code

 

多项式ADT

标签:

原文地址:http://www.cnblogs.com/gabygoole/p/4603455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!