adding cs142 code
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user