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
|
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
const int IGNORE_VALUE = 1000;
const int MENU_MAX = 6;
const int MENU_MIN = 0;
const int NOT_FOUND = -1;
int FindRestaurant(string newRestaurant, vector<string> restaurantChoices);
void PrintMenu(){
cout << "Please select one of the following options: " << endl;
cout << endl;
cout << "0 - Quit the program" << endl;
cout << "1 - Display all restaurants" << endl;
cout << "2 - Add a restaurant" << endl;
cout << "3 - Remove a restaurant" << endl;
cout << "4 - \"Cut\" the list of restaurants" << endl;
cout << "5 - \"Shuffle\" the list of restaurants" << endl;
cout << "6 - Begin the tournament" << endl;
cout << endl;
cout << "Enter your selection now: " << endl;
return;
}
bool menuError(int menuChoice) {
if (menuChoice > MENU_MAX || menuChoice < MENU_MIN)
{
cout << "Invalid selection. Please try again." << endl;
PrintMenu();
return true;
}
else {
return false;
}
}
void PrintVector(vector<string> restaurantChoices) {
for (unsigned int i = 0; i < restaurantChoices.size(); ++i) {
cout << "\t\"" << restaurantChoices.at(i) << "\"" << endl;
}
}
void AddRestaurant(string& newRestaurant, vector<string>& restaurantChoices) {
int found = 0;
found = FindRestaurant(newRestaurant, restaurantChoices);
if (found == NOT_FOUND) {
restaurantChoices.push_back(newRestaurant);
cout << newRestaurant << " has been added." << endl;
}
else {
cout << "That restaurant is already on the list, you can not add it again." << endl;
}
return;
}
int FindRestaurant(string newRestaurant, vector<string> restaurantChoices)
{
for (unsigned int i = 0; i < restaurantChoices.size(); i++)
{
if (newRestaurant == restaurantChoices[i])
{
return i;
}
}
return NOT_FOUND;
}
void RemoveRestaurant(string newRestaurant, vector<string>& restaurantChoices){
int found = 0;
found = FindRestaurant(newRestaurant, restaurantChoices);
if (found != NOT_FOUND) {
restaurantChoices.erase(restaurantChoices.begin() + found);
cout << newRestaurant << " has been removed." << endl;
}
else {
cout << "That restaurant is not on the list, you can not remove it." << endl;
}
return;
}
void CutRestaurant(vector<string>& restaurantList, int cutChoice) {
vector<string> changeVector = restaurantList;
if (cutChoice > changeVector.size()) {
cout << endl << "The restaurants can not be cut there, there are only " << changeVector.size() << " current - number - of - restaurants ";
return;
}
else{
for(unsigned int i = 0; i < cutChoice; i++){
changeVector.at(i) = restaurantList.at(0);
restaurantList.erase(restaurantList.begin());
}
for (int j=0; j < cutChoice; j++) {
restaurantList.push_back(changeVector.at(j));
}
}
return;
}
void ShuffleRestaurants(vector<string>& restaurantList) {
int sizeOfVector = 0;
int halfOfVector = 0;
vector<string> newVector;
sizeOfVector = restaurantList.size();
if (sizeOfVector % 2 == 1) {
cout << "The current tournament size (" << sizeOfVector << ") is not a power of two (2, 4, 8...)." << endl;
cout<< "A shuffle is not possible. Please add or remove resturants." << endl;
return;
}
else {
halfOfVector = sizeOfVector/2;
for (int i = 0; i < halfOfVector; i++) {
newVector.push_back(restaurantList.at(i + halfOfVector));
newVector.push_back(restaurantList.at(i));
}
restaurantList.clear();
for (int i = 0; i < sizeOfVector; i++) {
restaurantList.push_back(newVector.at(i));
}
return;
}
return;
}
void BeginTournament(vector<string>& restaurantList) {
double logMath = 0.00;
int logInt = 0;
int userSelection = 0;
int round = 0;
int combination = 0;
int listSize = 0;
int halfOfList = 0;
halfOfList = listSize/2;
listSize = restaurantList.size();
logMath = log(listSize / log(2.0));
logInt = logMath;
vector<string> tempVector;
if (logInt != logMath) {
cout << "The current tournament size(" << listSize << ") is not a power of two (2, 4, 8...)." << endl;
cout << "A tournament is not possible. Please add or remove resturants." << endl;
return;
}
while(listSize > 1) { //continues for loop until vector has one restaurant left
round++;
combination = halfOfList;
cout << "Round: " << round << endl;
tempVector.clear();
for (int i = 0; i < combination; i++) {
cout << "Type \"1\" if you prefer " << restaurantList.at(i) << " or" << endl;
cout << "type \"2\" if you prefer " << restaurantList.at(i + 1) << endl;
cout << "Choice: " << endl;
cin >> userSelection;
while(cin.fail() || (userSelection != 1 && userSelection != 2)) {
cin.clear();
cin.ignore('\n' , IGNORE_VALUE);
cout << "invalid response." << endl;
cin >> userSelection;
}
if (userSelection == 1) {
restaurantList.erase(restaurantList.begin() + i + 1);
}
else if (userSelection == 2) {
restaurantList.erase(restaurantList.begin() + i);
}
}
}
cout << "The winning restaurant is " << restaurantList.at(0) << endl << endl;
return;
}
int main() {
int menuChoice = 0;
vector<string> restaurantChoices;
string restaurantName;
int userCutPoint;
cout << "Welcome to the restaurant battle!" << endl;
cout << endl;
cout << endl;
PrintMenu();
do {
do {
cin >> menuChoice;
} while (menuError(menuChoice));
switch (menuChoice) {
case 1:
cout << "Here are the current restaurants: " << endl;
cout << endl;
PrintVector(restaurantChoices);
PrintMenu();
break;
case 2:
cout << "What is the name of the restaurant you want to add? " << endl;
cin.ignore();
getline (cin, restaurantName);
AddRestaurant(restaurantName, restaurantChoices);
cout << endl;
PrintMenu();
break;
case 3:
cout << "What is the name of the restaurant you want to remove?" << endl;
cin.ignore();
getline(cin, restaurantName);
RemoveRestaurant(restaurantName, restaurantChoices);
cout << endl;
PrintMenu();
break;
case 4:
cout << "How many restaurants should be taken from the top and put on the bottom?" << endl;
cin.ignore();
cin >> userCutPoint;
CutRestaurant(restaurantChoices, userCutPoint);
cout << endl;
PrintMenu();
break;
case 5:
ShuffleRestaurants(restaurantChoices);
PrintMenu();
break;
case 6:
BeginTournament(restaurantChoices);
PrintMenu();
break;
case 0:
cout << "Goodbye!";
return 0;
break;
}
} while (true);
return 0;
}
|