Home > database >  For c bosses
For c bosses

Time:09-29

# include & lt; stdio.h>
# include & lt; stdlib.h>
Typedef struct node/* polynomials, storage coefficient and index * */
{
Float c; Float e;
} Element;

Typedef struct Node polynomial list * *//*
{
Element data; Struct Node * Next;
PolyList} * and * Position;

Void CreatPoly (PolyList * L)/* build M a polynomial * */
{
If (* L==NULL)/* head node polynomial coefficient of 0.0, index - 1 * */
{
* L=malloc (sizeof (struct Node));
(* L) - & gt; Next=NULL;
(* L) - & gt; Data. C=0.0;
(* L) - & gt; Data. E=1;

}
The Position of Tmp=* L;
Float c, e,
While (the scanf (" % f ", & amp; C) & amp; & The scanf (" % f ", & amp; E))//* * * read coefficient and index
{
The Position NewNode=malloc (sizeof (struct Node));
NewNode - & gt; Next=NULL;
NewNode - & gt; Data. C=c;
NewNode - & gt; Data. E=e;
Tmp - & gt; Next=NewNode;/* tail inserting new node */
Tmp=NewNode;
}

}

Void ShowPoly (PolyList * L)
{
The Position of Tmp=* L;
If (Tmp - & gt; Next==NULL)
Printf (" \ n polynomial is null ");
The else
{
While (Tmp - & gt; Next!=NULL)
{if (Tmp - & gt; Next - & gt; Next==NULL)/* * */the last item on the
Printf (" %. 1 fx ^ %. 0 f ", Tmp - & gt; Next - & gt; Data. J c, Tmp - & gt; Next - & gt; Data. E);
The else
Printf (" %. 1 fx ^ %. 0 f + ", Tmp - & gt; Next - & gt; Data. J c, Tmp - & gt; Next - & gt; Data. E);
Tmp=Tmp - & gt; Next;
}
}
printf("\n");
}
Void InsertsortedPoly (PolyList * L, Position NewNode)/* order insert node polynomials, polynomial must have a head node * */
{
//the second node is inserted into the list in the list 1
The Position of Tmp=* L;
While (Tmp - & gt; Next!=NULL)
{
If (Tmp - & gt; Next - & gt; Data. E==NewNode - & gt; Data. E)
{
Tmp - & gt; Next - & gt; Data. C +=NewNode - & gt; Data. C;
return;
}
Else if (Tmp - & gt; Next - & gt; Data. E{
Tmp=Tmp - & gt; Next;
}
The else/* * * inserting new node/
{
NewNode - & gt; Next=Tmp - & gt; Next;
Tmp - & gt; Next=NewNode;
return;
}
}

Tmp - & gt; Next=NewNode;/* insert the new node to the beginning or the end of the polynomial * */

}
Void InisortedPoly (PolyList * L)/* build M order polynomial, can be arbitrary input/* *
{

If (* L==NULL)/* generate head node polynomial coefficient of 0.0, index - 1 * */
{
* L=malloc (sizeof (struct Node));
(* L) - & gt; Next=NULL;/* important * */
(* L) - & gt; Data. C=0.0;
(* L) - & gt; Data. E=1;

}
The Position of Tmp=* L;
Float c, e,
While (the scanf (" % f ", & amp; C) & amp; & The scanf (" % f ", & amp; E))//* * * read coefficient and index
{
//the scanf (" % f % f ", & amp; C., & amp; E) if (e<0.0) continue;
The Position NewNode=malloc (sizeof (struct Node));
NewNode - & gt; Next=NULL;/* important * */
NewNode - & gt; Data. C=c;
NewNode - & gt; Data. E=e;
InsertsortedPoly (& amp; Tmp, NewNode);

}

}
Void AddPolynomian (PolyList * A, PolyList * B)//merge the two similar terms in the list
{
The Position Pa, Pb, Tmp;
Pa=* A; Pb=* B;
While (Pa - & gt; Next!=NULL & amp; & Pb - & gt; Next!=NULL)
{if (Pa - & gt; Next - & gt; Data. E==Pb - & gt; Next - & gt; Data. E)
{
Pa - & gt; Next - & gt; Data. C +=Pb - & gt; Next - & gt; Data. C;
Tmp=Pb - & gt; Next;
Pb - & gt; Next=Tmp - & gt; Next;
Tmp - & gt; Next=NULL;
Free (Tmp);
Pa=Pa - & gt; Next;
}
Else if (Pa - & gt; Next - & gt; Data. E{
Pa=Pa - & gt; Next;
}
The else/* will Pb after subsequent node is inserted into the Pa * */
{
Tmp=Pb - & gt; Next;
Pb - & gt; Next=Tmp - & gt; Next;
Tmp - & gt; Next=Pa - & gt; Next;
Pa - & gt; Next=Tmp;
}}
If (Pa - & gt; Next==NULL)
{
Pa - & gt; Next=Pb - & gt; Next;
Pb - & gt; Next=NULL;
}
}
PolyList the mul (PolyList * A, PolyList * B)
{
The Position (p1, p2, p3, L, Lm;
L=(PolyList) malloc (sizeof (struct Node));
P1=(* A) - & gt; Next;
The p2=(* B) - & gt; Next;
L-> Next=NULL;
If (p1 & amp; & P2) {
A) for (p1=(* - & gt; Next; P1. P1=p1 - & gt; Next) {
Lm=(PolyList) malloc (sizeof (struct Node));
Lm - & gt; Next=NULL;
P3=Lm;
For (p2=(* B) - & gt; Next; The p2. The p2=p2 - & gt; Next) {
PolyList p4=(PolyList) malloc (sizeof (struct Node));//stern interpolation
P4 - & gt; Next - & gt; Data. C=p1 - & gt; Next - & gt; Data. C * p2 - & gt; Next - & gt; Data. C;
P4 - & gt; Next - & gt; Data. E=p1 - & gt; Next - & gt; Data. E + p2 - & gt; Next - & gt; Data. E;
P3 - & gt; Next=p4;
P3=p4;
}
P3 - & gt; Next=NULL;//if not write, memory error



}
}
Return Lm;
}
Int main (int arg c, const char * argv [])
{
PolyList L=NULL;
PolyList M=NULL;
PolyList N;
//IniPoly (& amp; L);
//ShowPoly (& amp; L);
InisortedPoly (& amp; L);
ShowPoly (& amp; L);
The fflush (stdin);
InisortedPoly (& amp; M);
ShowPoly (& amp; M);

Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n \ n \ n ");
N=the mul (& amp; L, & amp; M);
ShowPoly (& amp; N);
return 0;
}
Trouble bosses to look at, I want to be a polynomial multiplication, the function has a problem, but the method can't display the results, I also don't know where is wrong, who can help me make up for this function, kneeling? Really
  • Related