List application: Unary polynomial operator.
Basic requirements:
(1) Enter and establish the polynomial, and display the polynomial with friendly interface, for example, 8x3-6x2+8 display as 8x^3-6x^2+8;
(2) Calculating the addition and subtraction of two polynomial;
(3) Given x, calculates the value of the polynomial at x
The code is as follows
#include <stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>typedefstructpolynode{intCoef; intexp; Polynode*Next;} Polynode,*polylist;voidInitlist (Polylist *m); Polylist createlist (polylist L);voidshowlist (polylist L);voidPolyadd (polylist Polya, Polylist polyb, polylist polyc);voidpolysub (polylist Polya, Polylist polyb, polylist polyc);intEvaluationatx (Polylist L,intx);voidInitlist (Polylist *L) { *l = (polylist)malloc(sizeof(polylist)); (*L)->next =NULL;}/*create a linked list of stored polynomial, the input book coefficient is small change*/polylist createlist (polylist L) {Polynode*pre, *Rear; Pre= Rear =L; Rear= (Polynode *)malloc(sizeof(Polynode)); scanf ("%d%d", &rear->coef, &rear->exp); while(rear->Coef) {Pre->next =Rear; Pre=Rear; Rear= (polynode*)malloc(sizeof(Polynode)); scanf ("%d%d", &rear->coef, &rear->exp); } Free(rear); Pre->next =NULL; returnL;}//output polynomial, note the format, and show the detailsvoidshowlist (polylist L) {Polynode*p = l->Next; if(P->exp = =0) {printf ("%d", p->Coef); } Else{printf ("%dx^%d", P->coef, p->exp); } for(p = p->next; p! = NULL; p = p->next) { if(P->exp = =0 ){ if(P->coef <0) printf ("%d", p->Coef); Elseprintf ("+%d", p->Coef); } Else{ if(P->coef <0){ if(P->coef = =-1&& P->exp = =1) {printf ("- x"); } Else if(P->coef! =-1&& P->exp = =1) {printf ("%DX", p->Coef); } Else if(P->coef = =-1&& P->exp! =1) {printf ("-x^%d", p->exp); } Else{printf ("%dx^%d", P->coef, p->exp); } } Else{ if(P->coef = =1&& P->exp = =1) {printf ("+x"); } Else if(P->coef! =1&& P->exp = =1) {printf ("+%DX", p->Coef); } Else if(P->coef = =1&& P->exp! =1) {printf ("+x^%d", p->exp); } Else{printf ("+%dx^%d", P->coef, p->exp); } }}} printf ("\ n");}/*The POLYC list is used to save the added and obtained results, not to use the existing linked list nodes, or to destroy the linked list nodes, otherwise the operation is only a one-time implementation of the following functions Polyc = Polya + polyb*/ voidPolyadd (polylist Polya, Polylist polyb, Polylist polyc) {Polynode*p, *q, *pre, *Rear; intsum; P= polya->Next; Q= polyb->Next; Pre= Rear =Polyc; while(P! = NULL && Q! = null) {//the size of the coefficients is only three cases, the classification process if(P->exp < q->exp) {Rear= (polynode*)malloc(sizeof(Polynode)); Rear->coef = p->Coef; Rear->exp = p->exp; Pre->next =Rear; Pre=Rear; P= p->Next; } Else if(P->exp = = q->exp) {//operations are performed only if the coefficients are equalsum = P->coef + q->Coef; if(Sum! =0) {Rear= (polynode*)malloc(sizeof(Polynode)); Rear->coef =sum; Rear->exp = p->exp; Pre->next =Rear; Pre=Rear; P= p->Next; Q= q->Next; } Else{p= p->Next; Q= q->Next; } } Else{Rear= (polynode*)malloc(sizeof(Polynode)); Rear->coef = q->Coef; Rear->exp = q->exp; Pre->next =Rear; Pre=Rear; Q= q->Next; } } //To handle a polynomial that has not been disposed of. if(P! =NULL) { while(P! =NULL) {Rear= (polynode*)malloc(sizeof(Polynode)); Rear->coef = p->Coef; Rear->exp = p->exp; Pre->next =Rear; Pre=Rear; P= p->Next; } } if(Q! =NULL) { while(Q! =NULL) {Rear= (polynode*)malloc(sizeof(Polynode)); Rear->coef = q->Coef; Rear->exp = q->exp; Pre->next =Rear; Pre=Rear; Q= q->Next; }} Rear->next =NULL;}/*in fact, the code can be added to the addition of a slight change can be achieved subtraction, but it feels like this will make the code longer, so I choose to use: polynomial subtraction is actually polynomial addition, to achieve POLYA-POLYB*/voidpolysub (polylist Polya, Polylist polyb, Polylist polyc) {polylist T; Initlist (&t); Polynode*pre, *Rear; Pre= Rear =T; //find an intermediate linked list T, to store-polyb for(Polynode *p = polyb->next; p! = NULL; p = p->next) {Rear= (polynode*)malloc(sizeof(Polynode)); Rear->coef =-1* p->Coef; Rear->exp = p->exp; Pre->next =Rear; Pre=Rear; } Pre->next =NULL; Polyadd (Polya, T, POLYC); Pre=T; while(Pre! = NULL) {//free off TRear = pre->Next; Free(pre); Pre=Rear; }}//calculates the value of a polynomial at x = xintEvaluationatx (Polylist L,intx) { intsum =0; for(Polynode *p = l->next; p! = NULL; p = p->next) {Sum+ = P->coef * POW (x, p->exp); } returnsum;}intMain () {polylist La, Lb, Lc, Ld; Initlist (&La); Initlist (&Lb); Initlist (&Lc); Initlist (&Ld); La=createlist (La); printf ("La ="); Showlist (La); Lb=createlist (LB); printf ("Lb ="); Showlist (LB); Polyadd (La, Lb, Lc); printf ("Lc ="); Showlist (LC); Polysub (La, Lb, Ld); printf ("Ld ="); Showlist (LD); intx =2; printf ("LC (%d) =%d\n", X, Evaluationatx (Lc, x)); return 0;}
View Code
The results of the operation are as follows:
List application: Unary polynomial operator.