1 /* Roy Keene 2 CS 2314 3 Section 04 4 Lab 05 5 08 Oct 02 6 poly.cpp 7 */ 8 9 #include <list> 10 #include <iterator> 11 #include <sstream> 12 #include <string> 13 #include <cmath> 14 #include "poly.h" 15 16 Poly::Poly(void) { return; } 17 Poly::~Poly(void) { return; } 18 void Poly::insert_term(int coeff, int exp) { 19 struct PolyTerm *newTerm=new struct PolyTerm; 20 list<struct PolyTerm *>::iterator start, stop; 21 struct PolyTerm *tmp; 22 23 newTerm->coeff=coeff; 24 newTerm->exp=exp; 25 26 start=polyterms.begin(); 27 stop=polyterms.end(); 28 while (start!=stop) { 29 tmp=*start; 30 if (tmp->exp==exp) { 31 newTerm->coeff+=tmp->coeff; 32 *start++; 33 polyterms.remove(tmp); 34 break; 35 /* continue; */ 36 } 37 start++; 38 } 39 if (newTerm->coeff==0) { delete newTerm; return; } 40 polyterms.push_back(newTerm); 41 return; 42 } 43 44 list<struct PolyTerm *> Poly::get_term(void) { 45 return(polyterms); 46 } 47 48 Poly *Poly::Mul(Poly b) { 49 Poly *ret=new Poly; 50 list<struct PolyTerm *> b_lst; 51 list<struct PolyTerm *>::iterator astart, astop, bstart, bstop; 52 struct PolyTerm *aterm, *bterm; 53 54 b_lst=b.get_term(); 55 56 astart=polyterms.begin(); 57 astop=polyterms.end(); 58 bstop=b_lst.end(); 59 while (astart!=astop) { 60 aterm=*astart; 61 bstart=b_lst.begin(); 62 while (bstart!=bstop) { 63 bterm=*bstart; 64 ret->insert_term((bterm->coeff)*(aterm->coeff), (bterm->exp)+(aterm->exp)); 65 bstart++; 66 } 67 astart++; 68 } 69 70 return(ret); 71 } 72 73 Poly *Poly::Sub(Poly b) { 74 Poly *ret=new Poly; 75 list<struct PolyTerm *> b_lst; 76 list<struct PolyTerm *>::iterator astart, astop, bstart, bstop; 77 struct PolyTerm *aterm, *bterm; 78 79 b_lst=b.get_term(); 80 81 astart=polyterms.begin(); 82 astop=polyterms.end(); 83 bstart=b_lst.begin(); 84 bstop=b_lst.end(); 85 while (astart!=astop) { 86 aterm=*astart; 87 ret->insert_term((aterm->coeff), aterm->exp); 88 astart++; 89 } 90 while (bstart!=bstop) { 91 bterm=*bstart; 92 ret->insert_term(-(bterm->coeff), bterm->exp); 93 bstart++; 94 } 95 96 return(ret); 97 } 98 99 Poly *Poly::Add(Poly b) { 100 Poly *ret=new Poly; 101 list<struct PolyTerm *> b_lst; 102 list<struct PolyTerm *>::iterator astart, astop, bstart, bstop; 103 struct PolyTerm *aterm, *bterm; 104 105 b_lst=b.get_term(); 106 107 astart=polyterms.begin(); 108 astop=polyterms.end(); 109 bstart=b_lst.begin(); 110 bstop=b_lst.end(); 111 while (astart!=astop) { 112 aterm=*astart; 113 ret->insert_term(aterm->coeff, aterm->exp); 114 astart++; 115 } 116 while (bstart!=bstop) { 117 bterm=*bstart; 118 ret->insert_term(bterm->coeff ,bterm->exp); 119 bstart++; 120 } 121 122 return(ret); 123 } 124 125 string Poly::equ(void) { 126 list<struct PolyTerm *>::iterator start, stop, start_s; 127 struct PolyTerm *tmp; 128 stringstream cs; 129 static string str; 130 131 start_s=start=polyterms.begin(); 132 stop=polyterms.end(); 133 cs << "("; 134 while (start!=stop) { 135 tmp=*start; 136 if (tmp->coeff<0) { 137 cs << " - "; 138 } else { 139 if (start!=start_s) cs << " + "; 140 } 141 if (tmp->exp==0) { 142 cs << abs(tmp->coeff); 143 } else { 144 cs << abs(tmp->coeff) << "x^" << tmp->exp; 145 } 146 start++; 147 } 148 cs << ")"; 149 str=cs.str(); 150 return(str); 151 } 152 153 double Poly::Evaluate(double x) { 154 list<struct PolyTerm *>::iterator start, stop; 155 struct PolyTerm *tmp; 156 double ret; 157 158 start=polyterms.begin(); 159 stop=polyterms.end(); 160 while (start!=stop) { 161 tmp=*start; 162 ret+=(tmp->coeff)*pow(x, tmp->exp); 163 start++; 164 } 165 return(ret); 166 } 167 168 int Poly::Evaluate(int x) { 169 list<struct PolyTerm *>::iterator start, stop; 170 struct PolyTerm *tmp; 171 int ret=0; 172 double dx=(double) x; 173 174 start=polyterms.begin(); 175 stop=polyterms.end(); 176 while (start!=stop) { 177 tmp=*start; 178 ret+=(tmp->coeff)*((int) pow(dx, tmp->exp)); 179 start++; 180 } 181 return(ret); 182 } |