Created
December 14, 2012 15:42
-
-
Save chrisfarber/4286334 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
CS 343 Project 3 | |
Polynomials | |
Chris Farber, Winter 2010 | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include "polynomial.h" | |
#define ___ " " | |
#define __1 "%i" | |
#define __2 "%i %i" | |
#define __3 "%i " | |
#define _k$ "x^%i" | |
#define $jj "%c " | |
#define _Rs "Result of Addition: " | |
#define _Rm "Result of Subtraction: " | |
#define _Ra "Result of Multiplication: " | |
#define _$$ "Number of terms in polynomial: " | |
#define a$$ "Result of Evaluation: %i\n" | |
#define $_$ "Enter coeficient and exponent of term %i: " | |
#define $__$ "Evaluate polynomial at integer value: " | |
#define _C$ "** Data for Polynamial %i **\n" | |
#define _R$ "Polynomial %i: " | |
void death_star(Polynomial*); /* has a trash^H^H^H^H^Hpolynomial compactor */ | |
void omg_ponies(Polynomial); /* ponies collect the garbage */ | |
void displayMenu(void) { | |
puts("**** Operations on Polynomials ****"); | |
puts("1. Add"); | |
puts("2. Subtract"); | |
puts("3. Multiply"); | |
puts("4. Evaluate"); | |
puts("5. Quit"); | |
printf("Your selection: "); | |
} | |
int main(void) { | |
int a=0; | |
while(a!=5&&!feof(stdin)) { | |
displayMenu();scanf("%i",&a); | |
if(a>0&&a<4){ | |
Polynomial _,$,t=NULL;printf(_C$,1);$=create();printf(_R$,1);display($); | |
printf(_C$,2);_=create();printf(_R$,2);display(_); | |
if(a==1){t=add($,_);printf(_Rs);display(t);} | |
else if(a==2){t=subtract($,_);printf(_Rm);display(t);} | |
else if(a==3){t=multiply($,_);printf(_Ra);display(t);} | |
if(t)omg_ponies(t);omg_ponies(_);omg_ponies($); | |
}else if(a==4){ | |
Polynomial $;printf(_C$,1);$=create();printf(_R$,1);display($); | |
int w;printf($__$);scanf(__1,&w);printf(a$$,evaluate($,w));omg_ponies($); | |
} | |
puts(""); | |
} | |
puts("BYE"); | |
return 0; | |
} | |
Polynomial create() { | |
int _;printf(_$$);scanf(__1,&_);Polynomial $f=NULL,$l=NULL; | |
for(int __=0;__<_;__++){ | |
Polynomial $=calloc(1,sizeof(Term));printf($_$,__+1); | |
scanf(__2,&$->coef,&($->exp)); | |
if(!$f)$f=$;if($l){$l->next=$;$l=$;}else $l=$; | |
} | |
death_star(&$f);return $f; | |
} | |
void display(Polynomial $$) { | |
int _=0; | |
for(Polynomial $=$$;$;$=$->next) { | |
int zx=$->coef,xz=$->exp;char x$=zx>=0?'+':'-'; | |
if(!_&&!zx)printf(__3,zx);else if(zx){ | |
//if(_&&(abs(zx)>>1||xz))printf($jj,x$); | |
if(!(!_&&zx>=0))printf($jj,x$); | |
if(abs(zx)>>1)printf(__1,abs(zx)); | |
if(xz){if(xz==1)printf("x");else printf(_k$,xz);} | |
printf(___); | |
}_++; | |
}puts(""); | |
} | |
int evaluate(Polynomial $_, int v) { | |
int _=0;for(Polynomial $=$_;$;$=$->next)_+=$->coef*(int)pow(v,$->exp); | |
return _; | |
} | |
Polynomial add(Polynomial $_, Polynomial _$) { | |
Polynomial __=NULL,__$=NULL;for(Polynomial $=$_;$;$=$->next) { | |
Polynomial _=calloc(1,sizeof(Term));_->coef=$->coef;_->exp=$->exp; | |
if(!__){__=_;__$=_;}else{__$->next=_;__$=_;} | |
} | |
for(Polynomial $=_$;$;$=$->next) { | |
int l$=1; | |
for(Polynomial $$=__;$$&&l$;$$=$$->next)if($->exp==$$->exp) | |
{l$--;$$->coef+=$->coef;} | |
if(l$){Polynomial $n$=calloc(1,sizeof(Term)); | |
$n$->coef=$->coef;$n$->exp=$->exp;__$->next=$n$;__$=$n$; | |
} | |
} | |
death_star(&__);return __; | |
} | |
Polynomial subtract(Polynomial $_, Polynomial _$) { | |
Polynomial __=NULL,__$=NULL;for(Polynomial $=$_;$;$=$->next) { | |
Polynomial _=calloc(1,sizeof(Term));_->coef=$->coef;_->exp=$->exp; | |
if(!__){__=_;__$=_;}else{__$->next=_;__$=_;} | |
} | |
for(Polynomial $=_$;$;$=$->next) { | |
int l$=1; | |
for(Polynomial $$=__;$$&&l$;$$=$$->next)if($->exp==$$->exp) | |
{l$--;$$->coef-=$->coef;} | |
if(l$){Polynomial $n$=calloc(1,sizeof(Term)); | |
$n$->coef=-$->coef;$n$->exp=$->exp;__$->next=$n$;__$=$n$; | |
} | |
} | |
death_star(&__);return __; | |
} | |
Polynomial multiply(Polynomial om, Polynomial nom) { | |
Polynomial __=NULL,__$=NULL;for(Polynomial $=om;$;$=$->next) { | |
for(Polynomial $$=nom;$$;$$=$$->next) { | |
Polynomial _=calloc(1,sizeof(Term));_->coef=$->coef*$$->coef; | |
_->exp=$->exp+$$->exp; | |
if(!__){__=_;__$=_;}else{__$->next=_;__$=_;} | |
} | |
} | |
death_star(&__);return __; | |
} | |
void omg_ponies(Polynomial b$e) { | |
while(b$e){Polynomial e_=b$e->next;free(b$e);b$e=e_;} | |
} | |
void death_star(Polynomial *s$$s$) { | |
Polynomial l$=NULL,$p,r; | |
for(Polynomial V=*s$$s$;V;V=V->next){ | |
if(!V->coef)continue; | |
if(!l$){l$=calloc(1,sizeof(Term));l$->coef=V->coef;l$->exp=V->exp;} | |
else{$p=NULL; | |
for(Polynomial h=l$;h;h=h->next){ | |
if(h->exp==V->exp){h->coef+=V->coef;break;} | |
if(V->exp>h->exp){ | |
r=calloc(1,sizeof(Term));r->exp=V->exp;r->coef=V->coef; | |
r->next=h;if($p){$p->next=r;}else{l$=r;}break; | |
} | |
if(!h->next){h->next=calloc(1,sizeof(Term)); | |
h->next->coef=V->coef;h->next->exp=V->exp;break;} | |
$p=h; | |
} | |
} | |
} | |
omg_ponies(*s$$s$);*s$$s$=l$; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef POLYNOMIALHEADER | |
#define POLYNOMIALHEADER | |
struct term { | |
int coef; | |
int exp; | |
struct term *next; | |
}; | |
typedef struct term Term; | |
typedef Term *Polynomial; | |
/* | |
Function that obtains required data for a polynomial from | |
the user, constructs a polynomial (linked list of terms) | |
and returns a pointer to the first (head) term. | |
*/ | |
Polynomial create(); | |
/* | |
Function that takes two polynomials, constructs and returns | |
a new polynomial that is the result of adding polynomials p1 and p2 | |
return NULL if either p1 or p2 is NULL | |
*/ | |
Polynomial add(Polynomial p1, Polynomial p2); | |
/* | |
Function that takes two polynomials, constructs and returns | |
a new polynomial that is the result of subtracting polynomial p2 from p1 | |
return NULL if either p1 or p2 is NULL | |
*/ | |
Polynomial subtract(Polynomial p1, Polynomial p2); | |
/* | |
Function that takes two polynomials, constructs and returns | |
a new polynomial that is the result of multiplying polynomials p1 and p2 | |
return NULL if either p1 or p2 is NULL | |
*/ | |
Polynomial multiply(Polynomial p1, Polynomial p2); | |
/* | |
Function that takes a polynomial, evaluates that polynomial at | |
a given value and returns the result of evaluation | |
return 0 if p is NULL | |
*/ | |
int evaluate(Polynomial p, int value); | |
/* | |
Function that displays the polynomial to the standard output | |
in the traditional mathematical form | |
*/ | |
void display(Polynomial p); | |
#endif /* POLYNOMIALHEADER */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment