assign7.cpp - Sheet1 Page 1...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sheet1 Page 1 /************************************************ * Name: Arria P. Owlia * UTEID: apo223 * Assignment: 7 * Purpose: To become familiar with C++ and * encapsulation *************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> struct node { int exponent double coefficient struct node *next } typedef struct node ListNode class Poly { public: ListNode *head Poly *addPoly (Poly *p2) ListNode *addPoly_LL(ListNode *poly1, ListNode *poly2) Poly *multiplyPoly (Poly *p2) double evalPoly(double xvalue) void printPoly() void freePoly() void freePoly_LL(ListNode *poly) void setHead( ListNode *poly) { head = poly void CreateList(char *str) ListNode *addNode(ListNode *first, int exp, double coeff) } ListNode *Poly::addNode(ListNode *first,int exp,double coeff)/*Adds a new node at end of Linked List with first as pointer to fir { 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 Sheet1 Page 2 } void Poly::CreateList(char *str)/*Creates Linked List containing the polynomial given in the file*/ { 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 { if(sscanf(t,"%lfx^%d",&coeff,&exp)==2) i else if(sscanf(t,"x^%d",&exp)==1) coeff = 1.0 else if((sscanf(t,"%lf%c",&coeff,&c)==2)&&(c=='x')) exp = 1 else if((sscanf(t,"%c",&c)==1)&&(c=='x')) { exp = 1 coeff = 1.0 } else if(sscanf(t,"%lf",&coeff)==1) exp = 0 else printf("ERROR!\n") coeff = coeff*sign poly = addNode(poly,exp,coeff) if((t=strtok(NULL," "))==NULL) break if(strcmp(t,"+")==0) sign = 1 else if(strcmp(t,"-")==0) sign = -1 else printf("ERROR!\n") } while((t = strtok(NULL," "))!=NULL) head = poly } Sheet1...
View Full Document

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

Page1 / 16

assign7.cpp - Sheet1 Page 1...

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