The design function is to find the derivative of the polynomial of one element. (Note: The first-order derivative of xn (n is an integer) is n*xn-1. )
Input format: input polynomial non-0 coefficients and exponents in exponential degradation (absolute values are integers not exceeding 1000). The numbers are separated by a space.
Output format: Outputs the coefficients and exponents of the derivative polynomial not 0 in the same format as the input. The numbers are separated by a space, but cannot have extra spaces at the end. Note that the exponent and coefficients for the "0 polynomial" are 0, but are expressed as "0 0".
Input Sample:
3 4-5 2 6 1-2 0
Sample output:
12 3-10 1) 6 0
#include <stdio.h>#include<stdlib.h>typedefstructpolynomial{intCoeff; intExp; structPolynomial *Next;} Poly Poly*Creatnode ();voidInsert (Poly *head);voidDerivation (Poly *head);voidPrintlist (Poly *head); intMain () {Poly*l =Creatnode (); Insert (l); Derivation (l); Printlist (l); return 0;} Poly*Creatnode () {Poly*Head; Head= (Poly *)malloc(sizeof(poly)); Head->coeff =0; Head->exp =0; Head->next =NULL; returnhead;}voidInsert (Poly *head) {Poly*p, *l; inttemp; L=Head; Do{p=Creatnode (); scanf ("%d",&temp); P->coeff =temp; scanf ("%d",&temp); P->exp =temp; L->next =p; L= l->Next; } while(temp = GetChar ()! ='\ n'); }voidDerivation (Poly *head) {Poly*current,*prev; Current= prev =Head; while(Current-Next! =NULL) {prev=Current ; Current= CurrentNext; if(Head->next-EXP = =0&& head->next->next ==null)//there is only one constant entry{ Current->coeff =0; } Else if(Current->coeff = =0&& current->exp = =0) { ; } Else if(Current->exp = =0) {prev->next = CurrentNext; Free(current); Current=prev; } Else{ Current->coeff = Current->coeff *Exp; CurrentEXP = Current->exp-1; } }}voidPrintlist (Poly *head) {Poly*p = HeadNext; if(P! =NULL) { while(P->next! =NULL) {printf ("%d%d",p->coeff,p->EXP); P= P-Next; } printf ("%d%d",p->coeff,p->EXP); } }
View Code
Problems encountered when doing a problem have
At first, I wanted to implement the structure array, but I didn't think of the method of deleting an array of items. Instead, it is implemented with a linked list.
Key in the derivation process
Output [0 0] When there is only one constant term , [ 3 4 5 0] should be output [3]
Coeff = 0 or Exp =0
The exponent and coefficients of the "0 polynomial" are 0, but are expressed as "0 0"
Create A pointer p should first allocate space and then Prev->next = P, if the pointer after the declaration of Prev->next = p obtained by the result is Prev->next = = NULL!!!!!
//Wrong code
voidInsert (Poly *head) {Poly*p = head->Next; inttemp; Do{ if(p = =NULL) {P=Creatnode (); } scanf ("%d",&temp); P->coeff =temp; scanf ("%d",&temp); P->exp =temp; P= p->Next; } while(temp = GetChar ()! ='\ n');}
02-Linear Structure 2. Derivation of one-element polynomial