Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Course: CS3420-01 Data Structure, Fall 2019
- // Name:
- // Assignment: Programming assignment 3
- // Date assigned: 9/23/19
- // Date due: 10/1/19
- // Date handed in: 10/1/19
- #include "stdafx.h"
- #include <iostream>
- #include <algorithm>
- #include <numeric>
- #include <string>
- using namespace std;
- template <class T>
- class container
- {
- template <class T>
- friend ostream& operator<<(ostream& out, container<T> &cobj);
- // Postcondition: display the contents of container object cobj in the format shown in the below sample outputs
- public:
- container();
- // Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
- bool isEmpty();
- // Postcondition: returns true if nothing is stored in the container; returns false otherwise
- bool isFull();
- // Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
- int size();
- // Postcondition: returns the “size” which is the actual number of elements currently stored in the containe robject; size <= capacity
- int capacity();
- // Postcondition: returns the storage capacity of the container.
- bool insertBack(T val);
- // Precondition: the container object is not full
- // 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
- // Otherwise, returns false; the value is not inserted and program execution continues.
- bool deleteBack();
- // Precondition: The array must not be empty
- // Postcondition: the last element stored in the array is logically removed and the size of the is decremented by 1
- bool insertFront(T val);
- // Precondition: the container is not full
- // 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
- // existing elements are shifted one step to the right. Otherwise, returns false; program execution continues
- bool deleteFront();
- // Precondition: the container must not be empty
- // Postcondition: returns true and the front element is deleted; and n is decremented by 1;
- // returns false otherwise; also, arr[1]... arr[size-1] of the container object is shifted one step left to close the gap.
- void clear();
- // Postcondition: all elements or values stored in the Stack are logically //removed and the Stack is empty.
- // Added to produce similar output.
- private:
- static const int CAPACITY = 10; // physical size of the arr array or the storage capacity of a container object
- T arr[CAPACITY]; // arr array can store up to CAPACITY (10 in our case) integers
- int n; // n is used as the subscript for the arr array. n is initialized to -1 for an empty array
- // Each time a new value is inserted into the arr array, n must first be incremented
- // by 1. Since n has been initialized to -1, the first inserted value is stored in arr[0],
- // and the 2nd inserted value will be in arr[1], etc. and the nth inserted value will be
- // stored in arr[n – 1]. Obviously, n + 1 represents the actual number of elements
- // stored in the array after n rounds of insertion.
- };
- // Class object to hold a Date value.
- class date
- {
- friend ostream& operator<<(ostream &out, date &);
- public:
- date();
- date(int mm, int dd, int yyyy);
- bool operator>(date &dobj2);
- // Postcondition: returns true if *this > dobj2; returns false otherwise
- bool operator==(date &dobj2);
- private:
- int month;
- int day;
- int year;
- };
- // Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
- template <class T>
- container<T>::container() {
- n = -1;
- T arr = {};
- }
- // Postcondition: returns true if nothing is stored in the container; returns false otherwise
- template <class T>
- bool container<T>::isEmpty() {
- if (size() == 0) {
- return true;
- }
- return false;
- }
- // Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
- template <class T>
- bool container<T>::isFull() {
- if (size() == CAPACITY) {
- return true;
- }
- return false;
- }
- // Postcondition: returns the “size” which is the actual number of elements currently stored in the containe robject; size <= capacity
- template <class T>
- int container<T>::size() {
- return (n + 1);
- }
- // Postcondition: returns the storage capacity of the container.
- template <class T>
- int container<T>::capacity() {
- return CAPACITY;
- }
- // Precondition: the container object is not full
- // 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
- // Otherwise, returns false; the value is not inserted and program execution continues.
- template <class T>
- bool container<T>::insertBack(T val) {
- if (!isFull()) {
- n++;
- arr[n] = val;
- return true;
- }
- cout << "\nCannot insert. Container is full.\n";
- return false;
- }
- // Precondition: The array must not be empty
- // Postcondition: the last element stored in the array is logically removed and the size of the is decremented by 1
- template <class T>
- bool container<T>::deleteBack() {
- if (!isEmpty()) {
- n--;
- return true;
- }
- cout << "\nCannot delete. Container is empty.\n";
- return false;
- }
- // Precondition: the container is not full
- // 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
- // existing elements are shifted one step to the right. Otherwise, returns false; program execution continues
- template <class T>
- bool container<T>::insertFront(T val) {
- if (!isFull()) {
- memmove(arr + 1, arr, size());
- arr[0] = val;
- n++;
- return true;
- }
- cout << "\nCannot insert. Container is full.\n";
- return false;
- }
- // Precondition: the container must not be empty
- // Postcondition: returns true and the front element is deleted; and n is decremented by 1;
- // returns false otherwise; also, arr[1]... arr[size-1] of the container object is shifted one step left to close the gap.
- template <class T>
- bool container<T>::deleteFront() {
- if (!isEmpty()) {
- n--;
- memmove(arr, arr + 1, size());
- return true;
- }
- cout << "\nCannot delete. Container is empty.\n";
- return false;
- }
- template <class T>
- void container<T>::clear() {
- n = -1;
- }
- // Initialize date to 01/01/1960
- date::date() {
- month = 01;
- day = 01;
- year = 1960;
- }
- // Initializes date to mm/dd/yyyy
- date::date(int mm, int dd, int yyyy) {
- month = mm;
- day = dd;
- year = yyyy;
- }
- // Overloaded > operator for date
- bool date::operator>(date &dobj2) {
- if (year > dobj2.year) {
- return true;
- }
- else if ((year == dobj2.year) && (month > dobj2.month)) {
- return true;
- }
- else if ((year == dobj2.year) && (month == dobj2.month) && (day > dobj2.day)) {
- return true;
- }
- return false;
- }
- // Overloaded == operator for date.
- bool date::operator==(date &dobj2) {
- if ((day == dobj2.day) && (month == dobj2.month) && (year == dobj2.year)) {
- return true;
- }
- return false;
- }
- // Postcondition: display the contents of container object cobj in the format shown in the below sample outputs
- template <class T>
- ostream& operator<<(ostream& out, container<T> &cobj) {
- out << "Container storage capacity = " << cobj.capacity()
- << "\nCurrently, container contains " << cobj.size() << " elements."
- << "\nThe contents of the container:\n";
- if (cobj.isEmpty()) {
- out << "*** Container is currently empty!";
- }
- else {
- for (int i = 0; i < cobj.size(); ++i) {
- out << cobj.arr[i] << "\t";
- }
- out << "\n\n";
- }
- return out;
- }
- // Modified << operator to output date
- ostream& operator<<(ostream& out, date& dobj) {
- cout << dobj.day << "/" << dobj.month << "/" << dobj.year << endl;
- return out;
- }
- int main() {
- //
- // Part 1. Store doubles
- //
- container<double> dcont1;
- cout << dcont1;
- // Using insertBack() to store 10 data members
- for (double i = 10.21; i < 20.21; ++i) {
- dcont1.insertBack(i);
- }
- cout << dcont1;
- // Attempt to insert to full container
- dcont1.insertBack(21.10);
- // Calling deleteBack()
- dcont1.deleteBack();
- cout << dcont1;
- // Insert Again
- dcont1.insertBack(21.21);
- cout << dcont1;
- // Calling clear()
- dcont1.clear();
- cout << dcont1;
- // Call insertFront() to store 9 data members
- for (double i = 10.21; i < 19.21; ++i) {
- dcont1.insertFront(i);
- }
- cout << dcont1;
- // Call 1 more insert Front
- dcont1.insertFront(20.21);
- cout << dcont1;
- // Call deleteFront()
- dcont1.deleteFront();
- cout << dcont1;
- // Calling clear()
- dcont1.clear();
- cout << dcont1;
- // Call deleteFront() on empty array
- dcont1.deleteFront();
- cout << dcont1;
- // =================================================
- //
- // Part 2. Store strings
- //
- container<string> scont1;
- cout << scont1;
- // Using insertBack() to store 10 data members
- scont1.insertBack("aa");
- scont1.insertBack("bb");
- scont1.insertBack("cc");
- scont1.insertBack("dd");
- scont1.insertBack("ee");
- scont1.insertBack("ff");
- scont1.insertBack("gg");
- scont1.insertBack("hh");
- scont1.insertBack("ii");
- scont1.insertBack("jj");
- cout << scont1;
- // Attempt to insert to full container
- scont1.insertBack("kk");
- // Calling deleteBack()
- scont1.deleteBack();
- cout << scont1;
- // Insert Again
- scont1.insertBack("kk");
- cout << scont1;
- // Calling clear()
- scont1.clear();
- cout << scont1;
- // Call insertFront() to store 9 data members
- scont1.insertFront("aa");
- scont1.insertFront("bb");
- scont1.insertFront("cc");
- scont1.insertFront("dd");
- scont1.insertFront("ee");
- scont1.insertFront("ff");
- scont1.insertFront("gg");
- scont1.insertFront("hh");
- scont1.insertFront("ii");
- cout << scont1;
- // Call 1 more insert Front
- scont1.insertFront("jj");
- cout << scont1;
- // Call deleteFront()
- scont1.deleteFront();
- cout << scont1;
- // Calling clear()
- scont1.clear();
- cout << scont1;
- // Call deleteFront() on empty array
- scont1.deleteFront();
- cout << scont1;
- // =================================
- //
- // Part 3. Store Date
- //
- container<date> dacont1;
- cout << dacont1;
- // Using insertBack() to store 10 data members
- for (int i = 1; i < 10; ++i) {
- dacont1.insertBack(date(i, i, (2000 + i)));
- }
- cout << dacont1;
- // Attempt to insert to full container
- dacont1.insertBack(date(10, 10, 2010));
- // Calling deleteBack()
- dacont1.deleteBack();
- cout << dacont1;
- // Insert Again
- dacont1.insertBack(date(10, 10, 2010));
- cout << dacont1;
- // Calling clear()
- dacont1.clear();
- cout << dacont1;
- // Call insertFront() to store 9 data members
- for (int i = 1; i < 9; ++i) {
- dacont1.insertFront(date(i, i, (2000 + i)));
- }
- cout << dacont1;
- // Call 1 more insert Front
- dacont1.insertFront(date(10, 10, 2010));
- cout << dacont1;
- // Call deleteFront()
- dacont1.deleteFront();
- cout << dacont1;
- // Calling clear()
- dacont1.clear();
- cout << dacont1;
- // Call deleteFront() on empty array
- dacont1.deleteFront();
- cout << dacont1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement