Polynomial - #include #include #include #include *math.h required for pow function used in EVALUTE

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>/*math.h required for pow() function used in EVALUTE polynomial*/ /*pow(a,b) gives a raised to power b*/ struct node {/*Defines struct node for each node of a Linked List*/ int exponent double coefficient struct node *next } typedef struct node ListNode ListNode *addNode(ListNode *first,int exp,double coeff)/*Adds a new node at end of Linked List with first as pointer to first nod e {/*Puts given fields exp and coeff in new node*/ ListNode *temp, *newNode newNode = (ListNode *)malloc(sizeof(ListNode)) newNode->exponent = exp newNode->coefficient = coeff newNode->next = NULL if(first==NULL) return newNode temp = first while(temp->next != NULL) temp = temp->next temp->next = newNode return first } ListNode *CreateList(char *str)/*Creates Linked List containing the polynomial given in the file*/ {/*Returns pointer to the first node of this Linked List*/ int exp, sign double coeff ListNode *poly=NULL char *t, c t = strtok(str," ") if(strcmp(t,"-")==0) { sign = -1 t = strtok(NULL," ") } else sign = 1 do { i coeff = 1.0
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sheet1 Page 2 exp = 1 { exp = 1 coeff = 1.0 } exp = 0 else printf("ERROR!\n") coeff = coeff*sign poly = addNode(poly,exp,coeff) if((t=strtok(NULL," "))==NULL)/* get next operator*/ break if(strcmp(t,"+")==0)/* sign: 1 for positive, -1 for negative*/ sign = 1 else if(strcmp(t,"-")==0) sign = -1 else printf("ERROR!\n") } while((t = strtok(NULL," "))!=NULL) return poly } ListNode *freePoly(ListNode *poly)/*frees the memory of Linked List list*/ { ListNode *i=poly, *temp while(i!=NULL) { temp = i i = i->next free(temp) } return NULL } ListNode *addPoly(ListNode *poly1, ListNode *poly2)/*Adds two polynomials given in Linked Lists poly1 and poly2*/ {/*Returns Linked List that contains the Sum*/ ListNode *i=poly1,*j=poly2, *sum = NULL double coeff { if(i->exponent > j->exponent) { sum = addNode(sum,i->exponent,i->coefficient) i = i->next }
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/30/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 21

Polynomial - #include #include #include #include *math.h required for pow function used in EVALUTE

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online