Polynomial+C+program

Rik fiddled with reading until I got something more-or-less satisfactory. Notable issue: it prints with carets ('^') but will not read them. Consistency would be better.

Also demonstrated is cramming terms into a Polynomial without normalizing or inserting the term in the right place to begin with. If you don't already have structure definitions for Term and Polynomial, you could do worse than to use these.

code rsmoody@ctec:~/poly$ poly 17.10x^13

aPoly= (thus showing one bug in printPoly???? Gimme terms: >  3.14x2  -x  -x-2 +5.5  3.14x^2 ... Putting term at 0 +   3.14x^2 > -1.00x^1 ... Putting term at 1 +   3.14x^2 +  -1.00x^1 > -1.00x^-2 ... Putting term at 2 +   3.14x^2 +  -1.00x^1 +  -1.00x^-2 >  5.50x^0 ... Putting term at 3 +   3.14x^2 +  -1.00x^1 +  -1.00x^-2 +   5.50x^0 > -x -2 -1.00x^1 ... Putting term at 4 +   3.14x^2 +  -1.00x^1 +  -1.00x^-2 +   5.50x^0 +  -1.00x^1 > -2.00x^0 ... Putting term at 5 +   3.14x^2 +  -1.00x^1 +  -1.00x^-2 +   5.50x^0 +  -1.00x^1 +  -2.00x^0 >

code

code format="c"
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 

struct Term { float coefficient; int exponent; };

void printTerm(struct Term *aTerm){ printf("%6.2fx^%i", aTerm->coefficient, aTerm->exponent); }

struct Term *newTerm(float coef, int expo) { struct Term *rslt; rslt = (struct Term *)malloc(sizeof(struct Term)); rslt->coefficient = coef; rslt->exponent = expo; return rslt; }

struct Term *readTerm { float coef=0.0; int expo; char ch, ch1; bool seenIt; putchar('>'); while (isspace(ch1 = getchar)); ungetc(ch1, stdin); seenIt = ( scanf("%f", &coef) > 0); if (!seenIt) { // printf(" seenIt = %i ch1=%c\n", seenIt, ch1 ); if (ch1 == '-') { coef = -1.0; seenIt=true;} };	if ((ch = getchar) != 'x') { if (seenIt) return(newTerm(coef, 0)); else return(newTerm(0.0, 0)); };	if (!seenIt) coef = 1.0; if (((ch = getchar) == '-') || (ch=='+') || isdigit(ch)) ungetc(ch, stdin); else return(newTerm(coef, 1)); if (scanf("%i", &expo) == 1) return(newTerm(coef, expo)); else return(newTerm(coef, 1)); }

// following is a bunch of stuff which I copied from OrderedCollection // and beat on it with a text editor:


 * 1) define PolyinitialSize 10

struct Polynomial { struct Term **theData; int firstIndex, lastIndex; int capacity; };   // "Poly" shall abbreviate  Polynomial

struct Polynomial aStaticPoly; struct Polynomial *myPoly = &aStaticPoly;

initializePoly(struct Polynomial *aPoly) { aPoly->capacity = PolyinitialSize; aPoly->theData = (struct Term **)malloc(aPoly->capacity * sizeof(struct Term *)); aPoly->firstIndex = 0; // aPoly->capacity/2; aPoly->lastIndex = (aPoly->firstIndex) - 1; // The 's are necessary  8-( }

bool isEmpty(struct Polynomial *aPoly) { return(aPoly->lastIndex < aPoly->firstIndex); }

bool hasRoomAtLast(struct Polynomial *aPoly) { return(aPoly->lastIndex < aPoly->capacity); }

bool hasRoomAtFirst(struct Polynomial *aPoly) { return(aPoly->firstIndex > 0); }

struct Term *polyAt(struct Polynomial *aPoly, int index) { return( aPoly->theData[(aPoly->firstIndex) + index]); }

growPoly(struct Polynomial *aPoly) { aPoly->capacity = (aPoly->capacity) * 2; aPoly->theData = (struct Term **)realloc(aPoly->theData, (aPoly->capacity * sizeof(struct Term *))); }

void assureRoomAtLast(struct Polynomial *aPoly) { if (hasRoomAtLast(aPoly)) return; printf("***** growing up (at last)\n"); growPoly(aPoly); }

addTerm(struct Polynomial *aPoly, struct Term *aTerm) { int ix; assureRoomAtLast(aPoly); ix = (aPoly->lastIndex = aPoly->lastIndex + 1); printf(" ... Putting term at %i\n", ix); aPoly->theData[ix] = aTerm; }

sizePoly(struct Polynomial *aPoly) { // answer how many Terms return(aPoly->lastIndex - aPoly->firstIndex + 1); }

printPoly(struct Polynomial *aPoly) { int ix; for(ix=0; ix < sizePoly(aPoly); ix++) { printf(" + "); printTerm(polyAt(aPoly, ix)); } }

main { // This clearly needs improvment... // but it gives C programmers an idea of what you can do. struct Polynomial p1, p2, p3; struct Polynomial *pp1 = &p1, *pp2 = &p2, *pp3 = &p3; struct Term t1; struct Term *pt1;

initializePoly(pp1); initializePoly(pp2); initializePoly(pp3);

pt1 = newTerm(17.1, 13); printTerm(pt1); printf("\n");

printf("\naPoly="); printPoly(pp1); printf(" (thus showing one bug in printPoly????\nGimme terms:\n");	while(1){		pt1 = readTerm;		printTerm(pt1);		addTerm(pp1,pt1);		printPoly(pp1);		printf("\n");		} } code