//Mashael al-Besher ID#:120057204
//Noaf Al-Namlah
ID#:120057183
//CS210PROJECT<<1>>
//header file for class List
#include <iostream>
using namespace std ;
#ifndef LIST
#define LIST
typedef int ElementType;
class List
{
public:
List(int maxSize = 1024);
~List();
List(const
List & origList);
List & operator=(const List & origList);
bool empty() const;
void
insert(ElementType item, int
pos);
void
erase(int pos);
void
display(ostream & out) const;
ElementType
retrieve (int pos) ;
private:
int mySize;
int myCapacity;
ElementType
* myArrayPtr;
};
ostream &
operator<< (ostream & out,const List & aList);
#endif
// implemenation file for class
List
#include <cassert>
#include <new>
using namespace std;
#include "List.h"
List::List(int maxSize)
: mySize(0), myCapacity(maxSize)
{
myArrayPtr = new(nothrow) ElementType[maxSize];
assert(myArrayPtr != 0);
}
List::~List()
{
delete
[] myArrayPtr;
}
List::List(const List & origList)
: mySize(origList.mySize), myCapacity(origList.myCapacity)
{
myArrayPtr = new(nothrow) ElementType[myCapacity];
if
(myArrayPtr != 0) // check if memory available
for(int i
= 0; i < myCapacity; i++)
myArrayPtr[i] = origList.myArrayPtr[i];
else
{
cerr << "*Inadequate
memory to allocate stack ***\n";
exit(1);
}
}
List & List::operator=(const List & origList)
{
if
(this != &origList)
{
mySize = origList.mySize;
myCapacity = origList.myCapacity;
if (myCapacity != origList.myCapacity)
{
delete[] myArrayPtr;
myArrayPtr = new(nothrow) ElementType[myCapacity];
if (myArrayPtr == 0)
{
cerr << "*Inadequate
memory to allocate stack ***\n";
exit(1);
}
}
for(int i
= 0; i < myCapacity; i++)
myArrayPtr[i] = origList.myArrayPtr[i];
}
return
*this;
}
bool List::empty() const
{
return
mySize == 0;
}
void List::display(ostream & out) const
{
for
(int i = 0; i < mySize; i++)
out << myArrayPtr[i] << "
";
}
ostream &
operator<< (ostream & out, const List &
aList)
{
aList.display(out);
return
out;
}
void List::insert(ElementType item, int pos)
{
if
(mySize == myCapacity)
{
cerr << "*** No space
for list element -- terminating "
"execution ***\n";
exit(1);
}
if
(pos < 0 || pos > mySize)
{
cerr << "*** Illegal
location to insert -- " << pos
<< ". List unchanged. ***\n";
return;
}
for(int i = mySize;
i > pos; i--)
myArrayPtr[i] = myArrayPtr[i - 1];
myArrayPtr[pos] = item;
mySize++;
}
void List::erase(int pos)
{
if
(mySize == 0)
{
cerr << "*** List is
empty ***\n";
return;
}
if
(pos < 0 || pos >= mySize)
{
cerr << "Illegal
location to delete -- " << pos
<< ". List unchanged. ***\n";
return;
}
for(int i = pos; i
< mySize; i++)
myArrayPtr[i] = myArrayPtr[i + 1];
mySize--;
}
ElementType List::retrieve
(int pos)
{
if
(mySize == 0)
{
cerr << "*** List is
empty ***\n";
return 0;
}
if
(pos < 0 || pos >= mySize)
{
cerr << "Illegal
location to delete -- " << pos
<< ". List unchanged. ***\n";
return 0;
}
return
myArrayPtr[pos] ;
}
//-------------------------------------------------------------
// Header file for class POLY
#include <iostream>
using namespace std ;
#ifndef POLY
#define POLY
typedef int ElementType;
class polynomial
{
int degree;
List Coeff;
public :
polynomial()
;
void
input() ;
void
output(ostream & out) ;
double
evaluation ( int x ) ;
void addition(polynomial & ) ;
};
ostream &
operator<< (ostream & out,polynomial
& P ) ;
#endif
//-----------------------------------------------
//Implemenation file
for class POLY
#include <iostream>
#include <cmath>
using namespace std;
#include "List.h"
#include "Poly.h"
polynomial::polynomial()
{
degree
= 0 ;
}
ostream &
operator<< (ostream & out,polynomial
& aPoly)
{
aPoly.output(out);
return
out;
}
void polynomial::input()
{
cout<<"\nPlease Insert the Degree of the Polynomial : " ;
cin>>degree ;
ElementType num ;
for( int i = 0 ; i<=
degree ; i++ )
{
cout<<"\nPlease Insert the Coefficient of X^("<<i<<") :
" ;
cin>>num ;
//item, pos
Coeff.insert ( num ,i ) ;
}
}
void polynomial::output(ostream & out)
{
out<<"\nThe Degree is : "<< degree <<"\n";
for
(int i = 0; i <= degree; i++)
{
out << Coeff.retrieve(i) << "X^"<<i
;
if(i< degree ) out<<" + " ;
else out<<" = 0 " ;
}
}
double polynomial:: evaluation ( int x )
{
int i ;
double result = 0 ;
for ( i = 0 ; i<= degree ; i++ )
result = result + ( Coeff.retrieve(i) * pow(x,i)
) ;
return result ;
}
void
polynomial:: addition(polynomial & T )
{
polynomial
Res ;
//T.input()
;
int Min ;
ElementType
num ;
if(
T.degree <
degree )
Min = T.degree ;
else
Min = degree ;
cout<<"\nMin
degree is : "<<Min <<"\n" ;
for(
int i = 0 ; i<=Min ; i++ )
{
num = T.Coeff.retrieve(i) + this->Coeff.retrieve(i) ;
this->Coeff.erase( i ) ;
this->Coeff.insert(num , i ) ;
}
}
//-------------------------------------------------
//Application file
#include <iostream>
using namespace std;
#include "List.h"
#include "Poly.h"
int menu() ;
void f(List aList)
{
for
(int i = 1; i < 5; i++)
{
aList.insert(100*i, i);
cout << aList
<< endl;
}
}
int main()
{
int ch , X ;
polynomial
P ;
ch = menu() ;
while( ch
!= 5 )
{
switch
( ch )
{
case
1 :
P.input() ;
break ;
case
2:
cout<<P
;
break ;
case
3:
{
polynomial T ;
T.input() ;
P.addition(T) ;
cout<<P ;
break ;
}
case
4:
cout<<"\nPlease inter the value of X : ";
cin>>X
;
cout<<"\n
P.evaluation ( "<<X<<" ) = " << P.evaluation
( X ) <<"\n" ;
break ;
}
ch = menu() ;
}
return
0 ;
}
int menu()
{
cout<<"\n================= MAIN MANU
================= \n";
cout<<"\n1.Reading a polynomial " ;
cout<<"\n2.Printing a polynomial " ;
cout<<"\n3.Adding two polynomials " ;
cout<<"\n4.Evaluating a polynomial " ;
cout<<"\n5.Exit " ;
cout<<"\n==================================================\n";
int ch ;
do{
cout<<"\nInter your Choice ? \n" ;
cin>>ch ;
if(
ch> 5 || ch< 0 ) cout<<"\nWrong value ,,,,
Try Again \n" ;
} while ( ch> 5 || ch<
0 ) ;
return ch
;
}