1. 程式人生 > >多項式ADT筆記(數據結構c版)

多項式ADT筆記(數據結構c版)

col pow pro int exc ddp size 一個 for

項式ADT筆記
設多項式為P=A1Xn1+A2Xn2+···AiXni

//多項式ADT的數組實現
typedef struct 
{
    int CoeffArray[MaxDegree + 1];//各個多項式的系數A
    int HighPower;  //最高的冪ni
} * Polynomial;

//將多項式初始化為零的過程
void ZeroPolynomial(Polynomial Poly)
{
    int i;
    for( i = 0; i <= MaxDegree; i ++)
        Poly -> CoeffArray[i] = 0;
    Poly 
-> HighPower = 0; } //多項式相加 void AddPolynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PolySum) { int i; ZeroPolynomial(PolySum); PolySum -> HighPower = Max(Poly1 -> HighPower, Poly2 -> HighPower); for( i = PolySum -> HighPower; 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 -> HighPower = Poly1 -> HighPower + Poly2 -> HighPower; if (PolyProd ->
HighPower > MaxDegree) Error("Exceeded array size"); else for(i = 0; i <= Poly1 -> HighPower; i++) for(j = 0; j <= Poly2 -> HighPower; j++) PolyProd -> CoeffArray[i + j] += Poly1 -> CoeffArray[i] * Poly2 -> CoeffArray[j]; }

使用鏈表實現

typedef struct Node* PtrToNode;//Node對象

struct Node
{
    int Coefficient;//系數A
    int Exponent;//
    PtrToNode Next;//下一個Node的指針
};

typedef PtrToNode Polynomail;//下一個Node對象

多項式ADT筆記(數據結構c版)