//Polynomial03.cpp
//04/10/2000 12:06 AM
//////////////////////////////////////////////////////////////////////
//include files
//////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
//////////////////////////////////////////////////////////////////////
//constants
//////////////////////////////////////////////////////////////////////
const int MAX_TERMS = 10;
const int MAX_COEF = 9;
const int MAX_EXPO = 5;
//////////////////////////////////////////////////////////////////////
//CTerm class definition
// stores information about each term in a polynomial // data members
// coef coeficient of the term
// expo exponent of the term
// prev pointer to the previous term
// next pointer to the next term // member functions
// constructors // friends
// CPolynomial to have access to the private members
// << for CTerm to allow access to CTerm data members
// << for CPolynomial to allow access to CTerm data members
// CrossLinked to allow access to CTerm data members
//////////////////////////////////////////////////////////////////////
class CTerm
{
private:
double coef; //new
int expo;
CTerm *prev;
CTerm *next; public:
CTerm(void); //*constructor default
CTerm(const int &c); //*constructor with given coef
CTerm(const double &coef); //*constructor with given coef
CTerm(const double &coef, const int &expo); //*constructor with given values
CTerm(const CTerm &cn); //*copy constructor
CTerm(const char *cp);
CTerm(char ch); //*constructor to build a random node
//no destructor
CTerm & operator = (const CTerm &cn); //no overloading for assign (=) operator CTerm operator + (const CTerm &cn) const; //*overload binary addition
CTerm operator + (const double &c) const; //*overload binary addition CTerm operator - (const CTerm &cn) const; //*overload binary subtraction
CTerm operator - (const double &c) const; //*overload binary subtraction CTerm operator * (const CTerm &cn) const; //*overload binary multiplication
CTerm operator * (const double &c) const; //*overload binary multiplication CTerm operator / (const CTerm &cn) const; //*overload binary /
CTerm operator / (const double &c) const; //*overload binary / bool operator == (const CTerm &cn) const; //*overload binary ==
bool operator == (const double &c) const; //*overload binary == new if (cn==0) bool operator != (const CTerm &cn) const; //*overload binary !=
bool operator != (const double &c) const; //*overload binary != new CTerm operator -() const; //*overload unary -
CTerm operator +() const; //*overload unary + void displayMembers(void) const; //displays coef and expo members friend CTerm operator + (const double &c, const CTerm & cn); //new
friend CTerm operator - (const double &c, const CTerm & cn); //new
friend CTerm operator * (const double &c, const CTerm & cn); //new
friend CTerm operator / (const double &c, const CTerm & cn); //new friend bool operator == (const double &c, const CTerm & cn); //new
friend bool operator != (const double &c, const CTerm & cn); //new friend ostream & operator << (ostream &os, const CTerm &p); //*overload << for output
friend istream & operator >> (istream &os, CTerm &p); //*overload >> for input friend void swap(CTerm &n1, CTerm &n2); //swaps only coef and expo friend class CPolynomial;
friend ostream & operator << (ostream &os, const CPolynomial &poly);
friend istream & operator >> (istream &os, CPolynomial &poly); friend CPolynomial & operator * (const CTerm &cn, const CPolynomial &poly);
friend bool areCrossLinked(const CPolynomial &poly1, const CPolynomial &poly2); };
//////////////////////////////////////////////////////////////////////
//displayMembers
//////////////////////////////////////////////////////////////////////
void CTerm::displayMembers(void) const
{
cout << " coef: " << coef << endl;
cout << " expo: " << expo << endl;
}
//////////////////////////////////////////////////////////////////////
//string constructor for CTerm
//Initializes a node with value from a string
//////////////////////////////////////////////////////////////////////
CTerm::CTerm(const char *ct)
{
prev = next = NULL;
coef = 1;
expo =1;
}
/*------------------------------------------------------------------*/
//testConstructorStringCTerm
/*------------------------------------------------------------------*/
void testConstructorStringCTerm(void)
{
cout << "testConstructorStringCTerm\n";
cout << "--------------------------\n"; char ch; char *test[]={"0", "1", "x", "2*x", "x^3", "4*x^4", "x^5", "-1", "-x", "-2*x", "-x^3", "-4*x^4", "-x^5"};
for (int i=0; i<sizeof(test)/sizeof(test[0]); i++)
{
cout << "input: " << test[i] << endl;
CTerm term(test[i]);
term.displayMembers();
cout << " term:" << term << endl;
cout << "===================\n";
} do
{
int myCase=rand()%5+1;
char testString[80];
double testCoef = double(rand())/(1+rand());
if (rand()%2 ==1) testCoef=-testCoef;
int testExpo = rand()%(MAX_EXPO+1); cout << "testCoef: " << testCoef << endl;
cout << "testExpo: " << testExpo << endl;
cout << "myCase : " << myCase << endl; switch (myCase)
{
case 1: sprintf(testString, "%f",testCoef); break;
case 2: sprintf(testString, "%s", "x"); break;
case 3: sprintf(testString, "%s^%d", "x", testExpo); break;
case 4: sprintf(testString, "%f*%s", testCoef, "x"); break;
case 5: sprintf(testString, "%f*%s^%d", testCoef, "x", testExpo); break;
default:;
}
CTerm ct(testString);
cout << "input: " << testString << endl;
cout << ct << endl << endl; cin >> ch;
}
while (ch != '0'); char testString[80];
do
{
cout << "Input string:";
cin >> testString;
cout << testString << endl;
if (strcmp(testString,"end")==0) break;
{
CTerm ct(testString);
cout << ct << endl;
}
}
while (strcmp(testString,"end")!=0); }
//////////////////////////////////////////////////////////////////////
//default constructor for CTerm
//Initializes a node with zero values
//////////////////////////////////////////////////////////////////////
CTerm::CTerm(void)
{
coef = expo = 0;
prev = next = NULL;
}
/*------------------------------------------------------------------*/
//testConstructorDefaultCTerm
/*------------------------------------------------------------------*/
void testConstructorDefaultCTerm(void)
{
cout << "testConstructorDefaultCTerm\n";
cout << "---------------------------\n"; char ch; do
{
CTerm n1; cout << n1 << endl; cin >> ch;
}
while (ch != '0'); }
//////////////////////////////////////////////////////////////////////
//another constructor for CTerm with values
//Initializes a node for given coef and expo
//////////////////////////////////////////////////////////////////////
CTerm::CTerm(const double &coef, const int &expo)
{
this->coef = coef;
this->expo = expo;
prev = next = NULL;
}
/*------------------------------------------------------------------*/
/*testConstructorAnotherCTerm */
/*------------------------------------------------------------------*/
/* CTerm n1(coef, expo); */
/* CTerm n2(coef); */
/* CTerm n3 = coef; */ void testConstructorAnotherCTerm(void)
{
cout << "testConstructorAnotherCTerm\n";
cout << "---------------------------\n"; char ch;
do
{
double coef = rand()%MAX_COEF;
int expo = rand()%MAX_EXPO;
CTerm n1(coef, expo); cout << "coef = " << coef << ", expo = " << expo << endl; cout << " after CTerm n1(coef, expo);\n";
cout << "n1 = " << n1 << endl; CTerm n2(coef);
cout << " after CTerm n2(coef);\n";
cout << "n2 = " << n2 << endl; CTerm n3 = coef;
cout << " after CTerm n3 = coef;\n";
cout << "n3 = " << n3 << endl; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//copy constructor for CTerm
//////////////////////////////////////////////////////////////////////
CTerm::CTerm(const CTerm &cn)
{
coef = cn.coef;
expo = cn.expo;
prev = NULL;
next = NULL;
//prev = cn.prev;
//next = cn.next;
}
/*------------------------------------------------------------------*/
/*testConstructorCopyCTerm */
/*------------------------------------------------------------------*/
void testConstructorCopyCTerm(void)
{
cout << "testConstructorCopyCTerm\n";
cout << "------------------------\n"; char ch;
do
{
CTerm n1('r');
CTerm n2(n1); cout << n1 << endl;
cout << n2 << endl; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//constructor for CTerm when coef is given
//////////////////////////////////////////////////////////////////////
CTerm::CTerm(const double &coef)
{
this->coef = coef;
this->expo = 0;
prev = next = NULL;
}
//////////////////////////////////////////////////////////////////////
//constructor for CTerm when coef is given
//////////////////////////////////////////////////////////////////////
CTerm::CTerm(const int &coef)
{
this->coef = coef;
this->expo = 0;
prev = next = NULL;
}
/*------------------------------------------------------------------*/
/*testConstructorCoefCTerm */
/*------------------------------------------------------------------*/
void testConstructorCoefCTerm(void)
{
cout << "testConstructorCoefCTerm\n";
cout << "------------------------\n"; char ch;
do
{
int a = rand()%MAX_COEF;
float b = float((rand()%MAX_COEF)*2.0f/(rand()%MAX_COEF+1));
double c = (rand()%MAX_COEF)*2.0f/(rand()%MAX_COEF+1);
CTerm n1(a);
cout << "n1 = " << n1 << endl; CTerm n2(b);
cout << "n2 = " << n2 << endl; CTerm n3(c);
cout << "n3 = " << n3 << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//constructor for CTerm to build a random node
//////////////////////////////////////////////////////////////////////
CTerm::CTerm(char ch)
{
if ('r' == ch)
{
coef = rand()%(MAX_COEF+1);
if ((rand()%2) == 1)
coef = -coef; expo = rand()%(MAX_EXPO+1);
prev = next = NULL;
}
else if ('u' == ch)
{
coef = 1;
expo = 0;
prev = next = NULL;
}
else
{
coef = 0;
expo = 0;
prev = next = NULL;
}
}
/*------------------------------------------------------------------*/
//testConstructorRandomCTerm
/*------------------------------------------------------------------*/
void testConstructorRandomCTerm(void)
{
cout << "testConstructorRandomCTerm\n";
cout << "--------------------------\n"; char ch;
do
{
if ((rand()%2)==0)
{
CTerm n1('r');
cout << n1 << endl;
}
else
{
CTerm n1('u');
cout << n1 << endl;
}
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//CTerm & CTerm::operator = (const CTerm &cn)
//////////////////////////////////////////////////////////////////////
CTerm & CTerm::operator = (const CTerm &cn)
{
coef = cn.coef;
expo = cn.expo;
return *this;
}
/*------------------------------------------------------------------*/
//testOperatorAssignCTerm
/*------------------------------------------------------------------*/
void testOperatorAssignCTerm(void)
{
cout << "testOperatorAssignCTerm\n";
cout << "-----------------------\n"; char ch;
do
{
CTerm n1('r'), n2('r'); cout << n1 << endl;
cout << n2 << endl;
n1 = n2; cout << "After n1 = n2;\n"; cout << n1 << endl;
cout << n2 << endl; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//member addition operator + overloaded for CTerm
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator + (const CTerm &cn) const
{
if (0 == coef)
return cn;
if (0 == cn.coef)
return *this; CTerm temp;
if (expo == cn.expo)
{
temp.coef = coef + cn.coef;
temp.expo = expo;
if (0 == temp.coef)
temp.expo = 0;
}
else
cout << "Not compatible to add\n";
return temp;
}
//////////////////////////////////////////////////////////////////////
//member addition operator + overloaded for CTerm and double
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator + (const double &c) const
{
if (0 == coef)
return c;
if (0 == c)
return *this; CTerm temp;
if (expo == 0)
{
temp.coef = coef + c;
if (0 == temp.coef)
temp.expo = 0;
}
else
cout << "Not compatible to add\n";
return temp;
}
/*
//////////////////////////////////////////////////////////////////////
//friend addition operator + overloaded for CTerm and double
//////////////////////////////////////////////////////////////////////
CTerm & operator + (const CTerm & cn, const double &c)
{
CTerm *temp=new CTerm(cn);
if (cn.expo != 0)
{
cout << "Not compatible to add\n";
return *temp;
} temp->coef=temp->coef+c;
return *temp;
}
*/ //////////////////////////////////////////////////////////////////////
//friend addition operator + overloaded for double and CTerm
//////////////////////////////////////////////////////////////////////
CTerm operator + (const double &c, const CTerm & cn)
{
CTerm temp(cn);
if (cn.expo != 0)
{
cout << "Not compatible to add\n";
return temp;
} temp.coef = temp.coef + c;
return temp;
}
/*------------------------------------------------------------------*/
//testOperatorAdditionCTerm
/*------------------------------------------------------------------*/
void testOperatorAdditionCTerm(void)
{
cout << "testOperatorAdditionCTerm\n";
cout << "-------------------------\n"; char ch;
do
{
CTerm n1('r');
CTerm n2('r');
CTerm n3('r'); cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl; n3 = n1+n2; cout << "After n3 = n1+n2;" << endl; cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl; n3 = n1+2; cout << "After n3 = n1+2;" << endl; cout << "n3 = " << n3 << endl; n3 = 2+n1; cout << "After n3 = 2+n1;" << endl; cout << "n3 = " << n3 << endl; cout << endl; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//member subtraction operator - overloaded for CTerm
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator - (const CTerm &cn) const
{
if (0 == coef)
return -cn;
if (0 == cn.coef)
return *this;
CTerm temp; if (expo == cn.expo)
{
temp.coef = coef - cn.coef;
temp.expo = expo;
if (0 == temp.coef)
temp.expo = 0;
}
else
cout << "Not compatible to subtract\n";
return temp;
}
//////////////////////////////////////////////////////////////////////
//friend subtraction operator - overloaded for CTerm and double
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator - (const double &c) const
{
if (0 == coef)
return -c;
if (0 == c)
return *this; CTerm temp;
if (expo == 0)
{
temp.coef = coef - c;
if (0 == temp.coef)
temp.expo = 0;
}
else
cout << "Not compatible to subtract\n";
return temp;
}
//////////////////////////////////////////////////////////////////////
//friend subtract operator - overloaded for double and CTerm
//////////////////////////////////////////////////////////////////////
CTerm operator - (const double &c, const CTerm & cn)
{
CTerm temp(cn);
if (cn.expo != 0)
{
cout << "Not compatible to subtract\n";
return temp;
} temp.coef=c - temp.coef;
return temp;
}
/*------------------------------------------------------------------*/
//testOperatorSubtractionCTerm
/*------------------------------------------------------------------*/
void testOperatorSubtractionCTerm(void)
{
cout << "testOperatorSubtractionCTerm\n";
cout << "----------------------------\n"; char ch;
do
{
CTerm n1('r');
CTerm n2('r');
CTerm n3('r'); cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl; n3 = n1-n2; cout << "After n3 = n1-n2;" << endl; cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl;
cout << endl; n3 = n1 - 2; cout << "After n3 = n1-2;" << endl; cout << "n3 = " << n3 << endl; n3 = 2 - n1; cout << "After n3 = 2-n1;" << endl; cout << "n3 = " << n3 << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//member multiplication operator * overloaded for CTerm
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator * (const CTerm &cn) const
{
if (0 == coef)
return coef;
else if (0 == cn.coef)
return cn.coef; CTerm tempNode; tempNode.coef = coef * cn.coef;
tempNode.expo = expo + cn.expo; return tempNode;
}
//////////////////////////////////////////////////////////////////////
//member multiplication operator * overloaded for CTerm and double
//////////////////////////////////////////////////////////////////////
CTerm CTerm :: operator * (const double &c) const
{
if (0 == coef)
return coef;
else if (0 == c)
return c; CTerm tempNode; tempNode.coef = coef * c;
tempNode.expo = expo; return tempNode;
}
//////////////////////////////////////////////////////////////////////
//friend multiplication operator * overloaded for double and CTerm
//////////////////////////////////////////////////////////////////////
CTerm operator * (const double &c, const CTerm & cn)
{
CTerm temp(cn);
temp.coef=temp.coef*c;
return temp;
}
/*------------------------------------------------------------------*/
//testOperatorMultiplicationCTerm
/*------------------------------------------------------------------*/
void testOperatorMultiplicationCTerm(void)
{
cout << "testOperatorMultiplicationCTerm\n";
cout << "-------------------------------\n"; char ch;
do
{
CTerm n1('r');
CTerm n2('r');
CTerm n3('r'); cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl; n3 = n1*n2; cout << "After n3 = n1*n2;" << endl; cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl;
cout << endl; n3 = n1*2; cout << "After n3 = n1*2;" << endl; cout << "n3 = " << n3 << endl; n3 = 2*n1; cout << "After n3 = 2*n1;" << endl; cout << "n3 = " << n3 << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//member division operator / overloaded for CTerm
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator / (const CTerm &cn) const
{
if (0 == coef)
return coef;
if (0 == cn.coef)
{
cout << "Division by zero\n";
return cn.coef;
}
CTerm tempNode; if (expo < cn.expo)
{
cout << " / not possible will result in -ve expo\n";
return double(0);
}
tempNode.coef = coef / cn.coef;
tempNode.expo = expo - cn.expo; // what if (tempNode.coef < 0)
if (0 == tempNode.coef) // could be 0 because of integer division
tempNode.expo = 0;
return tempNode;
}
//////////////////////////////////////////////////////////////////////
//member division operator / overloaded for CTerm and double
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator / (const double &c) const
{
if (0 == coef)
return coef;
if (0 == c)
{
cout << "Division by zero\n";
return c;
} CTerm temp;
temp = *this;
temp.coef=temp.coef/c;
return temp;
}
//////////////////////////////////////////////////////////////////////
//friend division operator / overloaded for double and CTerm
//////////////////////////////////////////////////////////////////////
CTerm operator / (const double &c, const CTerm & cn)
{
CTerm temp; if (0 == c)
return temp;
if (cn.expo != 0)
{
cout << " / not possible will result in -ve expo\n";
return temp;
} if (0 == cn.coef)
{
cout << "Division by zero\n";
return temp;
} temp=cn;
temp.coef=c/temp.coef;
return temp;
}
/*------------------------------------------------------------------*/
//testOperatorDivisionCTerm
/*------------------------------------------------------------------*/
void testOperatorDivisionCTerm(void)
{
cout << "testOperatorDivisionCTerm\n";
cout << "-------------------------\n"; char ch;
do
{
CTerm n1('r');
CTerm n2('r');
CTerm n3('r'); cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl; n3 = n1/n2; cout << "After n3 = n1/n2;" << endl; cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
cout << "n3 = " << n3 << endl;
cout << endl; n3 = n1/2; cout << "After n3 = n1/2;" << endl; cout << "n3 = " << n3 << endl; n3 = 2/n1; cout << "After n3 = 2/n1;" << endl; cout << "n3 = " << n3 << endl;
cin >> ch;
}
while (ch != '0');
}
//**stop
///////////////////////////////////////////////////////////////////////
//member equal operator == overloaded for CTerm
//////////////////////////////////////////////////////////////////////
bool CTerm::operator == (const CTerm &cn) const
{
return ((expo == cn.expo) && (coef == cn.coef));
}
///////////////////////////////////////////////////////////////////////
//member equal operator == overloaded for CTerm and double
//////////////////////////////////////////////////////////////////////
bool CTerm::operator == (const double &c) const
{
return ((0 == expo) && (coef == c));
}
///////////////////////////////////////////////////////////////////////
//friend equal operator == overloaded for double and CTerm
//////////////////////////////////////////////////////////////////////
bool operator == (const double &c, const CTerm & cn)
{
return (cn == c);
} /*------------------------------------------------------------------*/
//testOperatorEqualCTerm
/*------------------------------------------------------------------*/
void testOperatorEqualCTerm(void)
{
cout << "testOperatorEqualCTerm\n";
cout << "----------------------\n"; char ch;
do
{
CTerm cn1('r'), cn2('r'), cn3(cn1); cout << cn1 << endl;
cout << cn2 << endl;
cout << cn3 << endl;
if (cn1 == cn2)
cout << cn1 << " is equal to " << cn2 << endl;
else
cout << cn1 << " is not equal to " << cn2 << endl; if (cn1 == cn3)
cout << cn1 << " is equal to " << cn3 << endl;
else
cout << cn1 << " is not equal to " << cn3 << endl; double c = rand()%MAX_COEF;
CTerm cn4(c, 0); cout << "cn4 = " << cn4 << endl;
if (cn4 == c)
cout << cn4 << " is equal to " << c << endl;
else
cout << cn4 << " is not equal to " << c << endl; if (cn1 == c)
cout << cn1 << " is equal to " << c << endl;
else
cout << cn1 << " is not equal to " << c << endl; if (c == cn4)
cout << c << " is equal to " << cn4 << endl;
else
cout << c << " is not equal to " << cn4 << endl; if (c == cn1)
cout << c << " is equal to " << cn1 << endl;
else
cout << c << " is not equal to " << cn1 << endl;
cin >> ch;
}
while (ch != '0'); }
//////////////////////////////////////////////////////////////////////
//member equal operator != overloaded for CTerm
//////////////////////////////////////////////////////////////////////
bool CTerm::operator != (const CTerm &cn) const
{
return !(*this == cn);
}
//////////////////////////////////////////////////////////////////////
//member equal operator != overloaded for CTerm and double
//////////////////////////////////////////////////////////////////////
bool CTerm::operator != (const double &c) const
{
return !(*this == c);
}
///////////////////////////////////////////////////////////////////////
//friend not equal operator != overloaded for double and CTerm
//////////////////////////////////////////////////////////////////////
bool operator != (const double &c, const CTerm & cn)
{
return !(cn == c);
} /*------------------------------------------------------------------*/
//testOperatorNotEqualCTerm
/*------------------------------------------------------------------*/
void testOperatorNotEqualCTerm(void)
{
cout << "testOperatorNotEqualCTerm\n";
cout << "-------------------------\n"; char ch;
do
{
CTerm cn1('r'), cn2('r'), cn3(cn1); cout << cn1 << endl;
cout << cn2 << endl;
cout << cn3 << endl;
if (cn1 != cn2)
cout << cn1 << " is not equal to " << cn2 << endl;
else
cout << cn1 << " is equal to " << cn2 << endl; if (cn1 != cn3)
cout << cn1 << " is not equal to " << cn3 << endl;
else
cout << cn1 << " is equal to " << cn3 << endl; double c = rand()%MAX_COEF;
CTerm cn4(c, 0); cout << "cn4 = " << cn4 << endl;
if (cn4 != c)
cout << cn4 << " is not equal to " << c << endl;
else
cout << cn4 << " is equal to " << c << endl; if (cn1 != c)
cout << cn1 << " is not equal to " << c << endl;
else
cout << cn1 << " is equal to " << c << endl; if (c != cn4)
cout << c << " is not equal to " << cn4 << endl;
else
cout << c << " is equal to " << cn4 << endl; if (c != cn1)
cout << c << " is not equal to " << cn1 << endl;
else
cout << c << " is equal to " << cn1 << endl;
cin >> ch;
}
while (ch != '0'); }
//////////////////////////////////////////////////////////////////////
//OperatorUnaryMinus
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator -() const
{
CTerm tempNode = *this;
tempNode.coef = -tempNode.coef;
return tempNode;
}
/*------------------------------------------------------------------*/
//testOperatorUnaryMisub
/*------------------------------------------------------------------*/
void testOperatorUnaryMinusCTerm(void)
{
cout << "testOperatorUnaryMinusCTerm\n";
cout << "---------------------------\n"; char ch;
do
{
CTerm cn1('r'), cn2('r'); cout << cn1 << endl;
cout << cn2 << endl; cn1 = -cn2;
cout << "After cn1 = -cn2;\n"; cout << cn1 << endl;
cout << cn2 << endl; cin >> ch;
}
while (ch != '0'); }
//////////////////////////////////////////////////////////////////////
//OperatorUnaryPlus
//////////////////////////////////////////////////////////////////////
CTerm CTerm::operator +() const
{
return *this;
}
/*------------------------------------------------------------------*/
//testOperatorUnaryPlus
/*------------------------------------------------------------------*/
void testOperatorUnaryPlusCTerm(void)
{
cout << "testOperatorUnaryPlusCTerm\n";
cout << "--------------------------\n"; char ch;
do
{
CTerm cn1('r'), cn2('r'); cout << cn1 << endl;
cout << cn2 << endl; cn1 = +cn2;
cout << "After cn1 = +cn2;\n"; cout << cn1 << endl;
cout << cn2 << endl; cin >> ch;
}
while (ch != '0'); }
//////////////////////////////////////////////////////////////////////
//insertion operator << for output overloaded for CTerm
//////////////////////////////////////////////////////////////////////
ostream & operator << (ostream &os, const CTerm &cn)
{
if (0 == cn.coef)
os << '0';
else
{
if (cn.coef > 0)
os << '+'; os << cn.coef;
os << "*x^" << cn.expo;
}
return os;
}
/*
csis 250, project #5 (total points 18)
date assigned 3/27/2000, date due 4/3/2000 at 10:00 AM
for questions contact kchandra@pittstate.edu
modifications to display function
i have identified the following distinct cases: coef expo output output case# conditions
stand smart
---- ---- ------ ------- ---- -----------------
0 0 0*x^0 0 1 coef==0
0 1 0*x^1 0
0 2 0*x^2 0 1 0 1*x^0 +1 2 coef<>0 and expo==0
2 0 2*x^0 +2
-3 0 -3*x^0 -3 1 1 1*x^1 +x 3 coef==1 or coef==-1 and expo==1
-1 1 -1*x^1 -x 1 2 1*x^2 +x^2 4 coef==1 or coef=-1 and expo>1
1 3 1*x^3 +x^3
-1 4 -1*x^4 -x^4 2 1 2*x^1 +2*x 5 coef>1 or coef<-1 and expo==1
3 1 3*x^1 +3*x
-4 1 -4*x^1 -4*x 2 2 2*x^2 +2*x^2 6 coef>1 or coef<-1 and expo>1
2 3 2*x^3 +2*x^3
-2 4 -2*x^4 -2*x^4 run enough iterations to cover each case
for each pair of coef and expo values display the following:
values of coef and expo
standard output
case #
smart output
a blank line OR ********* do not submit the entire source code
submit only your contribution
submit the following:
printed source code of displaySmart function (6 points)
printed source code of testDisplaySmart function (6 points)
printed sample runs (6 points) void CTerm::displaySmart(void)
{
cout << "coef = " << coef << endl;
cout << "expo = " << expo << endl;
cout << *this << endl; if (0 == coef) //case #1
{
cout << "Case #1\n";
cout << coef << endl << endl;
return;
}
*/ /*------------------------------------------------------------------*/
//testOperatorInsertionCTerm
/*------------------------------------------------------------------*/
void testOperatorInsertionCTerm(void)
{
cout << "testOperatorInsertionCTerm\n";
cout << "--------------------------\n"; char ch;
do
{
CTerm cn('r'); cout << cn << endl;
cin >> ch;
}
while (ch != '0'); }
//////////////////////////////////////////////////////////////////////
//extraction operator >> for input overloaded for CTerm
//////////////////////////////////////////////////////////////////////
istream & operator >> (istream &is, CTerm &cn)
{
cout << "coefficient: ";
is >> cn.coef;
cout << "exponent : ";
is >> cn.expo;
cn.prev = cn.next = NULL;
return is;
}
/*------------------------------------------------------------------*/
//testOperatorExtractionCTerm
/*------------------------------------------------------------------*/
void testOperatorExtractionCTerm(void)
{
cout << "testOperatorExtractionCTerm\n";
cout << "---------------------------\n"; char ch;
do
{
CTerm cn; cin >> cn; cout << cn << endl;
cin >> ch;
}
while (ch != '0'); }
//////////////////////////////////////////////////////////////////////
//swap
// Swaps the contents of nodes n1 and n2 except the pointer fields
//////////////////////////////////////////////////////////////////////
void swap(CTerm &n1, CTerm &n2)
{
if (n1 != n2)
{
CTerm tNode = n1;
n1 = n2;
n2 = tNode;
}
/*
{
double tCoef = n1.coef;
n1.coef = n2.coef;
n2.coef = tCoef;
int tExpo = n1.expo;
n1.expo = n2.expo;
n2.expo = tExpo;
}
*/
}
/*------------------------------------------------------------------*/
//testSwap
/*------------------------------------------------------------------*/
void testSwap(void)
{
cout << "testSwap\n";
cout << "========\n"; char ch;
do
{
CTerm x('r'), y('r');
cout << "x = " << x << endl;
cout << "y = " << y << endl; swap(x, y);
cout << "After swap(x, y);\n"; cout << "x = " << x << endl;
cout << "y = " << y << endl;
cin >> ch; }
while (ch != '0');
}
//stop 4/3/2000
/////////////////////////////////////////////////////////////////////
//CPolynomial class definition
// stores a polynomial represented by a doubly-linked list of CTerm
// data members
// count number of terms in a polynomial
// head points to the first term in the doubly-linked list
// tail points to the last term in the doubly-linked list // function members
// CTerm * CPolynomial::addressOfNodeAtPos(int pos) const
// Returns the address of a node at specified position (0, 1, 2...
// CPolynomial::CPolynomial (CPolynomial &poly)
// Copy constructor //////////////////////////////////////////////////////////////////////
class CPolynomial
{
private:
int count;
CTerm * head;
CTerm * tail;
public:
CPolynomial(void);
CPolynomial(const int &c);
CPolynomial(const double &c);
CPolynomial(const CTerm &cn); ///
CPolynomial(const CPolynomial &poly);
CPolynomial(const char *cp);
CPolynomial(char ch);
~CPolynomial(void); void insertAtTail(double coef, int expo);
void insertAtTail(const CTerm &cn);
void insertAtHead(double coef, int expo);
void insertAtHead(const CTerm &cn); void populate(int n);
void deleteAll(void);
void deleteAt(CTerm *p);
void displayNodes(void) const;
void displayReverse(void) const;
int getCount(void) const;
int getDegree(void) const;//new
bool isOK(void) const;
CTerm * addressOfNodeAtPos(int pos) const; CPolynomial & operator + (const CPolynomial &poly);
CPolynomial & operator - (const CPolynomial &poly);
CPolynomial & operator = (const CPolynomial &poly);
CPolynomial & operator * (const CPolynomial &poly);
CPolynomial & operator / (const CPolynomial &poly);//new
CPolynomial & operator -() const;
CPolynomial & operator +() const; void operator += (const CPolynomial &poly);
void operator -= (const CPolynomial &poly);
void operator *= (const CPolynomial &poly);
void operator /= (const CPolynomial &poly); bool operator == (const CPolynomial &poly) const;
bool operator != (const CPolynomial &poly) const; void simplify(void);
void sortDesc(void); friend CPolynomial & operator * (const CTerm &cn, const CPolynomial &poly);
friend CPolynomial & operator + (const CTerm &cn, const CPolynomial &poly);
friend CPolynomial & operator - (const CTerm &cn, const CPolynomial &poly);
friend bool operator == (const CTerm &cn, const CPolynomial &poly);
friend bool operator != (const CTerm &cn, const CPolynomial &poly); friend ostream & operator << (ostream &os, const CPolynomial &poly);
friend istream & operator >> (istream &os, CPolynomial &poly); friend bool areCrossLinked(const CPolynomial &poly1, const CPolynomial &poly2);
};
//////////////////////////////////////////////////////////////////////
//string constructor for CPolynomial
//Initializes a polynomial with value from a string
//Dummy version
//////////////////////////////////////////////////////////////////////
CPolynomial::CPolynomial(const char *cp)
{
head = tail = NULL;
count = 0;
CTerm ct(cp);
cout << cp << endl;
cout << ct << endl;
insertAtTail(ct);
/*
char co[10];
char ex[10];
co[0] = cp[0];
co[1] = cp[1];
co[2] = '\0';
ex[0] = cp[5];
ex[1] = '\0';
coef =atoi(co);
expo =atoi(ex);
*/
};
/*------------------------------------------------------------------*/
//testConstructorStringCPolynomial
/*------------------------------------------------------------------*/
void testConstructorStringCPolynomial(void)
{
cout << "testConstructorStringCPolynomial\n";
cout << "--------------------------------\n"; char ch; do
{
CPolynomial p1("16*x^3"); cout << "p1 = " << p1 << endl; CPolynomial p2 ="16*x^3"; cout << "p2 = " << p2 << endl; CPolynomial p3;
p3 ="16*x^3";
p3 = 2*p3;
p3 = p3*p1;
p3 = p3 + p1;
cout << "p3 = " << p3 << endl;
cin >> ch;
}
while (ch != '0'); }
//////////////////////////////////////////////////////////////////////
//CPolynomial
// Default constructor
//////////////////////////////////////////////////////////////////////
CPolynomial::CPolynomial(void)
{
head = tail = NULL;
count = 0;
}
/*------------------------------------------------------------------*/
//testConstructorDefaultCPolynomial
/*------------------------------------------------------------------*/
void testConstructorDefaultCPolynomial(void)
{
cout << "testConstructorDefaultCPolynomial\n";
cout << "=================================\n"; char ch;
do
{
CPolynomial poly;
cout << poly << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//insertAtTail
//Inserts a new node with given coeficient and exponent-of-x
//resulting polynomial may not be simplified
//////////////////////////////////////////////////////////////////////
void CPolynomial::insertAtTail(double coef, int expo)
{
CTerm *p = new CTerm(coef, expo); if (NULL == p)
{
cout << "out of memory\n";
return;
} if (NULL == head)
{
head = tail = p;
assert(NULL == p->prev);
assert(NULL == p->next);
}
else
{
tail->next = p;
p->prev = tail;
assert(NULL == p->next);
tail = p;
}
count++;
}
/*------------------------------------------------------------------*/
//testInsertAtTailCPolynomial
/*------------------------------------------------------------------*/
void testInsertAtTailCPolynomial(void)
{
cout << "testInsertAtTailCPolynomial\n";
cout << "===========================\n"; char ch;
double coeficient;
int expo;
CPolynomial poly; do
{
coeficient = rand()%MAX_COEF;
expo = rand()%MAX_EXPO;
poly.insertAtTail(coeficient, expo);
cout << "After inserting " << coeficient << ' ' << expo << endl;
cout << poly << endl; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//insertAtTail
//Inserts a new node with given node
//resulting polynomial may not be simplified
//////////////////////////////////////////////////////////////////////
void CPolynomial::insertAtTail(const CTerm &cn)
{
CTerm *p = new CTerm(cn);
// p->prev = NULL;
// p->next = NULL; if (NULL == p)
{
cout << "out of memory\n";
return;
} if (NULL == head)
{
head = tail = p;
assert(NULL == p->prev);
assert(NULL == p->next);
}
else
{
tail->next = p;
p->prev = tail;
assert(NULL == p->next);
tail = p;
}
count++;
}
/*------------------------------------------------------------------*/
//testInsertAtTail2CPolynomial
/*------------------------------------------------------------------*/
void testInsertAtTail2CPolynomial(void)
{
cout << "testInsertAtTail2CPolynomial\n";
cout << "============================\n"; char ch;
CPolynomial poly; do
{
{
CTerm cn('r');
poly.insertAtTail(cn);
cout << "After inserting " << cn << endl;
cout << poly << endl;
assert(poly.isOK());
} cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//insertAtHead
//Inserts a new node with given coeficient and exponent-of-x
//resulting polynomial may not be simplified
//////////////////////////////////////////////////////////////////////
void CPolynomial::insertAtHead(double coef, int expo)
{
CTerm *p = new CTerm(coef, expo); if (NULL == p)
{
cout << "out of memory\n";
return;
} if (NULL == head)
{
head = tail = p;
assert(NULL == p->prev);
assert(NULL == p->next);
}
else
{
head->prev = p;
p->next = head;
head = p;
assert(NULL == p->prev);
}
count++;
}
/*------------------------------------------------------------------*/
//testInsertAtHeadCPolynomial
/*------------------------------------------------------------------*/
void testInsertAtHeadCPolynomial(void)
{
cout << "testInsertAtHeadCPolynomial\n";
cout << "===========================\n"; char ch;
double coeficient;
int expo;
CPolynomial poly; do
{
coeficient = rand()%MAX_COEF;
expo = rand()%MAX_EXPO;
poly.insertAtHead(coeficient, expo);
cout << "After inserting " << coeficient << ' ' << expo << endl;
cout << poly << endl; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//insertAtHead
//Inserts a new node with given node
//resulting polynomial may not be simplified
//////////////////////////////////////////////////////////////////////
void CPolynomial::insertAtHead(const CTerm &cn)
{
CTerm *p = new CTerm(cn); if (NULL == p)
{
cout << "out of memory\n";
return;
} if (NULL == head)
{
head = tail = p;
assert(NULL == p->prev);
assert(NULL == p->next);
}
else
{
head->prev = p;
p->next = head;
head = p;
assert(NULL == p->prev);
}
count++;
}
/*------------------------------------------------------------------*/
//testInsertAtHead2CPolynomial
/*------------------------------------------------------------------*/
void testInsertAtHead2CPolynomial(void)
{
cout << "testInsertAtHead2CPolynomial\n";
cout << "============================\n"; char ch;
CPolynomial poly; do
{
{
CTerm cn('r');
poly.insertAtHead(cn);
cout << "After inserting " << cn << endl;
cout << poly << endl;
assert(poly.isOK());
} cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Output operator
//Overload << insertion operator for output
//////////////////////////////////////////////////////////////////////
ostream & operator << (ostream &os, const CPolynomial &poly)
{
os << "p(x," << poly.count << ") ="; if (0==poly.count)
cout << " 0";
else
for (CTerm *p=poly.head; (p!=NULL); p=p->next)
cout << *p; return os;
}
/*------------------------------------------------------------------*/
//testOperatorInsertionCPolynomial
/*------------------------------------------------------------------*/
void testOperatorInsertionCPolynomial(void)
{
cout << "testOperatorInsertionCPolynomial\n";
cout << "================================\n"; char ch; do
{
CPolynomial poly('r'); cout << poly << endl; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Overload >> extraction operator for input
//////////////////////////////////////////////////////////////////////
istream & operator >> (istream &is, CPolynomial &poly)
{
CTerm cn;
poly.deleteAll();
while(true)
{
is >> cn;
if (cn.coef != 0)
poly.insertAtTail(cn);
else
break;
} return is;
}
/*------------------------------------------------------------------*/
//testOperatorExtractionCPolynomial
/*------------------------------------------------------------------*/
void testOperatorExtractionCPolynomial(void)
{
cout << "testOperatorExtractionCPolynomial\n";
cout << "=================================\n"; char ch; do
{
CPolynomial poly('r'); cout << poly << endl;
cin >> poly;
cout << poly << endl; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//deleteAll
//Deletes all the nodes from the doubly-linked list
//////////////////////////////////////////////////////////////////////
void CPolynomial::deleteAll(void)
{
CTerm *p; while (head != NULL)
{
p = head;
head = head ->next;
delete p;
}
tail = NULL;
count = 0;
}
/*------------------------------------------------------------------*/
//void testDeleteAllCPolynomial(void)
/*------------------------------------------------------------------*/
void testDeleteAllCPolynomial(void)
{
cout << "testDeleteAllCPolynomial\n";
cout << "========================\n"; char ch; do
{
CPolynomial poly('r');
cout << poly << endl;
assert(poly.isOK());
poly.deleteAll();
cout << poly << endl;
assert(poly.isOK());
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//~CPolynomial
// Destructor
//////////////////////////////////////////////////////////////////////
CPolynomial::~CPolynomial(void)
{
deleteAll();
}
/*------------------------------------------------------------------*/
//testDestructorCPolynomial
/*------------------------------------------------------------------*/
void testDestructorCPolynomial(void)
{
cout << "testDestructorCPolynomial\n";
cout << "=========================\n"; char ch;
do
{
CPolynomial *ptrPoly = new CPolynomial('r'); cout << *ptrPoly << endl; delete ptrPoly; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//create
// Populates a polynomial with specified number of terms
// resulting polynomial may not be simplified
//////////////////////////////////////////////////////////////////////
void CPolynomial::populate(int n)
{
double coef;
int expo; if (n<0)
n = 0; head = tail = NULL;
count = 0; for (int i=0; i<n; i++)
{
coef = 1 + rand()%MAX_COEF;
if ((rand()%2) == 1)
coef = -coef;
expo = rand()%(MAX_EXPO+1);
insertAtTail(coef, expo);
}
}
/*------------------------------------------------------------------*/
//testPopulateCPolynomial
/*------------------------------------------------------------------*/
void testPopulateCPolynomial(void)
{
cout << "testPopulateCPolynomial\n";
cout << "======================\n"; char ch;
do
{
CPolynomial poly;
int n; cout << poly << endl; n = rand()%(MAX_TERMS+1); cout << "After populate(" << n << ");\n";
poly.populate(n);
cout << poly << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//CPolynomial
//Another constructor with coef paramater
//resulting polynomial has only one term if it is nonzero
//////////////////////////////////////////////////////////////////////
CPolynomial::CPolynomial(const double &coef)
{
head = tail = NULL;
count = 0;
if (0 != coef)
insertAtTail(coef, 0);
return;
}
//////////////////////////////////////////////////////////////////////
//CPolynomial
//Another constructor with coef paramater
//resulting polynomial has only one term if it is nonzero
//////////////////////////////////////////////////////////////////////
CPolynomial::CPolynomial(const int &coef)
{
head = tail = NULL;
count = 0;
if (0 != coef)
insertAtTail(coef, 0);
return;
}
/*------------------------------------------------------------------*/
//void testConstructorCoefCPolynomial(void)
//testConstructor4
//CPolynomial(n)
/*------------------------------------------------------------------*/
void testConstructorCoefCPolynomial(void)
{
cout << "testConstructorCoefCPolynomial\n";
cout << "==============================\n"; char ch;
do
{
int a = rand()%MAX_COEF+1;
float b = float(rand()%MAX_COEF+1);
double c = rand()%MAX_COEF+1;
cout << c << endl;
CPolynomial p1(a);
CPolynomial p2(b);
CPolynomial p3(c); cout << "p1= " << p1 << endl;
cout << "p2= " << p2 << endl;
cout << "p3= " << p3 << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//CPolynomial
//Another constructor with CTerm parameter
//resulting polynomial has only one term if it is nonzero
//////////////////////////////////////////////////////////////////////
CPolynomial::CPolynomial(const CTerm &cn)
{
head = tail = NULL;
count = 0;
if (0 != cn.coef)
insertAtTail(cn);
return;
}
/*------------------------------------------------------------------*/
//void testConstructorCTermCPolynomial(void)
//CPolynomial(cn)
/*------------------------------------------------------------------*/
void testConstructorCTermCPolynomial(void)
{
cout << "testConstructorCTermCPolynomial\n";
cout << "===============================\n"; char ch;
do
{
CTerm cn('r');
cout << "cn = " << cn << endl;
CPolynomial poly(cn); cout << "poly = " << poly << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//CPolynomial
// Another constructor with a character paramater
// It constructs a random polynomial with random number of terms
// r gives random polynomial
// m gives minimized (simplified) polynomial
//////////////////////////////////////////////////////////////////////
CPolynomial::CPolynomial(char ch)
{
switch (ch)
{
case 'r':
{
int tCount = rand()%MAX_TERMS;
populate(tCount);
break;
}
case 'm':
{
int tCount = rand()%MAX_TERMS;
head = tail = NULL;
populate(tCount);
this->simplify();
break;
}
default:
{
head = tail = NULL;
count = 0;
}
}
}
/*------------------------------------------------------------------*/
//testConstructorRandomCPolynomial
/*------------------------------------------------------------------*/
void testConstructorRandomCPolynomial(void)
{
cout << "testConstructorRandomCPolynomial\n";
cout << "================================\n"; char ch;
do
{
CPolynomial poly('r');
cout << poly << endl;
cin >> ch;
}
while (ch != '0');
}
/*------------------------------------------------------------------*/
//void testConstructorMinimizedCPolynomial(void)
//testConstructor3
//CPolynomial('m')
/*------------------------------------------------------------------*/
void testConstructorMinimizedCPolynomial(void)
{
cout << "testConstructorMinimizedCPolynomial\n";
cout << "===================================\n"; char ch;
do
{
CPolynomial poly('m');
cout << poly << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//sortDesc
//Sorts terms of the doubly-linked list in decreasing order of expo
//////////////////////////////////////////////////////////////////////
void CPolynomial::sortDesc(void)
{
if (count <= 1)
return; bool sorted;
CTerm *p1, *p2; do
{
sorted = true;
for (p1=head, p2=p1->next; p2!=NULL; p1=p2, p2=p2->next)
if (p1->expo < p2->expo)
{
swap(*p1, *p2);
sorted = false;
}
}
while (!sorted);
}
/*------------------------------------------------------------------*/
//void testSortDescCPolynomial(void)
//testSortDesc
/*------------------------------------------------------------------*/
void testSortDescCPolynomial(void)
{
cout << "testSortDescCPolynomial\n";
cout << "=======================\n"; char ch;
do
{
CPolynomial poly('r');
cout << poly << endl;
poly.sortDesc();
cout << "After poly.sortDesc()\n";
cout << poly << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//addressOfNodeAtPos
//Returns the address of a node at specified position (0, 1, 2, ...)
//////////////////////////////////////////////////////////////////////
CTerm * CPolynomial::addressOfNodeAtPos(int pos) const
{
if (pos >=count)
return NULL;
else if (NULL == head)
return NULL;
else
{
for (CTerm *ptr=head; pos>0; ptr=ptr->next,pos--)
;
return ptr;
}
}
/*------------------------------------------------------------------*/
//testAddressOfNodeAtPosCPolynomial
/*------------------------------------------------------------------*/
void testAddressOfNodeAtPosCPolynomial(void)
{
cout << "testAddressOfNodeAtPosCPolynomial\n";
cout << "======================\n"; char ch;
do
{
CPolynomial poly('r');
cout << poly << endl;
poly.displayNodes();
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//deleteAt
//Deletes the node at a given address
//Handles the following cases
// Empty list
// Only node
// First node
// Last node
// Between two or more nodes
//////////////////////////////////////////////////////////////////////
void CPolynomial::deleteAt(CTerm *p)
{
if (0 == count)
return;
//only node
else if ((head == p) && (tail == p))
{
head = NULL;
tail = NULL;
count = 0;
delete p;
}
//first node
else if (head == p)
{
head = p->next;
head->prev = NULL;
count--;
delete p;
}
//last node
else if (tail == p)
{
tail = p->prev;
tail->next = NULL;
count--;
delete p;
}
//between two nodes
else
{
p->prev->next = p->next;
p->next->prev = p->prev;
count--;
delete p;
}
}
/*------------------------------------------------------------------*/
//void testDeleteAtCPolynomial(void)
/*------------------------------------------------------------------*/
void testDeleteAtCPolynomial(void)
{
cout << "testDeleteAtCPolynomial\n";
cout << "=======================\n"; char ch;
CTerm *ptr;
int pos; do //delete the first node
{
CPolynomial poly('r');
cout << poly << endl; while (true)
{
pos = 0;
ptr = poly.addressOfNodeAtPos(pos);
if (NULL == ptr)
break;
else
{
cout << "Deleted node " << *ptr << " at position " << pos
<< " at address " << ptr << endl;
poly.deleteAt(ptr);
assert(poly.isOK());
cout << poly << endl;
}
}
cin >> ch;
}
while (ch != '0'); do //delete the last node
{
CPolynomial poly('r');
cout << poly << endl; while (true)
{
pos = poly.getCount()-1;
ptr = poly.addressOfNodeAtPos(pos);
if (NULL == ptr)
break;
else
{
cout << "Deleted node " << *ptr << " at position " << pos
<< " at address " << ptr << endl;
poly.deleteAt(ptr);
assert(poly.isOK());
cout << poly << endl;
}
}
cin >> ch;
}
while (ch != '0'); do //delete the node between two or more nodes
{
CPolynomial poly(double(3+rand()%MAX_TERMS));
cout << poly << endl; while (true)
{
if (poly.getCount() < 3)
break;
pos = rand()%poly.getCount();
ptr = poly.addressOfNodeAtPos(pos);
if (NULL == ptr)
break;
else
{
cout << "Deleted node " << *ptr << " at position " << pos
<< " at address " << ptr << endl;
poly.deleteAt(ptr);
assert(poly.isOK());
cout << poly << endl;
}
}
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//simplify
//Sorts polynomial in descending order by expo (exponent)
//Combines terms with equal expo (similar exponents)
//Deletes the terms with zero coeficients
//////////////////////////////////////////////////////////////////////
void CPolynomial::simplify(void)
{
if (count < 2)
return; sortDesc(); CTerm *p1, *p2; //combine nodes with same exponent values
for (p1=head; p1->next!=NULL; )
if (p1->expo == p1->next->expo)
{
p1->coef = p1->coef + p1->next->coef;
deleteAt(p1->next);
}
else
p1=p1->next; //Delete terms with zero coeficient
p1 = head;
while (p1 != NULL)
{
if (0 == p1->coef)
{
p2 = p1;
p1 = p1->next;
deleteAt(p2);
}
else
p1 = p1->next;
} }
/*------------------------------------------------------------------*/
//void testSimplifyCPolynomial(void)
/*------------------------------------------------------------------*/
void testSimplifyCPolynomial(void)
{
cout << "testSimplifyCPolynomial\n";
cout << "=======================\n"; char ch; do
{
CPolynomial poly('r');
cout << poly << endl;
assert(poly.isOK());
poly.simplify();
assert(poly.isOK());
cout << poly << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Copy constructor
//////////////////////////////////////////////////////////////////////
CPolynomial::CPolynomial (const CPolynomial &poly)
{
count = 0;
head = tail = NULL; for (CTerm *p=poly.head; p!=NULL; p=p->next)
{
//cout << poly << endl;
//cout << *p << endl;
insertAtTail(*p);
//cout << *this << endl;
}
//insertAtTail(p->coef, p->expo);
}
/*------------------------------------------------------------------*/
//void testCopyConstructorCPolynomial(void)
/*------------------------------------------------------------------*/
void testCopyConstructorCPolynomial(void)
{
cout << "testCopyConstructorCPolynomial\n";
cout << "=============================\n"; char ch; do
{
CPolynomial poly1('r');
CPolynomial poly2(poly1); cout << poly1 << endl;
assert(poly1.isOK());
cout << poly2 << endl;
assert(poly2.isOK());
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//getCount
//returns the number of terms in the polynomial
//////////////////////////////////////////////////////////////////////
int CPolynomial::getCount(void) const
{
return count;
}
/*------------------------------------------------------------------*/
//testGetCountCPolynomial
/*------------------------------------------------------------------*/
void testGetCountCPolynomial(void)
{
cout << "testGetCountCPolynomial\n";
cout << "=======================\n"; char ch;
do
{
CPolynomial poly('r');
cout << poly << endl;
cout << "Number of terms = " << poly.getCount() << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Check
//Checks the integrity of the polynomial represented by the list
// count should agree with the number of nodes
// End nodes (first and last) to have NULL pointers
// each node should have valid pointers
//////////////////////////////////////////////////////////////////////
bool CPolynomial::isOK(void) const
{
if (0 == count)
return true; bool checkResult = true;
int tCount = 0;
CTerm *p; //Check is count is OK
for (p=head; p!=NULL; p=p->next)
tCount++; if (tCount != count)
{
cout << "isOK: Count is corrupt\n";
checkResult = false;
} //Check if the end nodes have NULL pointers
if (head->prev != NULL)
{
cout << "isOK: head node's prev pointer is corrupt\n";
checkResult = false;
} if (tail->next != NULL)
{
cout << "isOK: tail node's next pointer is corrupt\n";
checkResult = false;
} //Check if each node has valid pointers
for (p=head; p!=NULL; p=p->next)
{
if ((p->next != NULL) && (p->next->prev != p))
{
cout << "isOK: pointers are corupt\n";
checkResult = false;
break;
}
if ((p->prev != NULL) && (p->prev->next != p))
{
cout << "isOK: pointers are corupt\n";
checkResult = false;
break;
}
} return checkResult;
}
/*------------------------------------------------------------------*/
//testIsOKCPolynomial
/*------------------------------------------------------------------*/
void testIsOKCPolynomial(void)
{
cout << "testIsOKCPolynomial\n";
cout << "===================\n"; char ch;
do
{
CPolynomial poly('r'); cout << poly << endl; if (poly.isOK())
cout << "polynomial is OK\n";
else
cout << "polynomial is not OK\n"; cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Assignment operator (=) overloaded
//////////////////////////////////////////////////////////////////////
CPolynomial & CPolynomial::operator = (const CPolynomial &poly)
{
if ((this != &poly) && (*this != poly))
{
if (count == poly.count)
for (CTerm *p1=head, *p2=poly.head; p1!=NULL; p1=p1->next, p2=p2->next)
*p1 = *p2;
else
{
if (count > 0)
this->deleteAll();
for (CTerm *p=poly.head; p!=NULL; p=p->next)
insertAtTail(*p);
}
} return *this;
}
/*------------------------------------------------------------------*/
//void testOperatorAssignCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorAssignCPolynomial(void)
{
cout << "testOperatorAssignCPolynomial\n";
cout << "=============================\n"; char ch; do
{
CPolynomial poly1('r'), poly2('r'), poly3('r');
cout << "p1 " << poly1 << endl;
cout << "p2 " << poly2 << endl;
cout << "p3 " << poly3 << endl; assert(poly1.isOK());
assert(poly2.isOK());
assert(poly3.isOK()); poly1 = poly2 = poly3; cout << "After poly1 = poly2 = poly3;\n";
cout << "p1 " << poly1 << endl;
cout << "p2 " << poly2 << endl;
cout << "p3 " << poly3 << endl;
assert(poly1.isOK());
assert(poly2.isOK());
assert(poly3.isOK()); poly1 = poly1;
cout << "After poly1 = poly1;\n";
cout << "p1 " << poly1 << endl << endl;
assert(poly1.isOK()); cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Addition operator (+) overloaded
//returns simplified polynomial representing sum of two polynomials
//////////////////////////////////////////////////////////////////////
CPolynomial & CPolynomial::operator + (const CPolynomial &poly)
{
CPolynomial *tPoly = new CPolynomial (*this); for (CTerm *p=poly.head; p!=NULL; p=p->next)
tPoly->insertAtTail(*p);
//tPoly->insertAtTail(p->coef, p->expo); tPoly->simplify();
return *tPoly;
}
//////////////////////////////////////////////////////////////////////
//CPolynomial & operator + (const CTerm &cn, const CPolynomial &poly)
//////////////////////////////////////////////////////////////////////
CPolynomial & operator + (const CTerm &cn, const CPolynomial &poly)
{
CPolynomial *ptrPoly = new CPolynomial(poly); ptrPoly->insertAtTail(cn); ptrPoly->simplify(); return *ptrPoly;
}
/*------------------------------------------------------------------*/
//void testOperatorAdditionCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorAdditionCPolynomial(void)
{
cout << "testOperatorAdditionCPolynomial\n";
cout << "===============================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'), p3('m');
cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << "p3 = " << p3 << endl;
cout << endl;
p3 = p1 + p2; assert(p1.isOK());
assert(p2.isOK());
assert(p3.isOK()); cout << "After p3 = p1 + p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << "p3 = " << p3 << endl; if (areCrossLinked(p1,p2))
cout << "p1 and p2 are croslinked\n"; if (areCrossLinked(p1,p3))
cout << "p1 and p3 are croslinked\n"; if (areCrossLinked(p2,p3))
cout << "p2 and p3 are croslinked\n";
CTerm cn('r');
cout << "cn = " << cn << endl; p1 = p1 + cn;
cout << "After p1 = p1 + cn;\n";
cout << "p1 = " << p1 << endl;
p1 = cn + p1;
cout << "After p1 = cn + p1;\n";
cout << "p1 = " << p1 << endl; double d = rand()%MAX_COEF;
p1 = p1 +d;
cout << "d = " << d << endl;
cout << "After p1 = p1 + d;\n";
cout << "p1 = " << p1 << endl; p1 = d + p1;
cout << "d = " << d << endl;
cout << "After p1 = d + p1;\n";
cout << "p1 = " << p1 << endl;
cout << endl;
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Subtraction operator (-) overloaded
//returns simplified polynomial representing difference of two
//polynomials
//////////////////////////////////////////////////////////////////////
CPolynomial & CPolynomial::operator - (const CPolynomial &poly)
{
CPolynomial *tPoly = new CPolynomial (*this); for (CTerm *p=poly.head; p!=NULL; p=p->next)
tPoly->insertAtTail(-*p); tPoly->simplify();
return *tPoly;
}
//////////////////////////////////////////////////////////////////////
//CPolynomial & operator - (const CTerm &cn, const CPolynomial &poly)
//////////////////////////////////////////////////////////////////////
CPolynomial & operator - (const CTerm &cn, const CPolynomial &poly)
{
CPolynomial *ptrPoly = new CPolynomial(-poly); ptrPoly->insertAtTail(cn); ptrPoly->simplify(); return *ptrPoly;
}
/*------------------------------------------------------------------*/
//void testOperatorSubtractionCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorSubtractionCPolynomial(void)
{
cout << "testOperatorSubtractionCPolynomial\n";
cout << "==================================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'), p3('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << "p3 = " << p3 << endl;
cout << endl;
p3 = p1 - p2; cout << "After p3 = p1 - p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << "p3 = " << p3 << endl; if (areCrossLinked(p1,p2))
cout << "p1 and p2 are croslinked\n"; if (areCrossLinked(p1,p3))
cout << "p1 and p3 are croslinked\n"; if (areCrossLinked(p2,p3))
cout << "p2 and p3 are croslinked\n";
CTerm cn('r');
cout << "cn = " << cn << endl; p1 = p1 - cn;
cout << "After p1 = p1 - cn;\n";
cout << "p1 = " << p1 << endl;
p1 = cn - p1;
cout << "After p1 = cn - p1;\n";
cout << "p1 = " << p1 << endl; double d = rand()%MAX_COEF;
p1 = p1 - d;
cout << "d = " << d << endl;
cout << "After p1 = p1 - d;\n";
cout << "p1 = " << p1 << endl; p1 = d - p1;
cout << "d = " << d << endl;
cout << "After p1 = d - p1;\n";
cout << "p1 = " << p1 << endl;
cout << endl;
cout << endl;
cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//operator == overloading
//possibilities:
// polynomials are different
// counts are different
// counts are same but some terms are not
// polynomials are same
// counts are zero
// counts are equal and all terms are equal
//////////////////////////////////////////////////////////////////////
bool CPolynomial::operator == (const CPolynomial &poly) const
{
if (count != poly.count)
return false;
else if (0==count) // at this point count==poly.count
return true;
else
{
CTerm *p1, *p2;
p1 = this->head;
p2 = poly.head; while (p1 != NULL)
{
//if ((p1->coef != p2->coef) || (p1->expo != p2->expo))
if (*p1 != *p2)
return false;
p1 = p1->next;
p2 = p2->next;
} return true;
}
} bool operator == (const CTerm &cn, const CPolynomial &poly)
{
return (poly==cn);
} /*------------------------------------------------------------------*/
//testOperatorEqualCPolynomial
/*------------------------------------------------------------------*/
void testOperatorEqualCPolynomial(void)
{
cout << "testOperatorEqualCPolynomial\n";
cout << "============================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; if (p1 == p2)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
p1 = p2; cout << "After p1 = p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl; if (p1 == p2)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
cout << "result of (p1 == p1)\n"; if (p1 == p1)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
CTerm cn('r');
cout << "cn = " << cn << endl;
cout << "result of (p1 == cn)\n"; if (p1 == cn)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
cout << "result of (cn == p1)\n"; if (cn == p1)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
p1 = cn;
cout << "After p1 = cn;\n";
cout << "p1 = " << p1 << endl;
cout << "cn = " << cn << endl;
cout << "result of (p1 == cn)\n"; if (p1 == cn)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
cout << "result of (cn == p1)\n"; if (cn == p1)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
double d = rand()%MAX_COEF;
cout << "d = " << d << endl;
cout << "result of (p2 == d)\n"; if (p2 == d)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
cout << "result of (d == p2)\n"; if (d == p2)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
p1 = d;
cout << "After p1 = d;\n";
cout << "p1 = " << p1 << endl;
cout << "d = " << d << endl;
cout << "result of (p1 == d)\n"; if (p1 == d)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
cout << "result of (d == p1)\n"; if (d == p1)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
if (1 == p1)
cout << "polynomials are equal\n";
else
cout << "polynomials are NOT equal\n";
cout << endl;
cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//operator != overloading
// uses ==
//////////////////////////////////////////////////////////////////////
bool CPolynomial::operator != (const CPolynomial &poly) const
{
return !(*this == poly);
}
bool operator != (const CTerm &cn, const CPolynomial &poly)
{
return (poly != cn);
}
/*------------------------------------------------------------------*/
//testOperatorNotEqualCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorNotEqualCPolynomial(void)
{
cout << "testOperatorNotEqualCPolynomial\n";
cout << "===============================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; if (p1 != p2)
cout << "polynomials are NOT equal\n";
else
cout << "polynomials are equal\n";
p1 = -p2; cout << "After p1 = -p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl; if (p1 != p2)
cout << "polynomials are NOT equal\n";
else
cout << "polynomials are equal\n";
cout << "result of (p1 != p1\n";
if (p1 != p1)
cout << "polynomials are NOT equal\n";
else
cout << "polynomials are equal\n"; CTerm cn('r');
cout << "cn = " << cn << endl;
cout << "result of (p1 != cn)\n"; if (p1 != cn)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
cout << "result of (cn != p1)\n"; if (cn != p1)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
p1 = cn;
cout << "After p1 = cn;\n";
cout << "p1 = " << p1 << endl;
cout << "cn = " << cn << endl;
cout << "result of (p1 != cn)\n"; if (p1 != cn)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
cout << "result of (cn != p1)\n"; if (cn != p1)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
double d = rand()%MAX_COEF;
cout << "d = " << d << endl;
cout << "result of (p2 != d)\n"; if (p2 != d)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
cout << "result of (d != p2)\n"; if (d != p2)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
p1 = d;
cout << "After p1 = d;\n";
cout << "p1 = " << p1 << endl;
cout << "d = " << d << endl;
cout << "result of (p1 != d)\n"; if (p1 != d)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
cout << "result of (d != p1)\n"; if (d != p1)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
if (1 != p1)
cout << "polynomials are not equal\n";
else
cout << "polynomials are equal\n";
cout << endl;
cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//CPolynomial & CPolynomial::operator -() const
//////////////////////////////////////////////////////////////////////
CPolynomial & CPolynomial::operator -() const
{
CPolynomial *polyPtr = new CPolynomial;
if (count > 0)
{
CTerm *ptr = head; while (ptr != NULL)
{
//polyPtr->insertAtTail(-ptr->coef, ptr->expo);
polyPtr->insertAtTail(-*ptr);
ptr = ptr->next;
}
}
return *polyPtr; }
/*------------------------------------------------------------------*/
//testOperatorUnaryNegativeCPolynomial
/*------------------------------------------------------------------*/
void testOperatorUnaryNegativeCPolynomial(void)
{
cout << "testOperatorUnaryNegativeCPolynomial\n";
cout << "====================================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; p1 = -p2; cout << "After p1 = -p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; CPolynomial p3('m'), p4('m'), p5('m'); cout << "p3 = " << p3 << endl;
cout << "p4 = " << p4 << endl;
cout << "p5 = " << p5 << endl;
cout << endl; p3 = -(p4 + p5); cout << "After p3 = -(p4 + p5);\n"; cout << "p3 = " << p3 << endl;
cout << "p4 = " << p4 << endl;
cout << "p5 = " << p5 << endl;
cout << endl; cout << "output caused by cout << -p5;\n";
cout << -p5 << endl; cout << endl;
cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//CPolynomial & CPolynomial::operator +() const
//////////////////////////////////////////////////////////////////////
CPolynomial & CPolynomial::operator +() const
{ CPolynomial *p = new CPolynomial(*this);
return *p;
/*
CPolynomial *polyPtr = new CPolynomial;
if (count>0)
{
CTerm *ptr = head; while (ptr != NULL)
{
//polyPtr->insertAtTail(ptr->coef, ptr->expo);
polyPtr->insertAtTail(*ptr);
ptr = ptr->next;
}
}
return *polyPtr;
*/ }
/*------------------------------------------------------------------*/
//testOperatorUnaryPlusCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorUnaryPlusCPolynomial(void)
{
cout << "testOperatorUnaryPlusCPolynomial\n";
cout << "================================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; p1 = +p2; cout << "After p1 = +p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; CPolynomial p3('m'), p4('m'), p5('m'); cout << "p3 = " << p3 << endl;
cout << "p4 = " << p4 << endl;
cout << "p5 = " << p5 << endl;
cout << endl; p3 = +(p4 + p5); cout << "After p3 = +(p4 + p5);\n"; cout << "p3 = " << p3 << endl;
cout << "p4 = " << p4 << endl;
cout << "p5 = " << p5 << endl;
cout << endl; cout << "output caused by cout << +p5;\n";
cout << +p5 << endl; cout << endl;
cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Multiplication operator (*) overloaded
//returns simplified polynomial representing product of two polynomials
//////////////////////////////////////////////////////////////////////
CPolynomial & CPolynomial::operator * (const CPolynomial &poly)
{
CPolynomial *tPoly = new CPolynomial;
CTerm *p1, *p2; for (p1=this->head; p1!=NULL; p1=p1->next)
for (p2=poly.head; p2!=NULL; p2=p2->next)
tPoly->insertAtTail((*p1)*(*p2));
//tPoly->insertAtTail(p1->coef*p2->coef, p1->expo+p2->expo); tPoly->simplify();
return *tPoly;
}
/*------------------------------------------------------------------*/
//void testOperatorMultiplicationCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorMultiplicationCPolynomial(void)
{
cout << "testOperatorMultiplicationCPolynomial\n";
cout << "=====================================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'), p3('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << "p3 = " << p3 << endl;
cout << endl;
p3 = p1 * p2; cout << "After p3 = p1 * p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << "p3 = " << p3 << endl; if (areCrossLinked(p1,p2))
cout << "p1 and p2 are croslinked\n"; if (areCrossLinked(p1,p3))
cout << "p1 and p3 are croslinked\n"; if (areCrossLinked(p2,p3))
cout << "p2 and p3 are croslinked\n"; CTerm cn('r');
cout << "cn = " << cn << endl; p1 = p1 * cn;
cout << "After p1 = p1 * cn;\n";
cout << "p1 = " << p1 << endl;
p1 = cn * p1;
cout << "After p1 = cn * p1;\n";
cout << "p1 = " << p1 << endl; double d = rand()%MAX_COEF;
p1 = p1 * d;
cout << "d = " << d << endl;
cout << "After p1 = p1 * d;\n";
cout << "p1 = " << p1 << endl; p1 = d * p1;
cout << "d = " << d << endl;
cout << "After p1 = d * p1;\n";
cout << "p1 = " << p1 << endl; int i = rand()%MAX_COEF;
p1 = i * p1;
cout << "i = " << i << endl;
cout << "After p1 = i * p1;\n";
cout << "p1 = " << p1 << endl; cout << i*p1 << endl; cout << endl;
cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Add and assign operator (+=) overloaded
//////////////////////////////////////////////////////////////////////
void CPolynomial::operator += (const CPolynomial &poly)
{
for (CTerm *p=poly.head; p!=NULL; p=p->next)
this->insertAtHead(*p);
//this->insertAtTail(*p); // will not work for p += p;
//this->insertAtTail(p->coef, p->expo); // will not work for p += p; this->simplify();
}
/*------------------------------------------------------------------*/
//void testOperatorAddAssignCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorAddAssignCPolynomial(void)
{
cout << "testOperatorAddAssignCPolynomial\n";
cout << "===============================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; p1 += p2; cout << "After p1 += p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; p1 += p1;
cout << "After p1 += p1;\n"; cout << "p1 = " << p1 << endl;
cout << endl; cin >> ch; }
while (ch != '0');
}
//Subtract and assign operator (-=) overloaded
//
//////////////////////////////////////////////////////////////////////
void CPolynomial::operator -= (const CPolynomial &poly)
{
for (CTerm *p=poly.head; p!=NULL; p=p->next)
this->insertAtHead(-*p);
//this->insertAtTail(-p->coef, p->expo); this->simplify();
}
/*------------------------------------------------------------------*/
//void testOperatorSubtractAssignCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorSubtractAssignCPolynomial(void)
{
cout << "testOperatorSubtractAssignCPolynomial\n";
cout << "=====================================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; p1 -= p2; cout << "After p1 -= p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//Multiply and assign operator (*=) overloaded
//////////////////////////////////////////////////////////////////////
void CPolynomial::operator *= (const CPolynomial &poly)
{
CPolynomial *tPoly = new CPolynomial;
CTerm *p1, *p2; for (p1=this->head; p1!=NULL; p1=p1->next)
for (p2=poly.head; p2!=NULL; p2=p2->next)
tPoly->insertAtTail((*p1)*(*p2));
//tPoly->insertAtTail(p1->coef*p2->coef, p1->expo+p2->expo); tPoly->simplify();
*this = *tPoly;
}
/*------------------------------------------------------------------*/
//void testOperatorMultiplyAssignCPolynomial(void)
/*------------------------------------------------------------------*/
void testOperatorMultiplyAssignCPolynomial(void)
{
cout << "testOperatorMultiplyAssignCPolynomial\n";
cout << "=====================================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; p1 *= p2; cout << "After p1 *= p2;\n"; cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl; p1 *= p1; cout << "After p1 *= p1;\n"; cout << "p1 = " << p1 << endl; cout << endl; cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//areCrossLinked
// Checks if two polynomials are cross-linked
// in other words, checks if two polynomials have a node in common
//////////////////////////////////////////////////////////////////////
bool areCrossLinked(const CPolynomial &poly1, const CPolynomial &poly2)
{
CTerm *p1, *p2;
for (p1 = poly1.head; (p1 != NULL); p1=p1->next)
for (p2 = poly2.head; (p2 != NULL); p2=p2->next)
if (p1 == p2)
return true;
return false;
}
/*------------------------------------------------------------------*/
//testAreCrossLinkedCPolynomial(void)
/*------------------------------------------------------------------*/
void testAreCrossLinkedCPolynomial(void)
{
cout << "testAreCrossLinkedCPolynomial\n";
cout << "=============================\n"; char ch;
do
{
CPolynomial p1('m'), p2('m'); cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << endl; cout << "p1 and p2";
if (areCrossLinked(p1, p2))
cout << " are cross linked\n";
else
cout << " are not cross linked\n"; cout << "p1 and p1";
if (areCrossLinked(p1, p1))
cout << " are cross linked\n";
else
cout << " are not cross linked\n"; cout << endl; cin >> ch; }
while (ch != '0');
}
/*------------------------------------------------------------------*/
//testOperatorAddCTermAnddouble(void)
/*------------------------------------------------------------------*/
void testOperatorAddCTermAnddouble(void)
{
cout << "testOperatorAddCTermAnddouble\n";
cout << "===============================\n";
char ch;
do
{
CTerm cn('r');
double c = rand()%MAX_COEF; cout << "cn = " << cn << endl;
cout << "c = " << c << endl;
cn = cn + c;
cout << "after cn = cn + c;\n";
cout << cn << endl;
cout << endl; cin >> ch; }
while (ch != '0');
}
/*------------------------------------------------------------------*/
//testOperatorAddCPolynomialAnddouble(void)
/*------------------------------------------------------------------*/
void testOperatorAddCPolynomialAnddouble(void)
{
cout << "testOperatorAddCPolynomialAnddouble\n";
cout << "=====================================\n";
char ch;
do
{
CPolynomial poly('m');
double c = rand()%MAX_COEF; cout << "poly = " << poly << endl;
cout << "c = " << c << endl;
poly = poly + c;
cout << "after poly = poly + c;\n";
cout << "poly = " << poly << endl;
cout << endl; cin >> ch; }
while (ch != '0');
}
/*------------------------------------------------------------------*/
//testOperatorAddCPolynomialAndCTerm(void)
/*------------------------------------------------------------------*/
void testOperatorAddCPolynomialAndCTerm(void)
{
cout << "testOperatorAddCPolynomialAndCTerm\n";
cout << "==================================\n";
char ch;
do
{
CPolynomial poly('m');
CTerm cn('r'); cout << "poly = " << poly << endl;
cout << "cn = " << cn << endl;
poly = poly + cn;
cout << "after poly = poly + cn;\n";
cout << "poly = " << poly << endl;
cout << endl; cin >> ch; }
while (ch != '0');
}
/*------------------------------------------------------------------*/
//testOperatorSubtractCPolynomialAndCTerm(void)
/*------------------------------------------------------------------*/
void testOperatorSubtractCPolynomialAndCTerm(void)
{
cout << "testOperatorSubtractCPolynomialAndCTerm\n";
cout << "=======================================\n";
char ch;
do
{
CPolynomial poly('m');
CTerm cn('r'); cout << "poly = " << poly << endl;
cout << "cn = " << cn << endl;
poly = poly - cn;
cout << "after poly = poly - cn;\n";
cout << "poly = " << poly << endl;
cout << endl; cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//CPolynomial & operator * (const CTerm &cn, const CPolynomial &poly)
//////////////////////////////////////////////////////////////////////
CPolynomial & operator * (const CTerm &cn, const CPolynomial &poly)
{
CPolynomial *ptrPoly = new CPolynomial(poly); for (CTerm *p=ptrPoly->head; p!=NULL; p=p->next)
*p = (*p)*cn; return *ptrPoly;
}
/*------------------------------------------------------------------*/
//testOperatorMultiplyCPolynomialAndCTerm(void)
/*------------------------------------------------------------------*/
void testOperatorMultiplyCPolynomialAndCTerm(void)
{
cout << "testOperatorMultiplyCPolynomialAndCTerm\n";
cout << "=======================================\n";
char ch;
do
{
CPolynomial poly('m');
CTerm cn('r'); cout << "poly = " << poly << endl;
cout << "cn = " << cn << endl;
poly = poly * cn;
cout << "after poly = poly * cn;\n";
cout << "poly = " << poly << endl;
poly = cn * poly;
cout << "after poly = cn * poly;\n";
cout << "poly = " << poly << endl;
cout << endl; cin >> ch; }
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//displayNodes
// Displays position, address, value (coef and expo) of each node
//////////////////////////////////////////////////////////////////////
void CPolynomial::displayNodes(void) const
{
int pos;
CTerm *ptr; cout << setw(05) << "Pos"
<< setw(15) << "Address"
<< setw(20) << "Value" << endl; for (pos = 0; pos < count; pos++)
{
ptr = addressOfNodeAtPos(pos);
if (ptr != NULL)
{
cout << setw(05) << pos
<< setw(15) << ptr;
cout << setw(15) << *ptr;
cout << endl;
}
}
}
/*------------------------------------------------------------------*/
//void testDisplayNodesCPolynomial(void)
/*------------------------------------------------------------------*/
void testDisplayNodesCPolynomial(void)
{
cout << "testDisplayNodesCPolynomial\n";
cout << "===========================\n"; char ch; do
{
CPolynomial poly('r');
cout << poly << endl;
assert(poly.isOK());
poly.displayNodes();
cin >> ch;
}
while (ch != '0');
}
//////////////////////////////////////////////////////////////////////
//displayReverse
// Displays the elements in reverse order (tail to head)
//////////////////////////////////////////////////////////////////////
void CPolynomial::displayReverse(void) const
{
CTerm *p;
cout << "p(x," << count << ") =";
p = tail; while (p != NULL)
{
cout << *p;
p = p->prev;
} cout << endl;
} /*------------------------------------------------------------------*/
//void testDisplayReverseCPolynomial(void)
/*------------------------------------------------------------------*/
void testDisplayReverseCPolynomial(void)
{
cout << "testDisplayReverseCPolynomial\n";
cout << "=============================\n"; char ch; do
{
CPolynomial poly('r');
cout << poly << endl;
assert(poly.isOK());
poly.displayReverse();
assert(poly.isOK());
cin >> ch;
}
while (ch != '0');
}
/*------------------------------------------------------------------*/
//testAllCPolynomial(void)
/*------------------------------------------------------------------*/
void testAllCPolynomial(void)
{
testConstructorDefaultCPolynomial(); //CPolynomial()
testInsertAtTailCPolynomial(); //insertAtTail(coef,expo)
testConstructorCoefCPolynomial(); //CPolynomial(coef)
testConstructorRandomCPolynomial(); //CPolynomial('r')
testSortDescCPolynomial(); //sortDesc()
testSimplifyCPolynomial(); //simplify()
testConstructorMinimizedCPolynomial(); //CPolynomial('m')
testCopyConstructorCPolynomial(); //CPolynomial poly2(poly1)
testDeleteAllCPolynomial(); //deleteAll()
testDestructorCPolynomial(); //~CPolynomial
testAddressOfNodeAtPosCPolynomial(); //addressOfNodeAtPos
testDeleteAtCPolynomial(); //deleteAt(ptr)
testOperatorAssignCPolynomial(); //poly1=poly2
testOperatorAssignCPolynomial(); //poly1==poly2
testOperatorAdditionCPolynomial(); //poly1=poly2+poly3
testOperatorSubtractionCPolynomial(); //poly1=poly2+poly3
testOperatorEqualCPolynomial(); //poly1==poly2
testOperatorNotEqualCPolynomial(); //poly1!=poly2
testOperatorUnaryNegativeCPolynomial(); //poly1=-poly2
testOperatorUnaryPlusCPolynomial(); //poly1=-poly2
testOperatorMultiplicationCPolynomial(); //p1 = p2 * p3
testOperatorAddAssignCPolynomial(); //poly1 += poly2
testOperatorSubtractAssignCPolynomial(); //poly1 -= poly2
testOperatorMultiplyAssignCPolynomial(); //poly1 *= poly2
}
/*------------------------------------------------------------------*/
//testAllCTerm(void)
/*------------------------------------------------------------------*/
void testAllCTerm(void)
{
testConstructorDefaultCTerm(); //CTerm()
testOperatorInsertionCTerm(); //cout << cn
testOperatorExtractionCTerm(); //cin >> cn
testConstructorCoefCTerm(); //CTerm(coef)
testConstructorAnotherCTerm(); //CTerm(coef, expo)
testConstructorCopyCTerm(); //CTerm(cn)
testConstructorRandomCTerm(); //CTerm('r')
testOperatorAssignCTerm(); //n1 = n2
testOperatorAdditionCTerm(); //n1=n2+n3
testOperatorSubtractionCTerm(); //n1=n2-n3
testOperatorMultiplicationCTerm(); //n1=n2*n3
testOperatorDivisionCTerm(); //n1=n2/n3
testOperatorEqualCTerm(); //n1==n2
testOperatorNotEqualCTerm(); //n1!=n2
testOperatorUnaryMinusCTerm(); //n1=-n2
testOperatorUnaryPlusCTerm(); //n1=+n2
testSwap(); //swap(n1, n2)
}
/*------------------------------------------------------------------*/
//testCTerm(void)
/*------------------------------------------------------------------*/
void testCTerm(void)
{
srand(time(NULL));
char ch; do
{
cout << "TEST CTerm MENU \n";
cout << "a CTerm(void) \n";
cout << "b cout << cn \n";
cout << "c CTerm(coef,expo) \n";
cout << "d CTerm(coef) \n";
cout << "e CTerm(CTerm n) \n";
cout << "f CTerm('r') \n";
cout << "g Assign (=) \n";
cout << "h Addition(+) \n";
cout << "i Subtraction(-) \n";
cout << "j Multiplication (*) \n";
cout << "k Division (/) \n";
cout << "l Equal(==) \n";
cout << "m Not Equal(!=) \n";
cout << "n Negation(-) \n";
cout << "o Plus (+) \n";
cout << "p cin >> cn \n";
cout << "q swap (n1, n2) \n";
cout << "r CTerm(string) \n"; cout << "z testAll() \n";
cin >> ch;
switch (ch)
{
case 'a': testConstructorDefaultCTerm(); break; //CTerm()
case 'b': testOperatorInsertionCTerm(); break; //cout << cn
case 'c': testConstructorAnotherCTerm(); break; //CTerm(coef, expo)
case 'd': testConstructorCoefCTerm(); break; //CTerm(coef)
case 'e': testConstructorCopyCTerm(); break; //CTerm(cn)
case 'f': testConstructorRandomCTerm(); break; //CTerm('r')
case 'g': testOperatorAssignCTerm(); break; //n1 = n2
case 'h': testOperatorAdditionCTerm(); break; //n1=n2+n3
case 'i': testOperatorSubtractionCTerm(); break; //n1=n2-n3
case 'j': testOperatorMultiplicationCTerm(); break; //n1=n2*n3
case 'k': testOperatorDivisionCTerm(); break; //n1=n2/n3
case 'l': testOperatorEqualCTerm(); break; //n1==n2
case 'm': testOperatorNotEqualCTerm(); break; //n1!=n2
case 'n': testOperatorUnaryMinusCTerm(); break; //n1=-n2
case 'o': testOperatorUnaryPlusCTerm(); break; //n1=+n2
case 'p': testOperatorExtractionCTerm(); break; //cin >> cn
case 'q': testSwap(); break; //swap(n1,n2)
case 'r': testConstructorStringCTerm(); break; //CTerm("3*x^5") case 'z': testAllCTerm(); break;
default: break;
}
}
while (ch!='0');
}
/*------------------------------------------------------------------*/
//testCPolynomial(void)
/*------------------------------------------------------------------*/
void testCPolynomial(void)
{
srand(time(NULL));
char ch; do
{
cout << "TEST CPolynomial MENU u Not Equal(!=) \n";
cout << "a CPolynomial(void) v Negation(-) \n"; //
cout << "b insertAtTail(coef,expo) w Plus (+) \n"; //
cout << "c insertAtTail(cn) x Multiplication (*) \n"; //
cout << "1 insertAtHead(coef,expo) \n"; //
cout << "2 insertAtHead(cn) \n"; // cout << "d insertion (<<) operator y Add & assign(+=) \n"; //
cout << "e extraction (>>) operator z Subtract & assign(-=) \n"; //
cout << "f deleteAll() A Multiply & assign(*=) \n"; //
cout << "g ~CPolynomial() B crossAreLinked(pouly,poly2)\n"; //
cout << "h populate(n) C testAll() \n"; //
cout << "i CPolynomial(coef) D testOperatorAddCTermAnddouble\n"; //
cout << "G CPolynomial(cn) \n"; //
cout << "j CPolynomial('r') E testOperatorAddCPolynomialAnddouble\n"; //
cout << "k CPolynomial('m') F testOperatorAddCPolynomialAndCTerm\n"; //
cout << "l sortDesc(void) H testOperatorSubtractCPolynomialAndCTerm\n"; //
cout << "m addressOfNodeAtPos(pos) I testOperatorMultiplyCPolynomialAndCTerm\n"; //
cout << "n deleteAt(ptr) J testConstructorStringCPolynomial();\n"; //
cout << "o simplify() \n"; //
cout << "p Copy constructor \n"; //
cout << "q Assign (=) \n";
cout << "r Addition(+) \n";
cout << "s Subtraction(-) \n";
cout << "t Equal(==) \n";
cout << "";
cout << "";
cout << "";
cout << "";
cout << "";
cout << "";
cout << "";
cout << "";
cout << "";
cin >> ch;
switch (ch)
{
case 'a': testConstructorDefaultCPolynomial(); break; //*CPolynomial()
case 'b': testInsertAtTailCPolynomial(); break; //*insertAtTail(coef,expo)
case 'c': testInsertAtTail2CPolynomial(); break; //*insertAtTail(cn)
case '1': testInsertAtHeadCPolynomial(); break; //*insertAtHead(coef,expo)
case '2': testInsertAtHead2CPolynomial(); break; //*insertAtHead(cn)
case 'd': testOperatorInsertionCPolynomial(); break; //*cout << p1
case 'e': testOperatorExtractionCPolynomial(); break; //*cout << p1
case 'f': testDeleteAllCPolynomial(); break; //*deleteAll()
case 'g': testDestructorCPolynomial(); break; //*~CPolynomial
case 'h': testPopulateCPolynomial(); break; //*populate
case 'i': testConstructorCoefCPolynomial(); break; //*CPolynomial(coef)
case 'G': testConstructorCTermCPolynomial(); break; //*CPolynomial(cn)
case 'j': testConstructorRandomCPolynomial(); break; //*CPolynomial('r')
case 'k': testConstructorMinimizedCPolynomial(); break; //*CPolynomial('m')
case 'l': testSortDescCPolynomial(); break; //*sortDesc()
case 'm': testAddressOfNodeAtPosCPolynomial(); break; //*addressOfNodeAtPos
case 'n': testDeleteAtCPolynomial(); break; //*deleteAt(ptr)
case 'o': testSimplifyCPolynomial(); break; //*simplify()
case 'p': testCopyConstructorCPolynomial(); break; //*CPolynomial poly2(poly1)
case 'q': testOperatorAssignCPolynomial(); break; //*poly1=poly2
case 'r': testOperatorAdditionCPolynomial(); break; //*poly1=poly2+poly3
case 's': testOperatorSubtractionCPolynomial(); break; //*poly1=poly2-poly3
case 't': testOperatorEqualCPolynomial(); break; //*poly1==poly2
case 'u': testOperatorNotEqualCPolynomial(); break; //*poly1!=poly2
case 'v': testOperatorUnaryNegativeCPolynomial(); break; //*poly1 = -poly2
case 'w': testOperatorUnaryPlusCPolynomial(); break; //*poly1 = +poly2
case 'x': testOperatorMultiplicationCPolynomial(); break; //*poly1 =poly2*poly3
case 'y': testOperatorAddAssignCPolynomial(); break; //*poly1 += poly2
case 'z': testOperatorSubtractAssignCPolynomial(); break; //*poly1 -= poly2
case 'A': testOperatorMultiplyAssignCPolynomial(); break; //*poly1 *= poly2
case 'B': testAreCrossLinkedCPolynomial(); break;
case 'C': testAllCPolynomial(); break;
case 'D': testOperatorAddCTermAnddouble(); break;
case 'E': testOperatorAddCPolynomialAnddouble(); break;
case 'F': testOperatorAddCPolynomialAndCTerm(); break;
case 'H': testOperatorSubtractCPolynomialAndCTerm();break;
case 'I': testOperatorMultiplyCPolynomialAndCTerm();break;
case 'J': testConstructorStringCPolynomial(); break;
default: break;
}
}
while (ch!='0');
}
/*------------------------------------------------------------------*/
//main(void)
/*------------------------------------------------------------------*/
void main(void)
{
srand(time(NULL));
testConstructorStringCTerm();
//testCPolynomial();
//return;
return; char ch; do
{
cout << "TEST MENU \n";
cout << "a testCTerm \n";
cout << "b testCPolynomial \n";
cin >> ch; switch (ch)
{
case 'a': testCTerm(); break;
case 'b': testCPolynomial(); break;
default: break;
}
}
while (ch!='0');
} |