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
|
#include "buckets.h"
#include <ctime>
#include <iostream>
using namespace std;
//@gets the number of buckets from the user
//@usage: num = getNumberOfBuckets();
int getNumberOfBuckets();
//@gets the answer to continue or not from the user
//@usage userSays = getUsersAnswer();
char getUsersAnswer();
//@prints the contents of the buckets
//@usage print(buckets);
void print(Buckets buckets);
//@sorts the pebbles into red section, white section, blue section
//@usage sort(buckets);
void sort(Buckets& buckets);
//@prints a number of newlines
//@usage printNewLines(4);
void printNewLines(int num);
//@ checks that a bucket label exists
//@usage if (isOkBucket(label))
bool isOkBucket(Buckets buckets, int label);
//@prints a message if a nonexisting bucket is accessed
//@usage printBadBucketMessage(label);
void printBadBucketMessage(int label);
int main()
{
Buckets buckets;
char userSays;
int number;
userSays = 'y';
printNewLines(3);
while (userSays == 'y')
{
// number = getNumberOfBuckets();
Buckets.buckets();
printNewLines(3);
//buckets.FillBuckets();
cout << "I got here!" << endl;
printNewLines(3);
print(buckets);
// sort(buckets);
printNewLines(3);
// print(buckets);
//buckets.TrashBuckets();
printNewLines(2);
userSays = getUsersAnswer();
printNewLines(2);
}
return 0;
}
//@gets the number of buckets from the user
//@usage: num = getNumberOfBuckets();
int getNumberOfBuckets()
{
int number;
cout << "Enter the number of buckets -> ";
cin >> number;
return number;
}
//@gets the answer to continue or not from the user
//@usage userSays = getUsersAnswer();
char getUsersAnswer()
{
cout << "Do you want to run again <y> or <n>? ";
char answer;
cin >> answer;
return answer;
}
//@prints the contents of the buckets
//@usage print(buckets);
void print(Buckets buckets)
{
for (int i = 0; i < buckets.GetNumber(); i++)
{
if (buckets.GetColor(i) == RED)
cout << i << " : " << "Red" << endl;
else if (buckets.GetColor(i) == WHITE)
cout << i << " : " << "White" << endl;
else
cout << i << " : " << "Blue" << endl;
}
}
//@sorts the pebbles into red section, white section, blue section
//@usage sort(buckets);
void sort(Buckets& buckets)
{
int lastRed = -1;
int lastWhite = -1;
int firstMixed = 0;
while (firstMixed < buckets.GetNumber())
{
if (isOkBucket(buckets, firstMixed))
{
if (buckets.GetColor(firstMixed) == BLUE)
{
firstMixed++;
} else if (buckets.GetColor(firstMixed) == WHITE)
{
lastWhite++;
if (isOkBucket(buckets, lastWhite))
buckets.Swap(lastWhite, firstMixed);
else
printBadBucketMessage(lastWhite);
firstMixed++;
} else
{
lastWhite++;
if (isOkBucket(buckets, lastWhite))
buckets.Swap(lastWhite, firstMixed);
else
printBadBucketMessage(lastWhite);
lastRed++;
if (isOkBucket(buckets, lastRed))
buckets.Swap(lastRed, lastWhite);
else
printBadBucketMessage(lastRed);
firstMixed++;
}
} else {
printBadBucketMessage(firstMixed);
}
}
}
//@prints a number of newlines
//@usage printNewLines(4);
void printNewLines(int num)
{
for (int i = 0; i < num; i++)
{
cout << endl;
}
}
//@ checks that a bucket label exists
//@usage if (isOkBucket(label))
bool isOkBucket(Buckets buckets, int label)
{
if (label >= 0 && label < buckets.GetNumber())
{
return true;
} else {
return false;
}
}
//@prints a message if a nonexisting bucket is accessed
//@usage printBadBucketMessage(label);
void printBadBucketMessage(int label)
{
cout << "bucket with label " << label << "does NOT exist!" << endl;
}
|