Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//
//INCLUDE files for :Polynomials
//**************************************
//
#include
#include
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
//
class Poly
{
public:
Poly(int size);
~Poly() {}
int getSize() const { return *fSize; }
int getCoeff(int index) const { return (*fSize>=index)*coeff[index]; }
void setCoeff(int index, int value) { coeff[index]=value; }
void enterDetails();
void showDetails();
Poly operator* (const Poly *);
Poly operator* (int factor);
Poly operator+ (const Poly *);
Poly operator- (const Poly *);
Poly operator- (int dec);
Poly operator+ (int inc);
Poly operator= (const Poly & rhs);
private:
int *coeff;
int *fSize; // Can be calculated, but I felt this would run faster...
};
Poly Poly::operator=(const Poly & rhs)
{
int size = rhs.getSize();
int co = rhs.getCoeff(size);
if (co==0) {
do
{
size--;
co= rhs.getCoeff(size);
} while(co==0); //reduce size to the first non-zero element
}
Poly ans(size); //Add only non-zero values to transient Poly
for (int z=0;z ans.setCoeff(z,rhs.getCoeff(z));//removing 0s }; // delete [] coeff; // Unable to delete. // .. Must find out why... Memory leak delete fSize; fSize = new int; *fSize = size; coeff = new int[size]; for (z=0;z setCoeff(z,ans.getCoeff(z)); }; return *this; }; Poly::Poly(int size) { fSize = new int; *fSize = size; coeff = new int[size]; for (int z=0;z coeff[z] = 0; }; }; void Poly::showDetails() { for (int x=getSize(); x>0;x--) { if (coeff[x]>0) { printf("%d",coeff[x]); printf("x^%d",x); printf(" + "); // Negative values are not the prettiest, but you get the gist... } } printf("%d",coeff[0]); fflush(stdin); } void Poly::enterDetails() { printf("\nPlease enter the coefficients of each term, starting with x^%d",getSize()); printf("\n\n"); int value; for (int x=getSize();x>-1;x--) { printf("x^%d",x); printf(" * "); cin >> value; coeff[x] = value; } printf("\nThank You.\n\n"); return; } Poly Poly::operator+(const Poly * inc) { int lim1 = getSize(); int lim2 = inc->getSize(); int lim3 = ((lim1>lim2)*lim1)+((lim2>lim1)*lim2)+((lim1==lim2)*lim1); //true condition evaluates to 1 Poly answer(lim3); for (int x=lim3;x>-1;x--) { answer.setCoeff(x,getCoeff(x)+inc->getCoeff(x)); } return answer; } Poly Poly::operator+(int inc) { setCoeff(0,getCoeff(0)+inc); return *this; } Poly Poly::operator-(const Poly * dec) { int lim1 = getSize(); int lim2 = dec->getSize(); int lim3 = ((lim1>lim2)*lim1)+((lim2>lim1)*lim2)+((lim1==lim2)*lim1); //true condition evaluates to 1 Poly answer(lim3); for (int x=lim3;x>-1;x--) { answer.setCoeff(x,getCoeff(x)-dec->getCoeff(x)); } return answer; } Poly Poly::operator-(int dec) { setCoeff(0,getCoeff(0)-dec); return *this; } Poly Poly::operator * (const Poly * factor) { int lim1 = getSize(); int lim2 = factor->getSize(); Poly * answer; answer = new Poly(lim1 + lim2); int x,y; for (x=lim1;x>-1;x--) { for (y=lim2;y>-1;y--) { answer->setCoeff(x+y,(answer->getCoeff(x+y)+(getCoeff(x)*factor->getCoeff(y)))); } } return *answer; } Poly Poly::operator * (int factor) { for (int x=getSize();x>-1;x--) { setCoeff(x,getCoeff(x)*factor); } return *this; } void main() { printf("What is the highest index x is raised to in your Polynomial? "); int size, choice, amount; cin >> size; Poly source(size); Poly * factor; source.enterDetails(); do{ printf ("\n\n\n"); printf ("1. Add a constant\n2. Subtract a constant\n3. Multiply by a constant\n"); printf("4. Add a Polynomial\n5. Subtract a Polynomial\n6. Multiply by a Polynomial\n7. Quit\n"); fflush(stdin); scanf("%d",&choice); switch(choice){ case 1: printf("Please enter the number to add."); cin >> amount; printf("\n"); source.showDetails(); printf(" + %d",amount); printf(" = "); source = source + amount; source.showDetails(); break; case 2: printf("Please enter the number to subtract."); cin >> amount; printf("\n"); source.showDetails(); printf(" - %d", amount); printf(" = "); source = source - amount; source.showDetails(); break; case 3: printf("Please enter the factor to Multiply by."); cin >> amount; printf("\n"); source.showDetails(); printf(" * %d",amount); printf(" = "); source = (source * amount); source.showDetails(); break; case 4: printf("What is the highest power of the Polynomial to add? "); cin >> size; factor = new Poly(size); factor->enterDetails(); printf("\n\n\n "); source.showDetails(); printf("\n + "); factor->showDetails(); printf("\n = "); source = source + factor; source.showDetails(); delete factor; break; case 5: printf("What is the highest power of the Polynomial to subtract? "); cin >> size; factor = new Poly(size); factor->enterDetails(); printf("\n\n\n "); source.showDetails(); printf("\n - "); factor->showDetails(); printf("\n = "); source = source - factor; source.showDetails(); delete factor; break; case 6: printf("What is the highest power of the Polynomial to multiply by? "); cin >> size; factor = new Poly(size); factor->enterDetails(); printf("\n\n\n "); source.showDetails(); printf("\n * "); factor->showDetails(); printf("\n = "); source = (source * factor); source.showDetails(); delete factor; break; case 7: goto end; } }while(choice!=7); end:; }