Home > Back-end >  For help, excuse me this why didn't achieve the goal of want? (to enter two polynomial and summ
For help, excuse me this why didn't achieve the goal of want? (to enter two polynomial and summ

Time:10-19

The header file
Struct Node;
Struct PN;
Typedef struct Node Node;
Typedef struct PN ElemType;
Typedef struct Node * Polynomial;
Typedef struct Node * List;//the List L==Node * p;
Typedef Node * Position;
Int Is (ElemType a, ElemType b);
Int IsEmpty (List L);
Int IsLast (Position p, the List L);
Position the Find (ElemType X, the List L);
Void the Delete (ElemType X, the List L);
Void Insert (ElemType X, the List L, the Position P);
The List InitList ();
ElemType InitPN ();
Polynomial CreatePolynomial (int n);
Polynomial AddPolynomial (Polynomial Pa, Polynomial Pb);
Void PrintPN Polynomial (PN);

Application:
#include
# include "List. H"

Int main ()
{
Polynomial Pa, Pb, Pc;

Pa=CreatePolynomial (5);

Pb=CreatePolynomial (6);

Pc=AddPolynomial (Pa, Pb);


PrintPN (Pc);

return 0;
}



Struct PN
{
Double coef.
Int expn;
};

Struct Node
{
ElemType data;
struct LNode *next;
};

Int IsEmpty (List L)//test List L is empty table
{
The return of L - & gt; Next==NULL;
}

Int IsLast (Position p, the List L)//whether this element is the last element in the table
{
The return p - & gt; Next==NULL;
}

Position the Find (ElemType X, the List L)
{
The Position of P;

For (P=L; P; P=P - & gt; Next)
{
If (Is (P - & gt; Data, X))
{
Printf (" Find it!" );
break;
}
}
If (P=NULL) printf (" Can 't Find!" );

Return the P;
}

Int Is ElemType a, ElemType (b)
{
Int bool=0;
If (a.c oef==biggest oef & amp; & A.e XPN==b.e XPN)
Bool=1;
The else bool=0;

Return a bool.
}

Void the Delete (ElemType X, the List L)
{
The Position of P, TmpCell;

P=Find (X, L);

If (P!=NULL)
{
TmpCell=P;

P=P - & gt; next;

Free (TmpCell);
}
}

Void Insert (ElemType X, the List L, the Position P)
{
The Position TmpCell;

TmpCell=malloc (sizeof (struct Node));

If (TmpCell==NULL)
{
Printf (" Out of Space ");
}

TmpCell - & gt; Next=P - & gt; next;
P - & gt; Next=TmpCell;
}//make sure TmpCell not NULL

The List InitList ()
{
The List of L;

L=malloc sizeof (struct Node));

L - & gt; Data=(https://bbs.csdn.net/topics/InitPN);

L - & gt; Next=NULL;

}

ElemType InitPN ()
{
ElemType PN;
PN. Coef=0;
PN. Expn=0;

The return PN;
}

Struct PN CreatPN ()
{
ElemType PN;

The scanf (" please input coefficient, number: lf % % d ", & amp; PN. Coef, & amp; PN. Expn);

The return PN;
}

Polynomial CreatePolynomial (int n)
{
The List of L=InitList ();
The Position of P=L;

For (int I=1; I & lt;=n; I++)
{
The Position s=malloc (sizeof (struct Node));
The scanf (" please input coefficient, number: lf % % d ", & amp; S - & gt; Data. Coef, & amp; S - & gt; Data. The expn);
The Position of the pre=P, q=P - & gt; next;

While (q & amp; & Q - & gt; Data. The expn & lt; S - & gt; Data. Expn)
{
The pre=q;
Q=q - & gt; next;
}
S - & gt; Next=q;
The pre - & gt; Next=s;

}

return L;
}

Polynomial AddPolynomial (Polynomial Pa, Polynomial Pb)
{
The Position p1=Pa - & gt; Next, the p2=Pb - & gt; Next;
The Position of p3=Pa;

While (p1 & amp; & P2)
{
If (p1 - & gt; Data. The expn==p2 - & gt; Data. Expn)
{
Double sum=p1 - & gt; Data. Coef + p2 - & gt; Data. The expn;

If (sum!=0)
{
P1 - & gt; Data. Coef=sum;
P3 - & gt; Next=p;
P3=p;
The Position r=p2;
The p2=p2 - & gt; next;
Free (r);
}
The else
{
The Position r1=p1, r2=p2;
P1=p1 - & gt; next;
The p2=p2 - & gt; next;
Free (r1);
Free (r2).
}

}
Else if (p1 - & gt; Data. The expn & lt; The p2 - & gt; Data. Expn)
{
P3 - & gt; Next=p;
P3=p;
P1=p1 - & gt; next;
}
The else
{
P3 - & gt; Next=p2;
P3=p2;
The p2=p2 - & gt; next;
}
}
If (p1==NULL)
{
P3 - & gt; Next=p2;
}
The else
{
P3 - & gt; Next=p;
}

Free (Pb);

Return (Pa);
}

Void PrintPN Polynomial (PN)
{
Int CNT=1;
For (the Position p; p; P=p - & gt; Next)
{
Printf (" % fX ^ % d + ", the PN - & gt; Data. Coef, PN - & gt; Data. The expn);
If (CNT % 5==0)
{
Printf (" \ n ");
}
Cnt++;
}
}
  • Related