adding cs142 code
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
CXX=g++
|
||||
CPPFLAGS=-Wall -g -std=c++0x
|
||||
SOURCES=restaurant.cc sm_array.cc collect.cc tournament.cc
|
||||
OBJECTS=$(SOURCES:.cc=.o)
|
||||
EXE=restaurant
|
||||
|
||||
all: $(EXE)
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(LDFLAGS) $(OBJECTS) -o $@
|
||||
|
||||
sm_array.o: sm_array.cc sm_array.h
|
||||
collect.o: collect.cc collect.h sm_array.h
|
||||
tournament.o: tournament.cc tournament.h
|
||||
|
||||
test-display: test-display.cc sm_array.o collect.o test_constants.h
|
||||
test-contains: test-contains.cc sm_array.o collect.o test_constants.h
|
||||
test-remove: test-remove.cc sm_array.o collect.o test_constants.h
|
||||
test-add: test-add.cc sm_array.o collect.o test_constants.h
|
||||
test-random: test-random.cc sm_array.o collect.o test_constants.h
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -rvf *.dSYM
|
||||
@rm -vf restaurant
|
||||
@rm -vf test-display
|
||||
@rm -vf test-random
|
||||
@rm -vf test-contains
|
||||
@rm -vf test-remove
|
||||
@rm -vf test-add
|
||||
|
||||
test: test-random test-display test-contains test-remove test-add
|
||||
./test-display
|
||||
./test-contains
|
||||
./test-remove
|
||||
./test-add
|
||||
@echo "remember to manually inspect test-random"
|
||||
|
||||
run: restaurant
|
||||
./restaurant
|
||||
@@ -0,0 +1,75 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "collect.h"
|
||||
|
||||
|
||||
ostream & operator<<(ostream & os, const vector<string> & s) {
|
||||
os << "menu:\n";
|
||||
for(auto i: s) {
|
||||
os << " " << i << endl;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
garray initial_restaurants() {
|
||||
garray a;
|
||||
a.add("Cafe Rio");
|
||||
a.add("Pappa Murphy's");
|
||||
a.add("Quiznos");
|
||||
a.add("Jimmy Johns");
|
||||
a.add("Thai Chili Gardens");
|
||||
a.add("Subway");
|
||||
a.add("Taco Bell");
|
||||
a.add("India Palace");
|
||||
return a;
|
||||
}
|
||||
|
||||
garray populate_restaurants() {
|
||||
string input;
|
||||
bool running = true;
|
||||
garray restaurants = initial_restaurants();
|
||||
while(running) {
|
||||
cout << "\n" << menu << endl;
|
||||
cout << "your selection: ";
|
||||
cin >> input;
|
||||
cout << endl;
|
||||
if(input[0] == 'd') {
|
||||
cout << restaurants << endl;
|
||||
}
|
||||
else if(input[0] == 'a') {
|
||||
cout << "which restaurant should I ADD: ";
|
||||
string name;
|
||||
cin.ignore();
|
||||
getline(cin, name);
|
||||
bool add_results = restaurants.add(name);
|
||||
if(add_results) {
|
||||
cout << "added '" << name << "' successfully!" << endl;
|
||||
}
|
||||
else {
|
||||
cerr << "did not add: '" << name << "' "
|
||||
<< "(already there or max size)" << endl;
|
||||
}
|
||||
}
|
||||
else if(input[0] == 'r') {
|
||||
cout << "which restaurant should I REMOVE: ";
|
||||
string name;
|
||||
cin.ignore();
|
||||
getline(cin, name);
|
||||
bool remove_results = restaurants.remove(name);
|
||||
if(remove_results) {
|
||||
cout << "removed '" << name << "' successfully!" << endl;
|
||||
}
|
||||
else {
|
||||
cerr << "did not remove: '" << name << "' "
|
||||
<< "(probably not in array)" << endl;
|
||||
}
|
||||
}
|
||||
else if(input[0] == 's') {
|
||||
restaurants.randomize();
|
||||
}
|
||||
else if(input[0] == 'b') {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
return restaurants;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef __COLLECT_H__
|
||||
#define __COLLECT_H__
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "sm_array.h"
|
||||
|
||||
const vector<string> menu = {
|
||||
"(d)isplay",
|
||||
"(a)dd",
|
||||
"(r)emove",
|
||||
"(s)huffle",
|
||||
"(b)egin",
|
||||
};
|
||||
|
||||
ostream & operator<<(ostream &, const vector<string> &);
|
||||
|
||||
garray initial_restaurants();
|
||||
garray populate_restaurants();
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "sm_array.h"
|
||||
#include "collect.h"
|
||||
#include "tournament.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
garray restaurants = populate_restaurants();
|
||||
string restaurant_to_rule_them_all;
|
||||
try {
|
||||
restaurant_to_rule_them_all = tournament(restaurants);
|
||||
}
|
||||
catch (string e) {
|
||||
cerr << e << endl;
|
||||
}
|
||||
cout << ">>> winner: " << restaurant_to_rule_them_all << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "sm_array.h"
|
||||
|
||||
garray::garray(): size(0) {};
|
||||
|
||||
bool garray::add(string name) {
|
||||
if(name == "") {
|
||||
throw string("cannot add empty string");
|
||||
}
|
||||
if((not contains(name)) and size < MAX_DATA_SIZE) {
|
||||
data[size] = name;
|
||||
size++;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool garray::contains(string name) {
|
||||
for(int i = 0; i < size; i++) {
|
||||
if(name == data[i])
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool garray::remove(string name) {
|
||||
bool reduce = false;
|
||||
if(contains(name)) {
|
||||
reduce = true;
|
||||
}
|
||||
else {
|
||||
return reduce;
|
||||
}
|
||||
garray tmp;
|
||||
for(int i = 0; i < size; i++) {
|
||||
if(data[i] != name) {
|
||||
tmp.add(data[i]);
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < MAX_DATA_SIZE; i++) {
|
||||
data[i] = tmp.data[i];
|
||||
}
|
||||
if(reduce)
|
||||
size--;
|
||||
return reduce;
|
||||
}
|
||||
|
||||
void garray::randomize() {
|
||||
random_shuffle(&data[0], &data[size]);
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, garray & g) {
|
||||
os << "[";
|
||||
for(int i = 0; i < g.size; i++) {
|
||||
os << i << "-" << g.data[i];
|
||||
if (i != g.size - 1) {
|
||||
os << ", ";
|
||||
}
|
||||
}
|
||||
os << "]";
|
||||
return os;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef __SM_ARRAY_H__
|
||||
#define __SM_ARRAY_H__
|
||||
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MAX_DATA_SIZE = 16;
|
||||
|
||||
struct garray {
|
||||
// gimpy string container that cannot be larger than 16,
|
||||
// and cannot contain empty strings
|
||||
|
||||
int size;
|
||||
string data [MAX_DATA_SIZE];
|
||||
|
||||
garray();
|
||||
|
||||
bool add(string name);
|
||||
bool remove(string name);
|
||||
void randomize();
|
||||
bool contains(string name);
|
||||
};
|
||||
|
||||
ostream & operator<<(ostream &, garray &);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "sm_array.h"
|
||||
#include "collect.h"
|
||||
#include "test_constants.h"
|
||||
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
garray a;
|
||||
|
||||
bool raised_error = false;
|
||||
bool correct_error_string = false;
|
||||
try {
|
||||
a.add("");
|
||||
}
|
||||
catch (string e) {
|
||||
raised_error = true;
|
||||
if(e == "cannot add empty string") {
|
||||
correct_error_string = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(not raised_error) {
|
||||
cerr << "should have raised an error trying to add an empty string" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(not correct_error_string) {
|
||||
cerr << "incorrect error string" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
garray g = initial_restaurants();
|
||||
if(g.add("Cafe Rio") or g.add("Jimmy Johns") or not g.add("Jimbo White")) {
|
||||
cerr << "shouldn't be able to add something that's already in there" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "sm_array.h"
|
||||
#include "collect.h"
|
||||
#include "test_constants.h"
|
||||
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
garray a = initial_restaurants();
|
||||
|
||||
if(a.contains("Shifty Pete's Tacos")) {
|
||||
cerr << "should not contain \"Shifty Pete's Tacos\"" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(not a.contains("Taco Bell")) {
|
||||
cerr << "Should have Taco Bell" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(not a.contains("Cafe Rio")) {
|
||||
cerr << "Failed test at beginning" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(not a.contains("India Palace")) {
|
||||
cerr << "Failed test at end" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(a.contains("")) {
|
||||
cerr << "should not contain empty string" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "sm_array.h"
|
||||
#include "collect.h"
|
||||
#include "test_constants.h"
|
||||
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
garray a = initial_restaurants();
|
||||
ostringstream output;
|
||||
output << a;
|
||||
|
||||
if(output.str() != TEST_ARRAY_OUTPUT) {
|
||||
cerr << "problem with text output" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(a.size != TEST_ARRAY_SIZE) {
|
||||
cerr << "problem with expected test array size" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "sm_array.h"
|
||||
#include "collect.h"
|
||||
#include "test_constants.h"
|
||||
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
srand(time(NULL));
|
||||
garray a = initial_restaurants();
|
||||
cout << " >>> " << a << endl;
|
||||
cout << "should print the previous array in random orders: " << endl;
|
||||
for(int i = 0; i < 10; i++) {
|
||||
a.randomize();
|
||||
cout << " >>> " << a << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "sm_array.h"
|
||||
#include "collect.h"
|
||||
#include "test_constants.h"
|
||||
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
ostringstream output;
|
||||
garray a = initial_restaurants();
|
||||
string expected;
|
||||
bool successful_removal;
|
||||
int expected_size;
|
||||
|
||||
successful_removal = a.remove("");
|
||||
output << a;
|
||||
expected = TEST_ARRAY_OUTPUT;
|
||||
expected_size = 8;
|
||||
if(output.str() != expected
|
||||
and a.size != expected_size
|
||||
and successful_removal) {
|
||||
cerr << "improper removal of empty string" << endl;
|
||||
cerr << "have: " << a << ", " << a.size << endl;
|
||||
cerr << "expected: " << expected << ", " << expected_size << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
successful_removal = a.remove("Taco Bell");
|
||||
output << a;
|
||||
expected = "[Cafe Rio, Pappa Murphy's, Quiznos, Jimmy Johns, Thai Chili Gardens, Subway, India Palace]";
|
||||
expected_size = 7;
|
||||
if(output.str() != expected
|
||||
and a.size != expected_size
|
||||
and not successful_removal) {
|
||||
cerr << "improper removal from middle (Taco Bell)" << endl;
|
||||
cerr << "have: " << a << ", " << a.size << endl;
|
||||
cerr << "expected: " << expected << ", " << expected_size << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
successful_removal = a.remove("India Palace");
|
||||
output.str("");
|
||||
output << a;
|
||||
expected = "[Cafe Rio, Pappa Murphy's, Quiznos, Jimmy Johns, Thai Chili Gardens, Subway]";
|
||||
expected_size = 6;
|
||||
if(output.str() != expected
|
||||
and a.size != expected_size
|
||||
and not successful_removal) {
|
||||
cerr << "improper removal from end (India palace)" << endl;
|
||||
cerr << "have: " << a << ", " << a.size << endl;
|
||||
cerr << "expected: " << expected << ", " << expected_size << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
successful_removal = a.remove("Shitar's Palace");
|
||||
output.str("");
|
||||
output << a;
|
||||
// same expected as last time
|
||||
expected_size = 6;
|
||||
if(output.str() != expected
|
||||
and a.size != expected_size
|
||||
and successful_removal) {
|
||||
cerr << "improper removal of value that is not in the list" << endl;
|
||||
cerr << "have: " << a << ", " << a.size << endl;
|
||||
cerr << "expected: " << expected << ", " << expected_size << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
successful_removal = a.remove("Cafe Rio");
|
||||
output.str("");
|
||||
output << a;
|
||||
expected = "[Pappa Murphy's, Quiznos, Jimmy Johns, Thai Chili Gardens, Subway]";
|
||||
expected_size = 6;
|
||||
if(output.str() != expected
|
||||
and a.size != expected_size
|
||||
and not successful_removal) {
|
||||
cerr << "improper removal from beginning (Cafe Rio)" << endl;
|
||||
cerr << "have: " << a << ", " << a.size << endl;
|
||||
cerr << "expected: " << expected << ", " << expected_size << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef __TESTFUNCS_H__
|
||||
#define __TESTFUNCS_H__
|
||||
|
||||
#include <string>
|
||||
|
||||
const std::string TEST_ARRAY_OUTPUT =
|
||||
"[0-Cafe Rio, 1-Pappa Murphy's, 2-Quiznos, "
|
||||
"3-Jimmy Johns, 4-Thai Chili Gardens, 5-Subway, 6-Taco Bell, 7-India Palace]";
|
||||
|
||||
const int TEST_ARRAY_SIZE = 8;
|
||||
|
||||
#endif
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef __TOURNAMENT_H__
|
||||
#define __TOURNAMENT_H__
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
#include "sm_array.h"
|
||||
|
||||
string tournament(garray &);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user