adding cs142 code

This commit is contained in:
dm
2016-04-06 20:45:34 -07:00
parent 552d456d53
commit 8e52ce1982
174 changed files with 14064 additions and 0 deletions
+46
View File
@@ -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;
}