Home > Back-end >  Small white refer to list to add and subtract polynomials
Small white refer to list to add and subtract polynomials

Time:10-08

Bosses to solve! Novice can't figure out, liao (crying)
Modify the output for a long time didn't get to,
Don't know how to input a line list at the same time more data?
Why does the scanf always only lost once?
How to add those two data in the list for the third list output?
Thank you big!

CodePudding user response:

For example
Int cm [100]={0};//assume up not more than 100 times the power
Int Max=0, c=0, m=0;
While (1) {//the first line of the input
The scanf (" % d % d ", & amp; C., & amp; M);
If (c==0 & amp; & M==0) break;
Cm + [m]=c;
If (max}
While (1) {//the second line of the input
The scanf (" % d % d ", & amp; C., & amp; M);
If (c==0 & amp; & M==0) break;
Cm + [m]=c;
If (max}
for (; Cm (Max)==0; Max -);
//print
If (cm [0] & gt; 0) printf (" % d ", cm [0]).//constant term
If (max> 0) printf (" + ");
for(int i=1; iIf (cm [I] & gt; 0 {
Printf (" % d * x ^ % d + "cm [I], I);
}
}
If (max> * x ^ 0) printf (" % d % d "cm/Max, Max).



CodePudding user response:

Change a place to
For (int I=1; iIf (cm [I]!=0) {//range here to switch to
Printf (" % d * x ^ % d + "cm [I], I);
}
}



CodePudding user response:

List ways to

# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Math. H>

# define EPSLON 1 e - 6
Typedef struct Polynomial {
Float coef.//coefficient
Int power;//power
Struct Polynomial * next;
} Poly real;


Poly real * init () {
//construct polynomial, keep short
Poly real * head=(Poly real *) malloc (sizeof (Poly real));
The head - & gt; Next=NULL;
return head;
}

//
//to insert a linked list of
//insert algorithm that small power in front of big in the back, the same will merge (combined coefficient is 0 is removed)
//addition and subtraction can use this function to achieve
Void insert (Poly real * head, float coef, int power) {
If (NULL==head) return;
Poly real * p=head;
While (p - & gt; Next) {
If (power & lt; P - & gt; Next - & gt; Power) {//insert
Poly real * pT=(Poly real *) malloc (sizeof (Poly real));
PT - & gt; Power=power;
PT - & gt; Coef=coef;
PT - & gt; Next=p - & gt; Next;
P - & gt; Next=pT;
return;
} else if (power==p - & gt; Next - & gt; Power) {
//merge
If (fabs (coef + p - & gt; Next - & gt; Coef) & lt; EPSLON) {
//the combined coefficient is 0. Float type cannot be directly judged==
Poly real * pT=p - & gt; Next;
P - & gt; Next=p - & gt; Next - & gt; Next;
Free (pT);
return;
} else {
P - & gt; Next - & gt; Coef +=coef;
return;
}
} the else
P=p - & gt; Next;
}
If (p - & gt; Next==NULL) {//to the tail, directly inserted into the
P - & gt; Next=(Poly real *) malloc (sizeof (Poly real));
P - & gt; Next - & gt; Power=power;
P - & gt; Next - & gt; Coef=coef;
P - & gt; Next - & gt; Next=NULL;
}
}

Void printPoly (Poly real * head) {
If (NULL==head) return;
Poly real * p=head;
While (p - & gt; Next) {
If (the head - & gt; Next==p - & gt; Next)
Printf (" % g * x ^ % d ", p - & gt; Next - & gt; Coef, p - & gt; Next - & gt; Power);
The else
Printf (" + % g * x ^ % d ", p - & gt; Next - & gt; Coef, p - & gt; Next - & gt; Power);
P=p - & gt; Next;
}
}
Int main () {
Poly real * head;
The head=init ();
While (1) {
Float coef. Int power;
The scanf (" % f, % d ", & amp; Coef, & amp; Power);
If (power!=0 & amp; & Fabs (coef) & gt; EPSLON)
Insert (head, coef, power);
The else
break;
}
While (1) {
Float coef. Int power;
The scanf (" % f, % d ", & amp; Coef, & amp; Power);
If (power!=0 & amp; & Fabs (coef) & gt; EPSLON)
Insert (head, coef, power);
The else
break;
}

PrintPoly (head);
return 0;
}

Not how to test, test yourself

CodePudding user response:

Fun
reference 3 floor response:
list way

# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Math. H>

# define EPSLON 1 e - 6
Typedef struct Polynomial {
Float coef.//coefficient
Int power;//power
Struct Polynomial * next;
} Poly real;


Poly real * init () {
//construct polynomial, keep short
Poly real * head=(Poly real *) malloc (sizeof (Poly real));
The head - & gt; Next=NULL;
return head;
}

//
//to insert a linked list of
//insert algorithm that small power in front of big in the back, the same will merge (combined coefficient is 0 is removed)
//addition and subtraction can use this function to achieve
Void insert (Poly real * head, float coef, int power) {
If (NULL==head) return;
Poly real * p=head;
While (p - & gt; Next) {
If (power & lt; P - & gt; Next - & gt; Power) {//insert
Poly real * pT=(Poly real *) malloc (sizeof (Poly real));
PT - & gt; Power=power;
PT - & gt; Coef=coef;
PT - & gt; Next=p - & gt; Next;
P - & gt; Next=pT;
return;
} else if (power==p - & gt; Next - & gt; Power) {
//merge
If (fabs (coef + p - & gt; Next - & gt; Coef) & lt; EPSLON) {
//the combined coefficient is 0. Float type cannot be directly judged==
Poly real * pT=p - & gt; Next;
P - & gt; Next=p - & gt; Next - & gt; Next;
Free (pT);
return;
} else {
P - & gt; Next - & gt; Coef +=coef;
return;
}
} the else
P=p - & gt; Next;
}
If (p - & gt; Next==NULL) {//to the tail, directly inserted into the
P - & gt; Next=(Poly real *) malloc (sizeof (Poly real));
P - & gt; Next - & gt; Power=power;
P - & gt; Next - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related