school/cs142/smcquay/lab06/tournament.cc

47 lines
1.1 KiB
C++
Raw Normal View History

2016-04-06 20:45:34 -07:00
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
#include "tournament.h"
int is_pow_2 (unsigned int x) {
return ((x != 0) && ((x & (~x + 1)) == x));
}
string tournament(garray & restaurants) {
if(not is_pow_2(restaurants.size)){
ostringstream o;
o << restaurants.size << " is not a power of 2";
throw o.str();
}
string selection;
bool finished = false;
while(not finished) {
garray tmp;
for(int i=0; i < restaurants.size / 2; i++) {
int a_i = 2*i;
int b_i = a_i + 1;
string a = restaurants.data[a_i];
string b = restaurants.data[b_i];
cout << "(a) " << a << " (b) " << b << endl;
cout << "your selection: ";
cin >> selection;
if(selection[0] == 'a')
tmp.add(a);
else
tmp.add(b);
}
if(tmp.size == 1) {
finished = true;
selection = tmp.data[0];
}
else {
restaurants = tmp;
}
}
return selection;
}