Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************
- * file name: polynomial.cpp
- * programmer name: Sujeet Bhandari and Ryan Hughes
- * date created: 2/9/12
- * date of last revision: N/A
- * details of the revision: N/A
- * short description: polynomial
- **********************************************************/
- #include <iostream>
- #include <cstdlib>
- #include "polynomial.h"
- using namespace std;
- namespace sujeet
- {
- template <class Item>
- polynomial<Item>::polynomial() // Default Constructor - Create an empty dynamic array of size 10
- {
- size = 10;
- ptr = new Item[size];
- used = 0;
- }
- template <class Item>
- polynomial<Item>::polynomial(int capacity) // Normal Constructor - Create an empty dynamic array of the size "capacity"
- {
- ptr = new Item[capacity];
- size = capacity;
- used = 0;
- }
- template <class Item>
- polynomial<Item>::~polynomial() // Destructor - Delete the memory allocated by the pointer's array
- {
- delete []ptr;
- }
- template <class Item>
- void polynomial<Item>::reserve(int new_size) // reserve(int) - Allocate more space for the dynamic array
- {
- Item *array1;
- if(new_size == size)
- return;
- if(new_size < used)
- new_size = used;
- array1 = new Item[new_size];
- copy(ptr, ptr + used, array1);
- delete []ptr;
- ptr = array1;
- size = new_size;
- }
- template <class Item>
- void polynomial<Item>::insert(const Item& name) // insert(string&) - Add a string to the end of the array
- {
- if(used == size)
- reserve(used+1);
- ptr[used] = name;
- used++;
- }
- template <class Item>
- void polynomial<Item>::remove(int place) // remove(int) - Delete the item from the list at "place"
- {
- ptr[place-1] = ptr[used-1];
- used--;
- }
- template <class Item>
- void polynomial<Item>::clear() // clear() - Essentially clear the list by setting used as 0
- {
- used = 0;
- }
- template <class Item>
- void polynomial<Item>::display() const // display() - Display the current list of polynomial
- {
- if(used == 0)
- cout << "0\n";
- else
- {
- for(int i = 0; i < used; i++)
- {
- if(i == 0)
- {
- if(used == 1)
- cout << ptr[i] << "\n";
- else
- cout << ptr[i] << " + ";
- }
- else
- {
- if(i == used-1)
- cout << ptr[i] << "x^" << i << "\n";
- else
- cout << ptr[i] << "x^" << i << " + ";
- }
- }
- }
- }
- template <class Item>
- int polynomial<Item>::sizeDisplay() const // sizeDisplay() - Return the number of items in the list
- {
- return used;
- }
- template <class Item>
- Item polynomial<Item>::ptrDisplay(int place) const // ptrDisplay(int) - Return the string at the "place" position
- {
- return ptr[place];
- }
- template <class Item>
- polynomial<Item> operator +(const polynomial<Item>& q1, const polynomial<Item>& q2) // Overloaded Operator to add Polynomials
- {
- polynomial<Item> add;
- int max;
- if(q1.count > q2.count)
- max = q1.count;
- else
- max = q2.count;
- add.reset();
- for(int i=0; i < max; i++)
- {
- add.setPolynomial(q1.quad[i] + q2.quad[i], i);
- }
- return(add);
- }
- template <class Item>
- polynomial<Item> operator *(Item r, const polynomial<Item>& q) // Overloaded operator to multiply Polynomial by a constant
- {
- polynomial<Item> multi;
- multi.reset();
- for(int i=0; i < q.count; i++)
- {
- multi.setPolynomial(q.quad[i] * r,i);
- }
- return(multi);
- }
- }
Add Comment
Please, Sign In to add comment