1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <iostream>
#include <iomanip>
#include <cstdlib> // for exit() function.
#include <ctime>
#include <string>
using namespace std;
class container {
public:
container();
// Postcondition: all data array elements are initialized to 0; count is set to -1
void insert(int value);
// Precondition: "data" array must not be full
// Postcondition: if the container is not full, insert the value data array (first available location from left) and increment count by 1.
// If the data array is full, display "Container is full; program is terminated!"
// and then terminates the program with exit(1) command.
void remove();
// Precondition: container must not be empty, i.e., "count" must be greater than or equal to 0.
// Postcondition: if data array is not empty, delete the most recently stored (rightmost) value in the container by decrementing
// count by 1 (logical deletion)
// if the container is empty display a message "Container is empty; program is terminated!"
// and then terminate the program with exit(1) system call.
void undelete();
// Postcondition: the most recently removed value is restored or undeleted
bool isEmpty();
// Postcondition: return true is container is empty; return false otherwise
bool isFull();
// Postcondition: return true is container is full; return false otherwise
void sort();
// Precondition: "data" array must not be empty and there must be at least two values stored in it.
// Postcondition: sort all values stored in the container in ascending order
friend ostream& operator<<(ostream& out, container &);
// Postcondition: displays all values currently stored in the contianer
private:
const static int CAPACITY = 10; // specifies storage capacity of a container object, the container
int data[CAPACITY]; // stores inserted integer values
int count; // (count + 1) indicates how many values have currently stored in data array
};
container::container()
{
count = -1; // container is empty
for (int i = 0; i < CAPACITY; i++)
data[i] = 0;
}
void container::undelete()
{
++count;
}
bool container::isEmpty()
{
return( count == -1 );
}
bool container::isFull()
{
return( count == CAPACITY -1 );
}
void container::insert(int value)
{
if (!isFull())
{
data[++count] = value;
cout << setw(4) << value << " has been inserted in data[" << count << "]." << endl;
}
else {
cout << "Attempts to insert a value into a full container; program is terminated!";
exit (1);
}
}
void container::remove() // logical removal
{
if( isEmpty( ) ) // the stack is empty
{
cout << endl << "Stack is empty" << endl;
exit( 1 );
}
--count;
}
void container::sort()
{
int pass, c, temp;
if (count >= 1) // sort only when the data array contains at least two elements
{
for (pass = 1; pass < count; pass++)
for (c = 0; c <= count - pass; c++)
if (data[c] > data[c + 1]) {
temp = data[c];
data[c] = data[c + 1];
data[c + 1] = temp;
}
}
}
ostream& operator<<(ostream& out, container & obj)
{
out << "The 'container' contains the following " << obj.count + 1 << " value(s):\n";
if (obj.count == -1)
out << "*** data array is empty!" << endl;
else
{
for (int i = 0; i <= count; i++)
out << obj.data[i] << '\t';
out << endl;
}
return out;
}
int main()
{
container c;
srand(static_cast<unsigned int>(time(0)));
cout << "We now insert 10 random integer values into an empty container:\n";
for (int i = 0; i < 10; i++)
c.insert(rand() % 99 + 1);
cout << "\nThe contents of the container are: " << endl;
cout<<c;
c.sort();
cout << "After sort: " << endl;
cout<<c;
cout << "We now remove all values in the container:\n";
for (int i = 0; i < 10; i++)
c.remove();
cout << "After all values in the container are removed, container contains: " << endl;
cout<<c;
for (int i = 0; i < 3; i++)
c.undelete();
cout << endl;
cout << "After executing undelete three times, container contains: " << endl;
cout<<c;
cout << endl << endl;
return 0;
}
|