Advertisement
KanadeTachibana

Hu No More Please Part 1

Feb 3rd, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.80 KB | None | 0 0
  1. //         Course: CS3420-60 Data Structures
  2. //           Name: Kendrick, Jessica
  3. //     Assignment: P1.1
  4. //  Date assigned: 01/23/2020
  5. //       Date due: 02/04/2020
  6. // Date handed in: 02/04/2020
  7. //         Remark: The program defines a class container which holds an array and contains functions
  8. //                 to manipulate it. An example container ex is made. Five values are put into the
  9. //                 back and then deleted to test the validity of the functions.
  10.  
  11.  
  12. #include "stdafx.h"
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <numeric>
  16.  
  17. using namespace std;
  18.  
  19. class container
  20. {
  21.     friend ostream& operator<<(ostream& out, container &cobj);
  22.     // Postcondition: display the contents of container object cobj in the format shown in the below sample outputs
  23. public:
  24.     container();
  25.     // Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
  26.     bool isEmpty();
  27.     // Postcondition: returns true if nothing is stored in the container; returns false otherwise
  28.     bool isFull();
  29.     // Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
  30.     int size();
  31.     // Postcondition: returns the “size” which is the actual number of elements currently stored in the containe robject; size <= capacity
  32.     int capacity();
  33.     // Postcondition: returns the storage capacity of the container.
  34.     bool insertBack(int val);
  35.     //  Precondition: the container object is not full
  36.     // 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
  37.     //                 Otherwise, returns false; the value is not inserted and program execution continues.
  38.     bool deleteBack();
  39.     //  Precondition: The array must not be empty
  40.     // Postcondition: the last element stored in the array is logically removed and the size of the is decremented by 1
  41.     bool insertFront(int val);
  42.     //  Precondition: the container is not full
  43.     // 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
  44.     //                 existing elements are shifted one step to the right.  Otherwise, returns false; program execution continues
  45.     bool deleteFront();
  46.     //  Precondition: the container must not be empty
  47.     // Postcondition: returns true and the front element is deleted; and n is decremented by 1;
  48.     //                 returns false otherwise; also, arr[1]... arr[size-1] of the container object is shifted one step left to close the gap.
  49. private:
  50.     static const int CAPACITY = 10;     // physical size of the arr array or the storage capacity of a container object
  51.     int arr[CAPACITY];                  // arr array can store up to CAPACITY  (10 in our case) integers
  52.     int n;                              // n is used as the subscript for the arr array. n is initialized to -1 for an empty array
  53.                                         // Each time a new value is inserted into the arr array, n must first be incremented
  54.                                         // by 1. Since n has been initialized to -1, the first inserted value is stored in arr[0],
  55.                                         // and the 2nd inserted value will be in arr[1], etc.  and the nth inserted value will be
  56.                                         // stored in arr[n – 1]. Obviously, n + 1 represents the actual number of elements
  57.                                         // stored in the array after n rounds of insertion.        
  58. };
  59.  
  60. // Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
  61. container::container() {
  62.     n = -1;
  63.     fill_n(arr, 10, 0);
  64. }
  65.  
  66. // Postcondition: returns true if nothing is stored in the container; returns false otherwise
  67. bool container::isEmpty() {
  68.     if (size() == 0) {
  69.         return true;
  70.     }
  71.     return false;
  72. }
  73.  
  74. // Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
  75. bool container::isFull() {
  76.     if (size() == CAPACITY) {
  77.         return true;
  78.     }
  79.     return false;
  80. }
  81.  
  82. // Postcondition: returns the “size” which is the actual number of elements currently stored in the containe robject; size <= capacity
  83. int container::size() {
  84.     return (n + 1);
  85. }
  86.  
  87. // Postcondition: returns the storage capacity of the container.
  88. int container::capacity() {
  89.     return CAPACITY;
  90. }
  91.  
  92. //  Precondition: the container object is not full
  93. // 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
  94. //                 Otherwise, returns false; the value is not inserted and program execution continues.
  95. bool container::insertBack(int val) {
  96.     if (!isFull()) {
  97.         n++;
  98.         arr[n] = val;
  99.         return true;
  100.     }
  101.     return false;
  102. }
  103.  
  104. //  Precondition: The array must not be empty
  105. // Postcondition: the last element stored in the array is logically removed and the size of the is decremented by 1
  106. bool container::deleteBack() {
  107.     if (!isEmpty()) {
  108.         arr[n] = 0;
  109.         n--;
  110.         return true;
  111.     }
  112.     return false;
  113. }
  114.  
  115. //  Precondition: the container is not full
  116. // 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
  117. //                 existing elements are shifted one step to the right.  Otherwise, returns false; program execution continues
  118. bool container::insertFront(int val) {
  119.     if (!isFull()) {
  120.         memmove(arr + 1, arr, size());
  121.         arr[0] = val;
  122.         n++;
  123.         return true;
  124.     }
  125.     return false;
  126. }
  127.  
  128. //  Precondition: the container must not be empty
  129. // Postcondition: returns true and the front element is deleted; and n is decremented by 1;
  130. //                 returns false otherwise; also, arr[1]... arr[size-1] of the container object is shifted one step left to close the gap.
  131. bool container::deleteFront() {
  132.     if (!isEmpty()) {
  133.         arr[0] = 0;
  134.         n--;
  135.         memmove(arr, arr + 1, size());
  136.         return true;
  137.     }
  138.     return false;
  139. }
  140.  
  141. // Postcondition: display the contents of container object cobj in the format shown in the below sample outputs
  142. ostream& operator<<(ostream& out, container &cobj) {
  143.     out << "Container storage capacity = " << cobj.CAPACITY
  144.         << "\nCurrently, container contains " << cobj.size() << " elements."
  145.         << "\nThe contents of the container:\n";
  146.     if (cobj.isEmpty()) {
  147.         out << "*** Container is currently empty!";
  148.     }
  149.     else {
  150.         for (int i = 0; i < cobj.size(); ++i) {
  151.             out << cobj.arr[i] << "\t";
  152.         }
  153.         out << "\n\n";
  154.     }
  155.     return out;
  156. }
  157.  
  158. int main() {
  159.     container ex;
  160.     cout << ex;
  161.  
  162.     cout << "\n\nThe container object is empty!"
  163.         << "\nCurrently, the container object contains 0 elemet(s) or value(s)"
  164.         << "\n\nWe now insert 5 values at the back of the array, one at a time:\n";
  165.     for (int i = 1; i < 6; ++i) {
  166.         ex.insertBack(i * 10);
  167.         cout << ex;
  168.     }
  169.  
  170.     cout << "We now delete all 5 values from the back of the array, one at a time:\n";
  171.     for (int i = 0; i < 5; ++i) {
  172.         ex.deleteBack();
  173.         cout << ex;
  174.     }
  175.  
  176.     cout << "\n\nWe try to delete from an empty container:";
  177.     ex.deleteBack();
  178.     cout << "\nCannot delete from an empty container!\n";
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement