Requirement specification:
Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term 2x4 has a coefficient of 2 and an exponent of 4. Develop a full class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities:a) Overload the addition operator (+) to add two Polynomials.
b) Overload the subtraction operator (-) to subtract two Polynomials.
c) Overload the assignment operator (=) to assign one Polynomial to another.
d) Overload the multiplication operator (*) to multiply two Polynomials.
e) Overload the addition assignment operator (+=), the subtraction assignment
operator (-=), and the multiplication assignment operator (*=). plz solve by
classes
Added: Constructor to create from string and method to display as string.
Added: Method to Add terms individually.
Program
#include<iostream> #ifndef Poly2H #define Poly2H #include <stddef.h> #include <string.h> #include <stdlib.h> class Term { /* dealing with polynomials is easier to deal with on a term by term basis, so we need a term class first to manipulate terms. */ private: public: int Coeff; int Exp; char Var; char *f_str; Term(){Coeff=0;Exp=0;Var=0;}; // default constructor Term(int C, int X, char V='x') // Iitialize with coeff and exp values; variable will default to x; { Coeff=C; Exp=X; Var=V; f_str=NULL; }; Term(char * c_term); // constructor from string Term(const Term& Orig); // Copy Constructor Term operator+(const Term &rhs); Term operator-(const Term &rhs); Term operator*(const Term &rhs); Term operator*(const int &rhs) { Term T(*this); T.Coeff = Coeff*rhs; return T; } char *AsString(); }; class Polynomial { private: Term *Terms; //Array of individual Terms int Size; // Number of terms char *f_str; // String representation of Poly char Var; void Normalize(); int HiExp() const; void SetVar(); public: Polynomial(){Terms = NULL;Size=0;f_str=NULL;}; //default constructor Polynomial(char * Str); // to construct from string Polynomial(int Size) // to construct empty poly of a given size { this->Size=Size; Terms = new Term[Size]; f_str=NULL; }; Polynomial(const Polynomial &Orig); // copy constructor ~Polynomial() { if(Terms) delete[]Terms; if(f_str) delete[]f_str; }; char *AsString(); void AddTerm(Term Trm); //----------------------- Operators ------------------------------------------- Polynomial operator+(const Polynomial &rhs); Polynomial operator-(const Polynomial &rhs); Polynomial operator*(const Polynomial &rhs); Polynomial & operator=(const Polynomial &rhs); friend Polynomial operator*(const Polynomial &P, const int &rhs); friend Polynomial operator*(const Polynomial P, const Term &T); Polynomial operator+=(const Polynomial &rhs); Polynomial operator-=(const Polynomial &rhs) { *this = *this - rhs; return *this; } Polynomial operator *=(Polynomial &rhs) { *this = *this * rhs; return *this; } }; //int StrToInt(char *); //char *IntToStr(int); int StrToInt(char *Str) { return atoi(Str); } char *IntToStr(int I) { char *Tmp = new char[33]; itoa(I,Tmp, 10); int Len = strlen(Tmp); char *T2 = new char[Len+1]; strcpy(T2, Tmp); delete[] Tmp; return T2; } void StripSpaces(char *Str) { char *Tmp = Str; char *S = Str; while (*S!=' ') { S++; if (*S==0) return; } Tmp = S++; StripSpaces(S); do { *Tmp = *S; Tmp++; S++; } while(*S); S--; *S=0; }; Term::Term(char *c_term) { char* tmp = new char[strlen(c_term)+1]; strcpy(tmp, c_term); char *S = tmp; while (*S <58){ if (*S==0){ // No Exp and no Var Coeff=StrToInt(tmp); Exp=0; Var=0; return; } S++; } Var = *S; *S=0; Coeff=StrToInt(tmp); *S = Var; S++; if (*S==0) Exp= 1; else Exp = StrToInt(S); delete[] tmp; } Term::Term(const Term &Orig) { f_str = NULL; Exp = Orig.Exp; Coeff = Orig.Coeff; Var = Orig.Var; } // Addition and subtraction operators check for same variable name and exponent; // if the check fails, they return a {0,0,'\0'} term. Term Term::operator+(const Term &rhs) { Term Tmp; if (Exp != 0) // this check is to compensate for a bug I cannot track dwn; // Degree zero terms are inconsistent in setting Var for some reason , // so following check will fail, causing terms to drop out. The check in the // case of zero degree terms in meaningless anyway, so let's just bypass it for them. if ((rhs.Exp!= Exp) || (Var != rhs.Var)) return Tmp; Tmp.Coeff = Coeff + rhs.Coeff; Tmp.Exp = Exp; Var==0?Tmp.Var = rhs.Var:Tmp.Var = Var; return Tmp; }; Term Term::operator-(const Term &rhs) { Term Tmp; if (Exp != 0) // this check is to compensate for a bug I cannot track down; // Degree zero terms are inconsistent in setting Var for some reason , // so following check will fail, causing terms to drop out. The check in the // case of zero degree terms in meaningless anyway, so let's just bypass it for them. if ((rhs.Exp!= Exp) || (Var != rhs.Var)) return Tmp; Tmp.Coeff = Coeff - rhs.Coeff; Tmp.Exp=Exp; Tmp.Var=Var; return Tmp; }; Term Term::operator*(const Term &rhs) { Term Tmp; Tmp.Coeff = Coeff * rhs.Coeff; Tmp.Exp = Exp + rhs.Exp; if (Var) Tmp.Var = Var; else Tmp.Var = rhs.Var; rturn Tmp; }; char* Term::AsString() { char *CStr, *EStr, *VStr, *Result; int Len; if (f_str) delete[] f_str; // if a pointer to a prev str is valid, delete it so we don't leak it. CStr = IntToStr(Coeff); EStr = IntToStr(Exp); VStr = new char[2]; VStr[0] = Var; VStr[1] ='\0'; Len = strlen(CStr) + strlen(EStr)+1; Result = new char[Len+1]; Result[0]='\0'; if(Coeff !=1) if (Coeff == -1) { Result[0] = '-'; Result[1] = '\0'; } else strcat(Result, CStr); if (Exp!=0) { strcat(Result, VStr); if (Exp !=1) strcat(Result, EStr); } delete[]CStr; delete[]EStr; delete[]VStr; f_str = Result; return Result; } Polynomial::Polynomial(char *Str) { Var = 0; Terms = NULL; Size = 0; f_str = new char[strlen(Str)+1]; strcpy(f_str, Str); StripSpaces(f_str); char *Tmp = f_str; char *str = f_str; while (*Tmp) { if ((*Tmp == '+')||(*Tmp == '-')||(*Tmp ==0)) // Check term seperator { /* Taking this out to see if fix for '+' and '-; characters is working; seems to be for now. // if (*Tmp == '+') // // need to have seperate branches right now for pos terms. // // Change the Term Constructor to take into account for '+' chars too, // // and this can be eliminated . // { // *Tmp = 0; // AddTerm(Term(str)); // *Tmp = '+'; // Tmp++; // str = Tmp; // } // else */ { char C = *Tmp; *Tmp = 0; AddTerm(Term(str)); *Tmp = C; str = Tmp; Tmp++; }; } else Tmp++; } AddTerm(Term(str)); // char Var =0; SetVar(); Normalize(); // normalize depends on Var being initialized }; Polynomial::Polynomial(const Polynomial &Orig) { f_str=NULL; Size = Orig.Size; Var = Orig.Var; Terms = new Term[Size]; for (int i=0; i < Size; i++) Terms[i] = Orig.Terms[i]; }; void Polynomial::AddTerm(Term T) { Term *Tmp = new Term[Size+1]; if (Terms) { for (int i = 0; i < Size; i++) Tmp[i] = Terms[i]; delete[] Terms; }; Tmp[Size] = T; Terms = Tmp; Size++; Normalize(); if (Var ==0) SetVar(); } Polynomial Polynomial::operator+(const Polynomial &rhs) { int Hi; Polynomial P1, P2, Tmp; this->f_str = NULL; if (this->HiExp() > rhs.HiExp()) { P1 = *this; P2 = rhs; Hi = this->HiExp(); } else { Hi = rhs.HiExp(); P1=rhs; P2=*this; } P1.Normalize(); P2.Normalize(); // Term *Tmp = new Term[Hi+1]; // for (int i =0; i < Hi+1; i++) { // Tmp[i].Exp = i; // Tmp[i].Var = Var; // } for (int i = 0; i < P2.Size; i++) { Tmp.AddTerm(P1.Terms[i]+P2.Terms[i]); } for (int i = P2.Size; i < P1.Size; i++) Tmp.AddTerm(P1.Terms[i]); return Tmp; }; polynomial Polynomial::operator-(const Polynomial &rhs) { Polynomial T = rhs*(-1); T = *this + T; return T; }; Polynomial Polynomial::operator*(const Polynomial &rhs) { /* To multiply Polynomials, you multiply each term in the rhs by each term in this, and then add all the resulting Polynomials together. */ Polynomial *P = new Polynomial[Size]; // the number of resulting polynomials // is the number of Terms in this. for (int i =0; i < Size; i++) P[i] = rhs * Terms[i]; Polynomial T = P[0]; for (int i =1; i < Size; i++) { T.Normalize(); T += P[i]; }; delete[]P; return T; }; Polynomial &Polynomial::operator=(const Polynomial &rhs) { if (this == &rhs) return *this; Size = rhs.Size; Terms = new Term[Size]; for (int i =0; i < Size; i++) { Terms[i] = rhs.Terms[i]; } return *this; }; Polynomial Polynomial::operator+=(const Polynomial &rhs) { *this = *this+rhs; return *this; } Polynomial operator *(const Polynomial &P,const int &rhs) { Polynomial T(P); for (int i= 0; i < P.Size; i++) { T.Terms[i].Coeff = P.Terms[i].Coeff*rhs; } return T; } Polynomial operator *(const Polynomial P, const Term &rhs) { Polynomial T; for (int i = 0; i < P.Size; i++) T.AddTerm(P.Terms[i] * rhs); T.Normalize(); return T; } void Polynomial::Normalize() { int Fact = HiExp()+1; Term *Temp = new Term[Fact]; SetVar(); for (int i = 0; i < Fact; i++) { // set all terms exponents and Var Char. Temp[i].Exp = i; Temp[i].Var = Var; } for (int i=0; i < Size; i++) Temp[Terms[i].Exp]=Terms[i]; delete[]Terms; Terms=Temp; Size = Fact; } char * Polynomial::AsString() { if (f_str) delete[]f_str; char *S = new char[255]; strcpy(S, Terms[Size-1].AsString()); for (int i = Size-2; i >=0; i--) { if (Terms[i].Coeff!=0) { if (Terms[i].Coeff>=1) strcat(S, "+"); strcat(S, Terms[i].AsString()); }; }; f_str = new char[strlen(S)+1]; strcpy(f_str, S); delete[]S; return f_str; } int Polynomial::HiExp() const { int E = Terms[0].Exp; for (int i = 1; i < Size; i++) if (Terms[i].Exp>E) E = Terms[i].Exp; return E; } void Polynomial::SetVar() { int x =0; do // Initialize Var { if (x >= Size) break; Var=Terms[x++].Var; } while (Var==0); } #endif
Post A Comment:
0 comments: