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
+29
View File
@@ -0,0 +1,29 @@
CXXFLAGS=-Wall -g -std=c++0x -Ijsoncpp -DJSON_IS_AMALGAMATION
all: app iotest
app: main.o tournament.o restaurant.o json.o io.o
$(CXX) $(CXXFLAGS) main.o restaurant.o tournament.o io.o json.o -o $@
main.o: main.cpp
restaurant.o: restaurant.cpp restaurant.h
tournament.o: tournament.cpp tournament.h
io.o: io.h io.cpp
json.o: jsoncpp/jsoncpp.cpp
$(CXX) $(CXXFLAGS) $< -c -o $@
iotest: iotest.cpp io.o json.o restaurant.o
clean:
@rm -vf *.o
@rm -vf app
@rm -vf iotest
@rm -rvf *.dSYM
test: all
@./app
io: iotest
@./iotest restaurants.db
debug: all
gdb rest
+30
View File
@@ -0,0 +1,30 @@
#include <string>
#include <fstream>
#include <streambuf>
#include <stdexcept>
using namespace std;
#include "jsoncpp/json/json.h"
#include "io.h"
#include "restaurant.h"
vector<restaurant> parse_file(string filename) {
vector<restaurant> restaurants;
ifstream infile(filename.c_str());
string file_contents((istreambuf_iterator<char>(infile)),
istreambuf_iterator<char>());
Json::Value root;
Json::Reader reader;
bool good_parse = reader.parse(file_contents, root);
if(not good_parse) {
throw runtime_error(reader.getFormattedErrorMessages());
}
for(unsigned int i = 0; i < root.size(); i++) {
string name = root[i]["name"].asString();
int score = root[i]["score"].asInt();
restaurant r(name, score);
restaurants.push_back(r);
}
return restaurants;
}
+11
View File
@@ -0,0 +1,11 @@
#ifndef __IO_H__
#define __IO_H__
#include <vector>
#include <string>
using namespace std;
#include "restaurant.h"
vector<restaurant> parse_file(string filename);
#endif
+27
View File
@@ -0,0 +1,27 @@
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
#include "io.h"
#include "restaurant.h"
const string usage = "usage: iotest <input json file>";
int main(int argc, char * argv[]) {
if (argc != 2) {
cerr << usage << endl;
return 1;
}
try {
vector<restaurant> v = parse_file(argv[1]);
for(auto r: v) {
cout << r << endl;
}
}
catch(runtime_error e) {
cerr << e.what() << endl;
}
return 0;
}
+249
View File
@@ -0,0 +1,249 @@
/// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/).
/// It is intented to be used with #include <json/json-forwards.h>
/// This header provides forward declaration for all JsonCpp types.
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: LICENSE
// //////////////////////////////////////////////////////////////////////
/*
The JsonCpp library's source code, including accompanying documentation,
tests and demonstration applications, are licensed under the following
conditions...
The author (Baptiste Lepilleur) explicitly disclaims copyright in all
jurisdictions which recognize such a disclaimer. In such jurisdictions,
this software is released into the Public Domain.
In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is
released under the terms of the MIT License (see below).
In jurisdictions which recognize Public Domain property, the user of this
software may choose to accept it either as 1) Public Domain, 2) under the
conditions of the MIT License (see below), or 3) under the terms of dual
Public Domain/MIT License conditions described here, as they choose.
The MIT License is about as close to Public Domain as a license can get, and is
described in clear, concise terms at:
http://en.wikipedia.org/wiki/MIT_License
The full text of the MIT License follows:
========================================================================
Copyright (c) 2007-2010 Baptiste Lepilleur
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
========================================================================
(END LICENSE TEXT)
The MIT license is compatible with both the GPL and commercial
software, affording one all of the rights of Public Domain with the
minor nuisance of being required to keep the above copyright notice
and license text in the source code. Note also that by accepting the
Public Domain "license" you can re-license your copy using whatever
license you like.
*/
// //////////////////////////////////////////////////////////////////////
// End of content of file: LICENSE
// //////////////////////////////////////////////////////////////////////
#ifndef JSON_FORWARD_AMALGATED_H_INCLUDED
# define JSON_FORWARD_AMALGATED_H_INCLUDED
/// If defined, indicates that the source file is amalgated
/// to prevent private header inclusion.
#define JSON_IS_AMALGATED
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: include/json/config.h
// //////////////////////////////////////////////////////////////////////
// Copyright 2007-2010 Baptiste Lepilleur
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
#ifndef JSON_CONFIG_H_INCLUDED
# define JSON_CONFIG_H_INCLUDED
/// If defined, indicates that json library is embedded in CppTL library.
//# define JSON_IN_CPPTL 1
/// If defined, indicates that json may leverage CppTL library
//# define JSON_USE_CPPTL 1
/// If defined, indicates that cpptl vector based map should be used instead of std::map
/// as Value container.
//# define JSON_USE_CPPTL_SMALLMAP 1
/// If defined, indicates that Json specific container should be used
/// (hash table & simple deque container with customizable allocator).
/// THIS FEATURE IS STILL EXPERIMENTAL! There is know bugs: See #3177332
//# define JSON_VALUE_USE_INTERNAL_MAP 1
/// Force usage of standard new/malloc based allocator instead of memory pool based allocator.
/// The memory pools allocator used optimization (initializing Value and ValueInternalLink
/// as if it was a POD) that may cause some validation tool to report errors.
/// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined.
//# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1
/// If defined, indicates that Json use exception to report invalid type manipulation
/// instead of C assert macro.
# define JSON_USE_EXCEPTION 1
/// If defined, indicates that the source file is amalgated
/// to prevent private header inclusion.
/// Remarks: it is automatically defined in the generated amalgated header.
// #define JSON_IS_AMALGAMATION
# ifdef JSON_IN_CPPTL
# include <cpptl/config.h>
# ifndef JSON_USE_CPPTL
# define JSON_USE_CPPTL 1
# endif
# endif
# ifdef JSON_IN_CPPTL
# define JSON_API CPPTL_API
# elif defined(JSON_DLL_BUILD)
# define JSON_API __declspec(dllexport)
# elif defined(JSON_DLL)
# define JSON_API __declspec(dllimport)
# else
# define JSON_API
# endif
// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for integer
// Storages, and 64 bits integer support is disabled.
// #define JSON_NO_INT64 1
#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6
// Microsoft Visual Studio 6 only support conversion from __int64 to double
// (no conversion from unsigned __int64).
#define JSON_USE_INT64_DOUBLE_CONVERSION 1
#endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6
#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008
/// Indicates that the following function is deprecated.
# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))
#endif
#if !defined(JSONCPP_DEPRECATED)
# define JSONCPP_DEPRECATED(message)
#endif // if !defined(JSONCPP_DEPRECATED)
namespace Json {
typedef int Int;
typedef unsigned int UInt;
# if defined(JSON_NO_INT64)
typedef int LargestInt;
typedef unsigned int LargestUInt;
# undef JSON_HAS_INT64
# else // if defined(JSON_NO_INT64)
// For Microsoft Visual use specific types as long long is not supported
# if defined(_MSC_VER) // Microsoft Visual Studio
typedef __int64 Int64;
typedef unsigned __int64 UInt64;
# else // if defined(_MSC_VER) // Other platforms, use long long
typedef long long int Int64;
typedef unsigned long long int UInt64;
# endif // if defined(_MSC_VER)
typedef Int64 LargestInt;
typedef UInt64 LargestUInt;
# define JSON_HAS_INT64
# endif // if defined(JSON_NO_INT64)
} // end namespace Json
#endif // JSON_CONFIG_H_INCLUDED
// //////////////////////////////////////////////////////////////////////
// End of content of file: include/json/config.h
// //////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: include/json/forwards.h
// //////////////////////////////////////////////////////////////////////
// Copyright 2007-2010 Baptiste Lepilleur
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
#ifndef JSON_FORWARDS_H_INCLUDED
# define JSON_FORWARDS_H_INCLUDED
#if !defined(JSON_IS_AMALGAMATION)
# include "config.h"
#endif // if !defined(JSON_IS_AMALGAMATION)
namespace Json {
// writer.h
class FastWriter;
class StyledWriter;
// reader.h
class Reader;
// features.h
class Features;
// value.h
typedef unsigned int ArrayIndex;
class StaticString;
class Path;
class PathArgument;
class Value;
class ValueIteratorBase;
class ValueIterator;
class ValueConstIterator;
#ifdef JSON_VALUE_USE_INTERNAL_MAP
class ValueMapAllocator;
class ValueInternalLink;
class ValueInternalArray;
class ValueInternalMap;
#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
} // namespace Json
#endif // JSON_FORWARDS_H_INCLUDED
// //////////////////////////////////////////////////////////////////////
// End of content of file: include/json/forwards.h
// //////////////////////////////////////////////////////////////////////
#endif //ifndef JSON_FORWARD_AMALGATED_H_INCLUDED
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+75
View File
@@ -0,0 +1,75 @@
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <vector>
using namespace std;
#include "restaurant.h"
#include "tournament.h"
#include "io.h"
int main() {
tournament t;
bool keep_going = true;
bool file_correct = false;
string input, input_file_name;
const string usage = "usage: iotest <input json file>";
while(!file_correct) {
cout << "please enter file name: ";
getline(cin, input);
ifstream inputs(input.c_str());
if(inputs.good()) {
input_file_name = input;
file_correct = true;
}
else {
cerr << "incorrect file name" << endl;
}
}
t.populate_restaurants(input_file_name);
bool succeful_pop = false;
while(keep_going) {
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[0] == '1'){
cout << t << endl;
}
else if(input[0] == '2'){
t.add_restaurants();
}
else if(input[0] == '3'){
t.remove_restaurants();
}
else if(input[0] == '4'){
t.shuffle();
}
else if(input[0] == '5'){
if(t.verify_size()) {
keep_going = false;
succeful_pop = true;
}
}
else if(input[0] == '0'){
keep_going = false;
}
}
if(succeful_pop) {
t.start_tournament();
string output_file_name;
file_correct = false;
while(!file_correct) {
cout << "please enter file name: ";
getline(cin, output_file_name);
ofstream out(output_file_name.c_str());
if(out.good()) {
out << t;
file_correct = true;
}
else {
cerr << "incorrect file name" << endl;
}
}
}
}
+8
View File
@@ -0,0 +1,8 @@
#include "restaurant.h"
using namespace std;
ostream & operator<<(ostream & os, restaurant & r) {
os << "{\"name\": \"" << r.name << "\", \"score\": " << r.score << "}";
return os;
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef __RESTAURANT_H__
#define __RESTAURANT_H__
#include <string>
#include <ostream>
using namespace std;
class restaurant {
public:
string name;
int score;
restaurant(string name, int score): name(name), score(score) {}
friend ostream & operator<<(ostream & os, restaurant & r);
};
#endif
+5
View File
@@ -0,0 +1,5 @@
[
{"name": "Italian Stallion", "score": 0},
{"name": "Pizza Planet", "score": 20},
{"name": "Gousteau's", "score": 500}
]
+196
View File
@@ -0,0 +1,196 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdexcept>
using namespace std;
#include "tournament.h"
#include "restaurant.h"
#include "io.h"
ostream & operator<<(ostream & os, vector<restaurant> & v) {
for(auto i: v) {
os << i << endl;
}
return os;
}
ostream & operator<<(ostream & os, tournament & t){
os << "[";
for(unsigned int i = 0; i < t.restaurants.size(); i++) {
if(i == t.restaurants.size() - 1) {
os << t.restaurants[i];
}
else {
os << t.restaurants[i]<< " ,";
}
}
os << "]";
return os;
}
bool tournament::populate_restaurants(string filename) {
try {
restaurants = parse_file(filename);
return true;
}
catch(runtime_error e) {
cerr << e.what() << endl;
return false;
}
}
bool tournament::already_in(string name) {
for(unsigned int i = 0; i < restaurants.size(); i++) {
if(restaurants[i].name == name) {
return true;
}
}
return false;
}
void tournament::add_restaurants() {
string name;
int score = 0;
cout << "name" << endl;
cin.ignore();
getline(cin, name);
if(not already_in(name)) {
restaurants.push_back(restaurant(name, score));
}
}
void tournament::remove_restaurants() {
string name;
cout << "name" << endl;
cin.ignore();
getline(cin, name);
if(already_in(name)) {
for(unsigned int i = 0; i < restaurants.size(); i++) {
if(restaurants[i].name == name){
restaurants.erase(restaurants.begin() + i);
}
}
}
}
void tournament::shuffle() {
random_shuffle(restaurants.begin(), restaurants.end());
}
int round_counter(int current_size) {
int round;
if(current_size == 32) {
round = 5;
}
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 == 32) {
match = 16;
}
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;
}
bool tournament::start_tournament(){
if(not verify_size()) {
cout << "not 2^n" << endl;
return false;
}
bool temp_bool = true;
bool tourn_running = true;
cin.ignore();
string name;
int match = match_counter(restaurants.size());
int round = round_counter(restaurants.size());
int current_round = 1;
vector<restaurant> temp_array;
while(tourn_running == true) {
int current_match = 1;
temp_bool = true;
cin.ignore();
for(unsigned int i = 0; i < restaurants.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].name or name == restaurants[i + 1].name) {
if(name == restaurants[i].name) {
restaurants[i].score++;
temp_array.push_back(restaurants[i]);
losers.push_back(restaurants[i+1]);
}
else {
restaurants[i+1].score++;
temp_array.push_back(restaurants[i + 1]);
losers.push_back(restaurants[i]);
}
temp_bool = false;
current_match++;
}
else {
cout << "invalid response " << name << endl;
}
}
temp_bool = true;
}
while(temp_bool == true) {
for(unsigned int i = 0; i < temp_array.size(); i++) {
restaurants[i] = temp_array[i];
}
for(unsigned int i = 0; i < temp_array.size(); i++){
restaurants.pop_back();
}
if(restaurants.size() == 1) {
losers.push_back(restaurants[0]);
cout << "this is the winner " << restaurants[0] << endl;
restaurants = losers;
return true;
}
else {
cout << "next round of tournament" << endl;
temp_bool = false;
}
}
temp_array.clear();
match = match/2;
current_round++;
}
return false;
}
bool tournament::verify_size() {
int r = restaurants.size();
if(r == 2 or r == 4 or r == 8 or r == 16 or r == 32 or r == 64) {
return true;
}
return false;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef __TOURNAMENT_H__
#define __TOURNAMENT_H__
#include <vector>
#include "restaurant.h"
using namespace std;
class tournament {
private:
vector<restaurant> restaurants;
vector<restaurant> losers;
public:
tournament(){};
void display_restaurant();
bool populate_restaurants(string filename);
bool already_in(string name);
void add_restaurants();
void remove_restaurants();
void shuffle();
bool start_tournament();
bool verify_size();
friend ostream & operator<<(ostream & os, tournament & t);
};
#endif