多项式相乘-数组实现

多项式抽象数据类型 数组实现的类型声明

typedef struct
{
    int CoeffArray[ MaxDegree + 1 ];
    int HightPower;
}*Polynomial;

将多项式初始化为零

void ZeroPolynomial( Polynomial Poly)
{
    for (int i = 0;i <= MaxDegree; i++)
        Poly->CoeffArray[i] = 0;
    Poly->HightPower = 0;rr
}

两个多项式相加

void AddPolynomial(const Polynomial Poly1,const Polynomial Poly2,const Polynomial PolySum)
{
    int i;
    ZeroPolynomial(PoluSum);
    PolySum->HightPower = Max(Poly1->HightPower,Poly2->HightPower);
    for(i = PolySum->HightPower; i >= 0; i--)
        PolySum->CoeffArray[i] = Poly1->CoeffArray[i] + Poly2->CoeffArray[i];
}

两个多项式相乘

void MultPolynomial(const Polynomial Poly1,const Polynomial Poly2 Polynomial PolyProd)
{
    int i,j;
    ZeroPolynomial(PolyProd);
    PolyProd->HightPower = Poly1->HightPower + Poly2->HightPower;
    if (PolyProd->HightPower > MaxDegree)
        Error("Exceeded array size");
    else
        for(i = 0; i <= Poly1->HightPower; i++)
            for(j = 0; j <= Poly2->HightPower; j++)
                PolyPord->CoeffArray[i+j] += Poly1->CoeffArray[i]*Poly2->CoeffArray[j];
}

转载于:https://www.cnblogs.com/y3w3l/p/6349640.html

你可能感兴趣的:(多项式相乘-数组实现)