CodePudding user response:
Bosses, help me, thank you very muchCodePudding user response:
Fyi://list of one yuan polynomial addition subtraction multiplication
#include
#include
Typedef struct node {
Float coef.//coefficient
Int expn;//index
Struct node * next;
}
PolyNode;//polynomial polynomial node node
Typedef PolyNode * Polynomial;
Polynomial createPolynomial () {//create a Polynomial
PolyNode * p * q, * head=(PolyNode *) malloc (sizeof (PolyNode));//head node
The head - & gt; Next=NULL;
Float coef.
Int expn;
Printf (" input the polynomial of each coefficient and index, and each row, enter 0 0 over! \n");
While (the scanf (" % f, % d ", & amp; Coef, & amp; Expn) & amp; & Coef) {//by default, according to the descending index
If (the head - & gt; Next) {
P=the head;
While (p - & gt; Next & amp; & Expn & lt; P - & gt; Next - & gt; Expn)
P=p - & gt; next;
If (p - & gt; Next) {
If (expn==p - & gt; Next - & gt; Expn) {//have the same index directly add to the original polynomial coefficient
P - & gt; Next - & gt; Coef +=coef;
If (p - & gt; Next - & gt; Coef & gt; 0.000001 & amp; & P - & gt; Next - & gt; Coef & lt; 0.000001) {//if the combined coefficient is 0, then abandon the node
Q=p - & gt; next;
P - & gt; Next=q - & gt; next;
Free (q);
}
} else {
Q=(PolyNode *) malloc (sizeof (PolyNode));
Q - & gt; Coef=coef;
Q - & gt; Expn=expn;
Q - & gt; Next=p - & gt; next;
P - & gt; Next=q;
}
} else {
P - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
P=p - & gt; next;
P - & gt; Coef=coef;
P - & gt; Expn=expn;
P - & gt; Next=NULL;
}
} else {
The head - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
The head - & gt; Next - & gt; Coef=coef;
The head - & gt; Next - & gt; Expn=expn;
The head - & gt; Next - & gt; Next=NULL;
}
}
return head;
}
Polynomial multiply (Polynomial poly real, float coef, int expn) {//Polynomial with specified monomial multiplication, the monomial for coefx ^ expn
PolyNode * p, * q * Poly real=(PolyNode *) malloc (sizeof (PolyNode));
P=Poly real;
Q=poly real - & gt; next;
While (q) {
P - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
P=p - & gt; next;
P - & gt; Coef=(q - & gt; Coef * coef);
P - & gt; Expn=(q - & gt; Expn + expn);
Q=q - & gt; next;
}
P - & gt; Next=NULL;
Return Poly real;
}
Void the add (Polynomial poly1, Polynomial poly2) {//add to the poly1 poly2
PolyNode * p * q * r;
R=poly1;
P=poly1 - & gt; next;//points to the first node
Q=poly2 - & gt; next;
Poly2 - & gt; Next=NULL;
While (p & amp; & Q) {
If (p - & gt; Expn & gt; Q - & gt; Expn) {
R - & gt; Next=p;
P=p - & gt; next;
R=r - & gt; next;
} else if (p - & gt; Expn & lt; Q - & gt; Expn) {
R - & gt; Next=q;
Q=q - & gt; next;
R=r - & gt; next;
} else {
PolyNode * t;
P - & gt; Coef +=q - & gt; Coef.
if (! (p - & gt; Coef & gt; 0.000001 & amp; & P - & gt; Coef & lt; 0.000001))//coefficient 0
{
R - & gt; Next=p;
R=r - & gt; next;
P=p - & gt; next;
} else {
T=p;
P=p - & gt; next;
Free (t);
}
T=q;
Q=q - & gt; next;
Free (t);
}
}
If (p)
R - & gt; Next=p;
If (q)
R - & gt; Next=q;
}
Polynomial polySubtract (Polynomial poly1, Polynomial poly2) {//Polynomial subtraction poly1 - poly2 to form a new Polynomial
//take opposite, poly2 coefficient of forming a new polynomial
Polynomial poly real=(PolyNode *) malloc (sizeof (PolyNode));//structure head node
PolyNode * p * q;
P=poly real;
Q=poly2 - & gt; next;
While (q) {
P - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
P=p - & gt; next;
P - & gt; Coef=- (q - & gt; Coef);//coefficient against
P - & gt; Expn=q - & gt; Expn;
Q=q - & gt; next;
}
P - & gt; Next=NULL;
Add (poly real poly1);//addition
Return poly real;
}
Polynomial polyAdd (Polynomial poly1, Polynomial poly2) {//Polynomial addition poly1 + poly2 to form a new Polynomial
Polynomial poly real=(PolyNode *) malloc (sizeof (PolyNode));//and the head of a polynomial node
Poly real - & gt; Next=NULL;
PolyNode * p * q * r;
R=poly real;
P=poly1 - & gt; next;
Q=poly2 - & gt; next;
While (p& & Q) {
If (p - & gt; Expn & gt; Q - & gt; Expn) {
R - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
R=r - & gt; next;
R - & gt; Coef=p - & gt; Coef.
R - & gt; Expn=p - & gt; Expn;
P=p - & gt; next;
} else if (p - & gt; Expn & lt; Q - & gt; Expn) {
R - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
R=r - & gt; next;
R - & gt; Coef=q - & gt; Coef.
R - & gt; Expn=q - & gt; Expn;
Q=q - & gt; next;
} else {
Float m=p - & gt; Coef + q - & gt; Coef.
if (! (m & gt; 0.000001 & amp; & M & lt; {
0.000001))R - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
R=r - & gt; next;
R - & gt; Coef=m;
R - & gt; Expn=p - & gt; Expn;
}
Q=q - & gt; next;
P=p - & gt; next;
}
}
While (p) {
R - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
R=r - & gt; next;
R - & gt; Coef=p - & gt; Coef.
R - & gt; Expn=p - & gt; Expn;
P=p - & gt; next;
}
While (q) {
R - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
R=r - & gt; next;
R - & gt; Coef=q - & gt; Coef.
R - & gt; Expn=q - & gt; Expn;
Q=q - & gt; next;
}
R - & gt; Next=NULL;
Return poly real; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull