school/cs142/lab06/add-display.cpp

212 lines
6.0 KiB
C++
Raw Normal View History

2016-04-06 20:45:34 -07:00
#include <iostream>
#include <array>
#include <time.h>
#include <ctime>
#include <functional>
#include <cstdlib>
#include <algorithm>
using namespace std;
void display_all_restaurants(array<string, 16> & restaurants, int & current_size) {
for(int i = 0; i <= current_size - 1; i++) {
if(i == current_size - 1) {
cout << i << "-" << restaurants[i] << endl;
}
else {
cout << i << "-" << restaurants[i] << ", ";
}
}
}
void add_a_restaurant(array<string, 16> & restaurants, int & current_size) {
string name;
bool correct_entry = true;
cout << "please enter name of restaurant: " << endl;
cin.ignore();
getline(cin, name);
if(current_size >= 16) {
cout << "The array already has 16 restaurants and you can't add anymore." << endl;
}
else {
for(int i = 0; i <= current_size - 1; i++) {
if(restaurants[i] == name) {
correct_entry = false;
}
}
if(correct_entry == true) {
restaurants[current_size] = name;
current_size++;
cout << "you added the restaurant " << name << endl;
}
else {
cout << "this restaurant is already existant" << endl;
}
}
}
void remove_a_restaurant(array<string, 16> & restaurants, int & current_size) {
string name;
bool correct_entry = false;
cout << "Please enter name of restaurant you would like to remove: " << endl;
cin.ignore();
getline(cin, name);
for(int i = 0; i <= current_size -1; i++) {
if(i == current_size && restaurants[i] == name) {
current_size--;
cout << name << " has been removed" << endl;
}
if(restaurants[i] == name) {
restaurants[i] = restaurants[current_size -1];
current_size--;
correct_entry = true;
cout << name << " has been removed" << endl;
}
}
if(correct_entry == false) {
cout << "restaurant does not exist" << endl;
}
}
void shuffle_array(array<string, 16> & restaurants, int current_size) {
random_shuffle (&restaurants[0], &restaurants[current_size]);
}
int round_counter(int current_size) {
int round;
if(current_size == 16) {
round = 4;
}
else if(current_size == 8) {
round = 3;
}
else if(current_size == 4) {
round = 2;
}
else if(current_size == 2) {
round =1;
}
return round;
}
int match_counter(int current_size) {
int match;
if(current_size == 16) {
match = 8;
}
else if(current_size == 8) {
match = 4;
}
else if(current_size == 4) {
match = 2;
}
else if(current_size == 2) {
match =1;
}
return match;
}
void begin_tournament(array<string, 16> & restaurants, int & current_size) {
bool temp_bool = true;
bool tourn_running = true;
cin.ignore();
string name;
int match = match_counter(current_size);
int round = round_counter(current_size);
int current_round = 1;
array<string,8> temp_array;
while(tourn_running == true) {
int current_match = 1;
int place = 0;
temp_bool = true;
cin.ignore();
for(int i = 0; i < current_size; i += 2) {
while(temp_bool == true) {
cout << "which restaurant: " << restaurants[i] << " or " << restaurants[i + 1] << endl;
cout << "match " << current_match << "/" << match << " Round "
<< current_round << "/" << round << endl;
getline(cin, name);
if(name == restaurants[i] or name == restaurants[i + 1]) {
if(name == restaurants[i]) {
temp_array[place] = name;
}
else {
temp_array[place] = name;
}
place ++;
temp_bool = false;
current_match++;
}
else {
cout << "invalid response " << name << endl;
}
}
temp_bool = true;
}
while(temp_bool == true) {
for(int i = 0; i < place; i++) {
restaurants[i] = temp_array[i];
}
current_size = current_size/2;
if(current_size == 1) {
cout << "this is the winner " << restaurants[0] << endl;
exit(0);
}
else {
cout << "next round of tournament" << endl;
temp_bool = false;
}
}
match = match/2;
current_round++;
}
}
void test_size(array<string,16> & restaurants, int & current_size) {
if(current_size ==2 or current_size ==4 or current_size ==8 or current_size == 16) {
cout << "you have 2^n" << endl;
begin_tournament(restaurants, current_size);
}
else {
cout << "you dont have 2^n number of restaurants, please remove or enter more" << endl;
}
}
int main() {
array<string,16> restaurants = {{
"cafe rio",
"india palace",
"panda express",
"subway",
"quiznos",
"olive garden",
"jimmy johns",
"cougareat",
}};
bool temp = true;
int current_size = 8;
srand(time(NULL));
while(temp == true) {
int input;
cout << "pick a function" << endl;
cout << "1)Display all restaurants\n2)Add a restaurant\n3)Remove a restaurant" << endl;
cout << "4)Shufflle the array\n5)Begin the tournament" << endl;
cin >> input;
if(input == 1){
display_all_restaurants(restaurants, current_size);
}
if(input == 2){
add_a_restaurant(restaurants, current_size);
}
if(input == 3){
remove_a_restaurant(restaurants, current_size);
}
if(input == 4){
shuffle_array(restaurants, current_size);
}
if(input == 5){
test_size(restaurants, current_size);
}
}
}