Guest User

Untitled

a guest
Jul 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. /*********************************************************
  2. * file name: polynomial.cpp
  3. * programmer name: Sujeet Bhandari and Ryan Hughes
  4. * date created: 2/9/12
  5. * date of last revision: N/A
  6. * details of the revision: N/A
  7. * short description: polynomial
  8. **********************************************************/
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include "polynomial.h"
  12. using namespace std;
  13.  
  14. namespace sujeet
  15. {
  16. template <class Item>
  17. polynomial<Item>::polynomial() // Default Constructor - Create an empty dynamic array of size 10
  18. {
  19. size = 10;
  20. ptr = new Item[size];
  21. used = 0;
  22. }
  23.  
  24. template <class Item>
  25. polynomial<Item>::polynomial(int capacity) // Normal Constructor - Create an empty dynamic array of the size "capacity"
  26. {
  27. ptr = new Item[capacity];
  28. size = capacity;
  29. used = 0;
  30. }
  31.  
  32. template <class Item>
  33. polynomial<Item>::~polynomial() // Destructor - Delete the memory allocated by the pointer's array
  34. {
  35. delete []ptr;
  36. }
  37.  
  38. template <class Item>
  39. void polynomial<Item>::reserve(int new_size) // reserve(int) - Allocate more space for the dynamic array
  40. {
  41. Item *array1;
  42. if(new_size == size)
  43. return;
  44. if(new_size < used)
  45. new_size = used;
  46.  
  47. array1 = new Item[new_size];
  48. copy(ptr, ptr + used, array1);
  49. delete []ptr;
  50. ptr = array1;
  51. size = new_size;
  52. }
  53.  
  54. template <class Item>
  55. void polynomial<Item>::insert(const Item& name) // insert(string&) - Add a string to the end of the array
  56. {
  57. if(used == size)
  58. reserve(used+1);
  59. ptr[used] = name;
  60. used++;
  61. }
  62.  
  63. template <class Item>
  64. void polynomial<Item>::remove(int place) // remove(int) - Delete the item from the list at "place"
  65. {
  66. ptr[place-1] = ptr[used-1];
  67. used--;
  68. }
  69.  
  70. template <class Item>
  71. void polynomial<Item>::clear() // clear() - Essentially clear the list by setting used as 0
  72. {
  73. used = 0;
  74. }
  75.  
  76. template <class Item>
  77. void polynomial<Item>::display() const // display() - Display the current list of polynomial
  78. {
  79. if(used == 0)
  80. cout << "0\n";
  81. else
  82. {
  83. for(int i = 0; i < used; i++)
  84. {
  85. if(i == 0)
  86. {
  87. if(used == 1)
  88. cout << ptr[i] << "\n";
  89. else
  90. cout << ptr[i] << " + ";
  91. }
  92. else
  93. {
  94. if(i == used-1)
  95. cout << ptr[i] << "x^" << i << "\n";
  96. else
  97. cout << ptr[i] << "x^" << i << " + ";
  98. }
  99. }
  100. }
  101. }
  102.  
  103. template <class Item>
  104. int polynomial<Item>::sizeDisplay() const // sizeDisplay() - Return the number of items in the list
  105. {
  106. return used;
  107. }
  108.  
  109. template <class Item>
  110. Item polynomial<Item>::ptrDisplay(int place) const // ptrDisplay(int) - Return the string at the "place" position
  111. {
  112. return ptr[place];
  113. }
  114.  
  115. template <class Item>
  116. polynomial<Item> operator +(const polynomial<Item>& q1, const polynomial<Item>& q2) // Overloaded Operator to add Polynomials
  117. {
  118. polynomial<Item> add;
  119. int max;
  120. if(q1.count > q2.count)
  121. max = q1.count;
  122. else
  123. max = q2.count;
  124. add.reset();
  125. for(int i=0; i < max; i++)
  126. {
  127. add.setPolynomial(q1.quad[i] + q2.quad[i], i);
  128. }
  129. return(add);
  130. }
  131.  
  132. template <class Item>
  133. polynomial<Item> operator *(Item r, const polynomial<Item>& q) // Overloaded operator to multiply Polynomial by a constant
  134. {
  135. polynomial<Item> multi;
  136. multi.reset();
  137. for(int i=0; i < q.count; i++)
  138. {
  139. multi.setPolynomial(q.quad[i] * r,i);
  140. }
  141. return(multi);
  142. }
  143. }
Add Comment
Please, Sign In to add comment