Advertisement
KanadeTachibana

3420.3

Mar 5th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1. // Course: CS3420-01 Data Structure, Fall 2019
  2. // Name:
  3. // Assignment: Programming assignment 3
  4. // Date assigned: 9/23/19
  5. // Date due: 10/1/19
  6. // Date handed in: 10/1/19
  7.  
  8. #include "stdafx.h"
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <numeric>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16. template <class T>
  17. class container
  18. {
  19. template <class T>
  20. friend ostream& operator<<(ostream& out, container<T> &cobj);
  21. // Postcondition: display the contents of container object cobj in the format shown in the below sample outputs
  22. public:
  23. container();
  24. // Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
  25. bool isEmpty();
  26. // Postcondition: returns true if nothing is stored in the container; returns false otherwise
  27. bool isFull();
  28. // Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
  29. int size();
  30. // Postcondition: returns the “size” which is the actual number of elements currently stored in the containe robject; size <= capacity
  31. int capacity();
  32. // Postcondition: returns the storage capacity of the container.
  33. bool insertBack(T val);
  34. // Precondition: the container object is not full
  35. // Postcondition: if arr array is not full, n is incremented by 1; returns true with val is inserted at the end of the arr array
  36. // Otherwise, returns false; the value is not inserted and program execution continues.
  37. bool deleteBack();
  38. // Precondition: The array must not be empty
  39. // Postcondition: the last element stored in the array is logically removed and the size of the is decremented by 1
  40. bool insertFront(T val);
  41. // Precondition: the container is not full
  42. // Postcondition: returns true if the container is not full and val is inserted at the front of the array; n is incremented by 1; and all
  43. // existing elements are shifted one step to the right. Otherwise, returns false; program execution continues
  44. bool deleteFront();
  45. // Precondition: the container must not be empty
  46. // Postcondition: returns true and the front element is deleted; and n is decremented by 1;
  47. // returns false otherwise; also, arr[1]... arr[size-1] of the container object is shifted one step left to close the gap.
  48. void clear();
  49. // Postcondition: all elements or values stored in the Stack are logically //removed and the Stack is empty.
  50. // Added to produce similar output.
  51.  
  52. private:
  53. static const int CAPACITY = 10; // physical size of the arr array or the storage capacity of a container object
  54. T arr[CAPACITY]; // arr array can store up to CAPACITY (10 in our case) integers
  55. int n; // n is used as the subscript for the arr array. n is initialized to -1 for an empty array
  56. // Each time a new value is inserted into the arr array, n must first be incremented
  57. // by 1. Since n has been initialized to -1, the first inserted value is stored in arr[0],
  58. // and the 2nd inserted value will be in arr[1], etc. and the nth inserted value will be
  59. // stored in arr[n – 1]. Obviously, n + 1 represents the actual number of elements
  60. // stored in the array after n rounds of insertion.
  61. };
  62.  
  63. // Class object to hold a Date value.
  64. class date
  65. {
  66. friend ostream& operator<<(ostream &out, date &);
  67. public:
  68. date();
  69. date(int mm, int dd, int yyyy);
  70. bool operator>(date &dobj2);
  71. // Postcondition: returns true if *this > dobj2; returns false otherwise
  72. bool operator==(date &dobj2);
  73. private:
  74. int month;
  75. int day;
  76. int year;
  77. };
  78.  
  79.  
  80. // Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
  81. template <class T>
  82. container<T>::container() {
  83. n = -1;
  84. T arr = {};
  85. }
  86.  
  87. // Postcondition: returns true if nothing is stored in the container; returns false otherwise
  88. template <class T>
  89. bool container<T>::isEmpty() {
  90. if (size() == 0) {
  91. return true;
  92. }
  93. return false;
  94. }
  95.  
  96. // Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
  97. template <class T>
  98. bool container<T>::isFull() {
  99. if (size() == CAPACITY) {
  100. return true;
  101. }
  102. return false;
  103. }
  104.  
  105. // Postcondition: returns the “size” which is the actual number of elements currently stored in the containe robject; size <= capacity
  106. template <class T>
  107. int container<T>::size() {
  108. return (n + 1);
  109. }
  110.  
  111. // Postcondition: returns the storage capacity of the container.
  112. template <class T>
  113. int container<T>::capacity() {
  114. return CAPACITY;
  115. }
  116.  
  117. // Precondition: the container object is not full
  118. // Postcondition: if arr array is not full, n is incremented by 1; returns true with val is inserted at the end of the arr array
  119. // Otherwise, returns false; the value is not inserted and program execution continues.
  120. template <class T>
  121. bool container<T>::insertBack(T val) {
  122. if (!isFull()) {
  123. n++;
  124. arr[n] = val;
  125. return true;
  126. }
  127. cout << "\nCannot insert. Container is full.\n";
  128. return false;
  129. }
  130.  
  131. // Precondition: The array must not be empty
  132. // Postcondition: the last element stored in the array is logically removed and the size of the is decremented by 1
  133. template <class T>
  134. bool container<T>::deleteBack() {
  135. if (!isEmpty()) {
  136. n--;
  137. return true;
  138. }
  139. cout << "\nCannot delete. Container is empty.\n";
  140. return false;
  141. }
  142.  
  143. // Precondition: the container is not full
  144. // Postcondition: returns true if the container is not full and val is inserted at the front of the array; n is incremented by 1; and all
  145. // existing elements are shifted one step to the right. Otherwise, returns false; program execution continues
  146. template <class T>
  147. bool container<T>::insertFront(T val) {
  148. if (!isFull()) {
  149. memmove(arr + 1, arr, size());
  150. arr[0] = val;
  151. n++;
  152. return true;
  153. }
  154. cout << "\nCannot insert. Container is full.\n";
  155. return false;
  156. }
  157.  
  158. // Precondition: the container must not be empty
  159. // Postcondition: returns true and the front element is deleted; and n is decremented by 1;
  160. // returns false otherwise; also, arr[1]... arr[size-1] of the container object is shifted one step left to close the gap.
  161. template <class T>
  162. bool container<T>::deleteFront() {
  163. if (!isEmpty()) {
  164. n--;
  165. memmove(arr, arr + 1, size());
  166. return true;
  167. }
  168. cout << "\nCannot delete. Container is empty.\n";
  169. return false;
  170. }
  171.  
  172. template <class T>
  173. void container<T>::clear() {
  174. n = -1;
  175. }
  176.  
  177. // Initialize date to 01/01/1960
  178. date::date() {
  179. month = 01;
  180. day = 01;
  181. year = 1960;
  182. }
  183.  
  184. // Initializes date to mm/dd/yyyy
  185. date::date(int mm, int dd, int yyyy) {
  186. month = mm;
  187. day = dd;
  188. year = yyyy;
  189. }
  190.  
  191. // Overloaded > operator for date
  192. bool date::operator>(date &dobj2) {
  193. if (year > dobj2.year) {
  194. return true;
  195. }
  196. else if ((year == dobj2.year) && (month > dobj2.month)) {
  197. return true;
  198. }
  199. else if ((year == dobj2.year) && (month == dobj2.month) && (day > dobj2.day)) {
  200. return true;
  201. }
  202. return false;
  203. }
  204.  
  205. // Overloaded == operator for date.
  206. bool date::operator==(date &dobj2) {
  207. if ((day == dobj2.day) && (month == dobj2.month) && (year == dobj2.year)) {
  208. return true;
  209. }
  210. return false;
  211. }
  212.  
  213. // Postcondition: display the contents of container object cobj in the format shown in the below sample outputs
  214. template <class T>
  215. ostream& operator<<(ostream& out, container<T> &cobj) {
  216. out << "Container storage capacity = " << cobj.capacity()
  217. << "\nCurrently, container contains " << cobj.size() << " elements."
  218. << "\nThe contents of the container:\n";
  219. if (cobj.isEmpty()) {
  220. out << "*** Container is currently empty!";
  221. }
  222. else {
  223. for (int i = 0; i < cobj.size(); ++i) {
  224. out << cobj.arr[i] << "\t";
  225. }
  226. out << "\n\n";
  227. }
  228. return out;
  229. }
  230.  
  231. // Modified << operator to output date
  232. ostream& operator<<(ostream& out, date& dobj) {
  233. cout << dobj.day << "/" << dobj.month << "/" << dobj.year << endl;
  234. return out;
  235. }
  236.  
  237. int main() {
  238.  
  239. //
  240. // Part 1. Store doubles
  241. //
  242. container<double> dcont1;
  243. cout << dcont1;
  244.  
  245. // Using insertBack() to store 10 data members
  246. for (double i = 10.21; i < 20.21; ++i) {
  247. dcont1.insertBack(i);
  248. }
  249. cout << dcont1;
  250.  
  251. // Attempt to insert to full container
  252. dcont1.insertBack(21.10);
  253.  
  254. // Calling deleteBack()
  255. dcont1.deleteBack();
  256. cout << dcont1;
  257.  
  258. // Insert Again
  259. dcont1.insertBack(21.21);
  260. cout << dcont1;
  261.  
  262. // Calling clear()
  263. dcont1.clear();
  264. cout << dcont1;
  265.  
  266. // Call insertFront() to store 9 data members
  267. for (double i = 10.21; i < 19.21; ++i) {
  268. dcont1.insertFront(i);
  269. }
  270. cout << dcont1;
  271.  
  272. // Call 1 more insert Front
  273. dcont1.insertFront(20.21);
  274. cout << dcont1;
  275.  
  276. // Call deleteFront()
  277. dcont1.deleteFront();
  278. cout << dcont1;
  279.  
  280. // Calling clear()
  281. dcont1.clear();
  282. cout << dcont1;
  283.  
  284. // Call deleteFront() on empty array
  285. dcont1.deleteFront();
  286. cout << dcont1;
  287.  
  288. // =================================================
  289.  
  290. //
  291. // Part 2. Store strings
  292. //
  293. container<string> scont1;
  294. cout << scont1;
  295.  
  296. // Using insertBack() to store 10 data members
  297. scont1.insertBack("aa");
  298. scont1.insertBack("bb");
  299. scont1.insertBack("cc");
  300. scont1.insertBack("dd");
  301. scont1.insertBack("ee");
  302. scont1.insertBack("ff");
  303. scont1.insertBack("gg");
  304. scont1.insertBack("hh");
  305. scont1.insertBack("ii");
  306. scont1.insertBack("jj");
  307. cout << scont1;
  308.  
  309. // Attempt to insert to full container
  310. scont1.insertBack("kk");
  311.  
  312. // Calling deleteBack()
  313. scont1.deleteBack();
  314. cout << scont1;
  315.  
  316. // Insert Again
  317. scont1.insertBack("kk");
  318. cout << scont1;
  319.  
  320. // Calling clear()
  321. scont1.clear();
  322. cout << scont1;
  323.  
  324. // Call insertFront() to store 9 data members
  325. scont1.insertFront("aa");
  326. scont1.insertFront("bb");
  327. scont1.insertFront("cc");
  328. scont1.insertFront("dd");
  329. scont1.insertFront("ee");
  330. scont1.insertFront("ff");
  331. scont1.insertFront("gg");
  332. scont1.insertFront("hh");
  333. scont1.insertFront("ii");
  334. cout << scont1;
  335.  
  336. // Call 1 more insert Front
  337. scont1.insertFront("jj");
  338. cout << scont1;
  339.  
  340. // Call deleteFront()
  341. scont1.deleteFront();
  342. cout << scont1;
  343.  
  344. // Calling clear()
  345. scont1.clear();
  346. cout << scont1;
  347.  
  348. // Call deleteFront() on empty array
  349. scont1.deleteFront();
  350. cout << scont1;
  351.  
  352. // =================================
  353.  
  354. //
  355. // Part 3. Store Date
  356. //
  357. container<date> dacont1;
  358. cout << dacont1;
  359.  
  360. // Using insertBack() to store 10 data members
  361. for (int i = 1; i < 10; ++i) {
  362. dacont1.insertBack(date(i, i, (2000 + i)));
  363. }
  364. cout << dacont1;
  365.  
  366. // Attempt to insert to full container
  367. dacont1.insertBack(date(10, 10, 2010));
  368.  
  369. // Calling deleteBack()
  370. dacont1.deleteBack();
  371. cout << dacont1;
  372.  
  373. // Insert Again
  374. dacont1.insertBack(date(10, 10, 2010));
  375. cout << dacont1;
  376.  
  377. // Calling clear()
  378. dacont1.clear();
  379. cout << dacont1;
  380.  
  381. // Call insertFront() to store 9 data members
  382. for (int i = 1; i < 9; ++i) {
  383. dacont1.insertFront(date(i, i, (2000 + i)));
  384. }
  385. cout << dacont1;
  386.  
  387. // Call 1 more insert Front
  388. dacont1.insertFront(date(10, 10, 2010));
  389. cout << dacont1;
  390.  
  391. // Call deleteFront()
  392. dacont1.deleteFront();
  393. cout << dacont1;
  394.  
  395. // Calling clear()
  396. dacont1.clear();
  397. cout << dacont1;
  398.  
  399. // Call deleteFront() on empty array
  400. dacont1.deleteFront();
  401. cout << dacont1;
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement