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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
#ifndef CONTAINERL_H
#define CONTAINERL_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
template <class T>
class container
{
template <class U>
friend ostream& operator<<(ostream&, const container<U>&);
// Precondition: Receives an object by reference
// Postcondition: Displays the contents of the container
public:
container();
// Postcondition: all data array elements are initialized to 0;
// count is set to -1
void insert(T);
// Precondition: integer passed by call by value,
// "data" array must not be full
// Postcondition: if the container is not full,
// insert the value data array and increment count by 1.
void remove();
// Precondition: container must not be empty
// Postcondition: if data array is not empty, delete
// the most recently stored value
// by decrementing count by 1
void undelete();
// Precondition: the objects must have been deleted, or count decremented
// Postcondition: the most recently removed value is undeleted
bool isEmpty();
// Postcondition: if container is empty return true;
// otherwise return false
bool isFull();
// Postcondition: if container is full return true;
// otherwise return false
void sort();
// Precondition: "data" array must have at least
// two values in it
// Postcondition: sort values stored in ascending order
private:
const static int CAPACITY = 10;
T data[CAPACITY];
T count;
};
template <class T>
class rational
{
template <class U>
friend istream& operator>>(istream&, rational<U>&);
// User is prompted to enter numerator then denominator for a rational number
// Precondition: denominator of rational number must not be zero
// Postcondition: rational reference object passed to the function is filled with values entered by user
template <class U>
friend ostream& operator<<(ostream&, rational<U>&);
// Postcondition: displays the contents of the rational object passed to the function
// in the following format: a / b, e.g., 1 / 2, -5 /9 (not 5 / -9), 1 / 4 (not 2 / 8), etc.
// If the rational number is 1 / 1, display just 1. If the rational number is 0 / 5, etc., display just 0.
public:
rational();
// default constructor
// Postcondition: declared rational object is initialized to 1 (i.e., 1 / 1)
rational(T aa, T bb);
// second constructor
// Postcondition: numerator & denominator of the declared rational object is initialized to aa and bb, respectively; bb != 0
T operator>(const rational<T>&r2) const;
// Postcondition: returns 1 if calling object is greater than r2; 0 if r1 is equal to r2; -1 is r1 is less than r2
private:
T GCD() const;
// You must use the Euclidean algorithm. https://en.wikipedia.org/wiki/Euclidean_algorithm
// Postcondition: returns the "greatest common divisor" between the numerator and denominator of the calling rational object
T num; // numerator
T denom; // denominator
};
#endif
#include <iostream>
#include "containerl.h"
using namespace std;
template <class U>
ostream& operator<<(ostream& out, const container<U>& c1)
{
out << "The 'container' contains the following " <<
c1.count + 1 << " value(s):\n";
if (c1.count == -1)
out << "*** data array is empty!" << endl;
else
{
for (int i = 0; i <= c1.count; i++)
out << c1.data[i] << '\t';
out << endl;
}
return out;
}
template <class T>
container<T>::container()
{
count = -1;
for (int i = 0; i < CAPACITY; i++)
data[i] = 0;
}
template <class T>
void container<T>::undelete()
{
++count;
}
template <class T>
bool container<T>::isEmpty()
{
if (count == -1)
return true;
else
return false;
}
template <class T>
bool container<T>::isFull()
{
if (count == 9)
return true;
else
return false;
}
template <class T>
void container<T>::insert(T 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);
}
}
template <class T>
void container<T>::remove()
{
if (count >= 0)
{
--count;
}
else
{
cout << endl << "Container is empty; program is terminated!";
exit(1);
}
}
template <class T>
void container<T>::sort()
{
T pass, c, temp;
if (count >= 1)
{
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;
}
}
}
////////////////////////////
template <class U>
istream& operator>>(istream &in, rational<U>& r1)
{
in >> r1.a >> r1.b;
return in;
}
template <class U>
ostream& operator<<(ostream &out, rational<U>& r1)
{
U gcd = r1.GCD();
if (r1.b < 0)
{
r1.b = -(r1.b);
r1.a = -(r1.a);
}
out << r1.a / gcd << "/" << r1.b / gcd;
return out;
}
template <class T>
rational<T>::rational()
{
num = 1;
denom = 1;
}
template <class T>
rational<T>::rational(T aa, T bb)
{
num = aa;
denom = bb;
}
template <class T>
T rational<T>::operator>(const rational<T>&r2) const
{
double r1total, r2total;
r1total = (double) num/denom;
r2total = (double) r2.num/r2.denom;
if (r1total > r2total)
return 1;
else if (r1total == r2total)
return 0;
else
return -1;
}
template <class T>
T rational<T>::GCD() const
{
T n = abs(num);
T d = abs(denom);
while(d != 0)
{
T temp = d;
d = n % d;
n = temp;
}
return n;
}
#include <iostream>
#include "containerl.h"
using namespace std;
int main()
{
container <int> 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 n = 0; n < 10; n++)
{
c.remove();
}
cout << endl;
cout << c;
cout <<
"\nWe now perform the 'undelete' operation three times:\n";
for (int m = 0; m < 3; m++)
{
c.undelete();
}
cout << c;
return 0;
}
|