02-Linear Structure 2. Derivation of one-element polynomial

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.