Browse Source

adding cs142 code

master
Derek McQuay 4 years ago
parent
commit
8e52ce1982
100 changed files with 11012 additions and 0 deletions
  1. +31
    -0
      cs142/final/Makefile
  2. +14
    -0
      cs142/final/administrator.cpp
  3. +15
    -0
      cs142/final/administrator.h
  4. +263
    -0
      cs142/final/campus.cpp
  5. +27
    -0
      cs142/final/campus.h
  6. +35
    -0
      cs142/final/employee.cpp
  7. +27
    -0
      cs142/final/employee.h
  8. +32
    -0
      cs142/final/faculty.cpp
  9. +19
    -0
      cs142/final/faculty.h
  10. +19
    -0
      cs142/final/hourly.cpp
  11. +15
    -0
      cs142/final/hourly.h
  12. +48
    -0
      cs142/final/main.cpp
  13. +13
    -0
      cs142/final/salaried.cpp
  14. +15
    -0
      cs142/final/salaried.h
  15. +46
    -0
      cs142/final/tags
  16. +4
    -0
      cs142/final/test.txt
  17. +46
    -0
      cs142/final/util.cpp
  18. +14
    -0
      cs142/final/util.h
  19. +6
    -0
      cs142/lab00/Makefile
  20. +10
    -0
      cs142/lab00/hello_world.cpp
  21. +17
    -0
      cs142/lab01/byu.cpp
  22. +22
    -0
      cs142/lab01/quiz.txt
  23. +10
    -0
      cs142/lab02/Makefile
  24. +57
    -0
      cs142/lab02/binary.cpp
  25. +20
    -0
      cs142/lab02/temperature.cpp
  26. +30
    -0
      cs142/lab02/timer.cpp
  27. +4
    -0
      cs142/lab03/Makefile
  28. +13
    -0
      cs142/lab03/class/main.cpp
  29. +60
    -0
      cs142/lab03/class/person.cpp
  30. +24
    -0
      cs142/lab03/class/person.h
  31. +304
    -0
      cs142/lab03/grade.cpp
  32. +16
    -0
      cs142/lab03/quick.cpp
  33. +59
    -0
      cs142/lab03/test.txt
  34. +7
    -0
      cs142/lab04/Makefile
  35. +53
    -0
      cs142/lab04/loops.py
  36. +111
    -0
      cs142/lab04/number.cpp
  37. +17
    -0
      cs142/lab04/quick.cpp
  38. +10
    -0
      cs142/lab05/Makefile
  39. +10
    -0
      cs142/lab05/game/Makefile
  40. +18
    -0
      cs142/lab05/game/game.cpp
  41. +175
    -0
      cs142/lab05/game/monopoly.cpp
  42. +40
    -0
      cs142/lab05/game/properties/properties_in_order.txt
  43. +182
    -0
      cs142/lab05/monopoly.cpp
  44. +6
    -0
      cs142/lab06/Makefile
  45. +211
    -0
      cs142/lab06/add-display.cpp
  46. +42
    -0
      cs142/lab06/array.cpp
  47. +10
    -0
      cs142/lab06/test.txt
  48. +29
    -0
      cs142/lab07/Makefile
  49. +250
    -0
      cs142/lab07/cookies.cpp
  50. +18
    -0
      cs142/lab07/test.txt
  51. +18
    -0
      cs142/lab08/Makefile
  52. +106
    -0
      cs142/lab08/main.cpp
  53. +47
    -0
      cs142/lab08/person.cpp
  54. +24
    -0
      cs142/lab08/person.h
  55. +43
    -0
      cs142/lab08/test.cc
  56. +21
    -0
      cs142/lab08/test.txt
  57. +29
    -0
      cs142/lab09/Makefile
  58. +30
    -0
      cs142/lab09/io.cpp
  59. +11
    -0
      cs142/lab09/io.h
  60. +27
    -0
      cs142/lab09/iotest.cpp
  61. +249
    -0
      cs142/lab09/jsoncpp/json/json-forwards.h
  62. +1855
    -0
      cs142/lab09/jsoncpp/json/json.h
  63. +4230
    -0
      cs142/lab09/jsoncpp/jsoncpp.cpp
  64. +75
    -0
      cs142/lab09/main.cpp
  65. +8
    -0
      cs142/lab09/restaurant.cpp
  66. +16
    -0
      cs142/lab09/restaurant.h
  67. +5
    -0
      cs142/lab09/test.json
  68. +196
    -0
      cs142/lab09/tournament.cpp
  69. +25
    -0
      cs142/lab09/tournament.h
  70. +56
    -0
      cs142/lab10/ArenaInterface.h
  71. +176
    -0
      cs142/lab10/Battle.h
  72. +7
    -0
      cs142/lab10/Factory.cpp
  73. +21
    -0
      cs142/lab10/Factory.h
  74. +200
    -0
      cs142/lab10/FighterInterface.h
  75. +33
    -0
      cs142/lab10/Makefile
  76. +21
    -0
      cs142/lab10/archer.cpp
  77. +21
    -0
      cs142/lab10/archer.h
  78. +111
    -0
      cs142/lab10/arena.cpp
  79. +23
    -0
      cs142/lab10/arena.h
  80. +59
    -0
      cs142/lab10/cleric.cpp
  81. +24
    -0
      cs142/lab10/cleric.h
  82. +72
    -0
      cs142/lab10/fighter.cpp
  83. +44
    -0
      cs142/lab10/fighter.h
  84. +205
    -0
      cs142/lab10/main.cpp
  85. +30
    -0
      cs142/lab10/robot.cpp
  86. +23
    -0
      cs142/lab10/robot.h
  87. +60
    -0
      cs142/lab10/util.cpp
  88. +14
    -0
      cs142/lab10/util.h
  89. +14
    -0
      cs142/smcquay/cootie/Makefile
  90. +42
    -0
      cs142/smcquay/cootie/cootie.cc
  91. +25
    -0
      cs142/smcquay/cootie/dice.cc
  92. +8
    -0
      cs142/smcquay/cootie/dice.h
  93. +67
    -0
      cs142/smcquay/cootie/player.cc
  94. +24
    -0
      cs142/smcquay/cootie/player.h
  95. +9
    -0
      cs142/smcquay/fun_with_formatting/Makefile
  96. +34
    -0
      cs142/smcquay/fun_with_formatting/main.cc
  97. +7
    -0
      cs142/smcquay/go/lab00/Makefile
  98. +7
    -0
      cs142/smcquay/go/lab00/hello.go
  99. +7
    -0
      cs142/smcquay/go/lab01/Makefile
  100. +29
    -0
      cs142/smcquay/go/lab01/trivia.go

+ 31
- 0
cs142/final/Makefile View File

@@ -0,0 +1,31 @@
CXXFLAGS= -Wall -g
OBJECTS=main.o employee.o hourly.o salaried.o administrator.o faculty.o util.o campus.o
EXE=main

all: $(EXE)


$(EXE): main.o
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@

main.o: main.cpp employee.o hourly.o salaried.o administrator.o faculty.o util.o campus.o
employee.o: employee.h employee.cpp
hourly.o: hourly.cpp hourly.h employee.h
salaried.o: salaried.h salaried.cpp employee.h
administrator.o: administrator.cpp administrator.h employee.h
faculty.o: faculty.cpp faculty.h employee.h
util.o: util.h util.cpp
campus.o: campus.cpp campus.h

run: $(EXE)
@./$(EXE)

clean:
@rm -vf *.o
@rm -vf $(EXE)

debug: $(EXE)
gdb ./$(EXE)

valgrind: $(EXE)
valgrind --tool=memcheck --leak-check=yes ./$(EXE)

+ 14
- 0
cs142/final/administrator.cpp View File

@@ -0,0 +1,14 @@
#include "administrator.h"
#include "employee.h"

using namespace std;

administrator::administrator(string name, double pay_rate, int hours, string type) :
employee(name, pay_rate, hours, type) {}

int administrator::get_salary() {
if(hours > 170) {
pay_rate += pay_rate * .1;
}
return pay_rate;
}

+ 15
- 0
cs142/final/administrator.h View File

@@ -0,0 +1,15 @@
#ifndef __ADMINISTRATOR_H__
#define __ADMINISTRATOR_H__

#include <iostream>
#include "employee.h"

using namespace std;

class administrator : public employee {
public:
administrator(string name, double pay_rate, int hours, string type);
int get_salary();
};

#endif

+ 263
- 0
cs142/final/campus.cpp View File

@@ -0,0 +1,263 @@
#include "campus.h"
#include "salaried.h"
#include "hourly.h"
#include "faculty.h"
#include "administrator.h"
#include "util.h"
#include <iostream>
#include <vector>
#include <cstdlib>
#include <fstream>

bool campus::contains(string name) { //function used to verify if the string is already included
employee * f = NULL;
for(unsigned int i = 0; i < workers.size(); i++) {
f = dynamic_cast<employee *>(workers[i]);
if(f->get_name() == name) {
return true;
}
}
return false;
}

void campus::add_worker_from_parse(string name, string type, double pay_rate) { //add worker for parsed input
bool passed = false;
employee * h = NULL;
int hours = 0;
if(type == "H") {
h = new hourly(name, pay_rate, hours, type);
passed = true;
}
else if(type == "S") {
h = new salaried(name, pay_rate, hours, type);
passed = true;
}
else if(type == "F") {
h = new faculty(name, pay_rate, hours, type);
passed = true;
}
else if(type == "A") {
h = new administrator(name, pay_rate, hours, type);
passed = true;
}
if(passed == true) {
workers.push_back(h);
}
}

void campus::add_worker() { //general add worker function which it gets the input
employee * h = NULL;
bool passed;
string info;
string name, type;
int hours = 0;
double pay_rate = 0;
cout << "Enter info in following format" << endl;
cout << "type pay_rate name" << endl;;
getline(cin, info);
passed = parse_n_load(info, name, type, pay_rate);
if(contains(name) == true) {
cout << "already exists" << endl;
return;
}
if(passed == false) {
cout << "incorrect entry" << endl;
}
if(info[0] == ' ') {
cout << "incorrect entry" << endl;
passed = false;
}
else if(type == "H") {
h = new hourly(name, pay_rate, hours, type);
}
else if(type == "S") {
h = new salaried(name, pay_rate, hours, type);
}
else if(type == "F") {
h = new faculty(name, pay_rate, hours, type);
}
else if(type == "A") {
h = new administrator(name, pay_rate, hours, type);
}
if(passed == true) {
workers.push_back(h);
}
}
void campus::delete_worker() {
string name;
employee * h = NULL;
cout << "Enter name of worker to delete" << endl;
getline(cin, name);
for(unsigned int i = 0; i < workers.size(); i++) {
h = dynamic_cast<employee *>(workers[i]);
if(h->get_name() == name) {
delete h;
workers.erase(workers.begin() + i);
}
}
}

void campus::display_worker() { //allows for display of all employees or selected
string input;
employee * h = NULL;
cout << "(a)ll workers or (o)ne worker?" << endl;
cin >> input;
cin.ignore();
cout << "type // payrate // name // hours // salary" << endl;
if(input == "a") {
for(unsigned int i = 0; i < workers.size(); i++){
h = dynamic_cast<employee *>(workers[i]);
cout << *workers[i] << " $" << h->get_salary() << endl;
}
}
if(input == "o") {
cout << "name: ";
employee * h = NULL;
getline(cin, input);
for(unsigned int i = 0; i < workers.size(); i++) {
h = dynamic_cast<employee *>(workers[i]);
if(h->get_name() == input) {
cout << *h << " $" << h->get_salary() << endl;
}
}

}
}

void campus::sort_name() {
bool ready = true;
employee * h = NULL;
employee * j = NULL;
while(ready) {
for(unsigned int i = 0; i < workers.size() -1; i++) {
h = dynamic_cast<employee *>(workers[i]);
j = dynamic_cast<employee *>(workers[i+1]);
ready = false;
if(h->get_name() > j->get_name()) {
workers.push_back(workers[i]);
workers[i] = workers[i+1];
workers[i+1] = workers[workers.size()-1];
workers.pop_back();
ready = true;
}
}
}
}

void campus::sort_pay() {
bool ready = true;
employee * h = NULL;
employee * j = NULL;
while(ready) {
for(unsigned int i = 0; i < workers.size() -1; i++) {
h = dynamic_cast<employee *>(workers[i]);
j = dynamic_cast<employee *>(workers[i+1]);
ready = false;
if(h->get_payrate() > j->get_payrate()) {
workers.push_back(workers[i]);
workers[i] = workers[i+1];
workers[i+1] = workers[workers.size()-1];
workers.pop_back();
ready = true;
}
}
}
}

void campus::sort_worker() { //allows for either sort by name or by pay
string input;
cout << "sort by (n)ame or by (p)ay?" << endl;
cin >> input;
if(input == "n") {
sort_name();
}
if(input == "p") {
sort_pay();
}
}

void campus::advance_month() {
employee * h = NULL;
for(unsigned int i = 0; i < workers.size(); i++) {
h = dynamic_cast<employee *>(workers[i]);
h->reset_hours();
}
}

void campus::set_hours() {
string name;
employee * h = NULL;
cout << "enter name of employee" << endl;
getline(cin, name);
for(unsigned int i = 0; i < workers.size(); i++) {
h = dynamic_cast<employee *>(workers[i]);
if(h->get_name() == name) {
int input;
cout << "enter hours" << endl;
cin >> input;
cin.ignore();
h->set_hours(input);
}
}
}

void campus::add_files(campus c) { //function for i/o. can either write to a file or take a file in
string add_or_write;
cout << "(a)dd or (w)rite to file?" << endl;
cin >> add_or_write;
cin.ignore();
if(add_or_write == "a") {
bool file_correct = false;
string input, input_file_name;
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;
string lineread;
while(getline(inputs, lineread)) {
bool worked;
string name, type;
double pay_rate;
worked = parse_n_load(lineread, name, type, pay_rate);
if(worked == true) {
add_worker_from_parse(name, type, pay_rate);
}
lineread = "";
}
}
else {
cerr << "incorrect file name" << endl;
}
}
}
if(add_or_write == "w") {
bool file_correct = false;
string input, input_file_name;
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 << c;
file_correct = true;
}
else {
cerr << "incorrect file name" << endl;
}
}
}
}

ostream & operator<<(ostream & os, campus & t) { //makes writing out to files much easier
employee * h = NULL;
for(unsigned int i = 0; i < t.workers.size(); i++) {
h = dynamic_cast<employee *>(t.workers[i]);
os << h->get_type() << " " << h->get_payrate() << " " << h->get_name() << "\n";
}
return os;
}

+ 27
- 0
cs142/final/campus.h View File

@@ -0,0 +1,27 @@
#ifndef __CAMPUS_H__
#define __CAMPUS_H__

#include "employee.h"
#include <vector>

using namespace std;

class campus {
private:
vector<employee*> workers;
public:
void add_worker();
void add_worker_from_parse(string name, string type, double pay_rate);
void delete_worker();
void display_worker();
void sort_worker();
void advance_month();
void set_hours();
void add_files(campus c);
void sort_name();
void sort_pay();
bool contains(string name);
friend ostream & operator<<(ostream & os, campus & t);
};

#endif

+ 35
- 0
cs142/final/employee.cpp View File

@@ -0,0 +1,35 @@
#include "employee.h"
#include <iostream>
using namespace std;

employee::employee(string name, double pay_rate, int hours, string type) :
name(name), pay_rate(pay_rate), hours(hours), type(type) {}

string employee::get_name(){
return name;
}

double employee::get_payrate() {
return pay_rate;
}

int employee::get_hours() {
return hours;
}

string employee::get_type() {
return type;
}

void employee::reset_hours() {
hours = 0;
}

void employee::set_hours(int input) {
hours += input;
}

ostream & operator<<(ostream & os, employee & t){ //makes printing out employee easy
os << t.type << " " << t.pay_rate << " " << t.name << " " << t.hours;
return os;
}

+ 27
- 0
cs142/final/employee.h View File

@@ -0,0 +1,27 @@
#ifndef __EMPLOYEE_H__
#define __EMPLOYEE_H__

#include <iostream>

using namespace std;

class employee {
private:
string name;
protected:
string type;
double pay_rate;
int hours;
public:
employee(string name, double pay_rate, int hours, string type);
string get_name();
double get_payrate();
string get_type();
int get_hours();
void set_hours(int input);
void reset_hours();
virtual int get_salary() = 0;
friend ostream & operator<<(ostream & os, employee & t);
};

#endif

+ 32
- 0
cs142/final/faculty.cpp View File

@@ -0,0 +1,32 @@
#include "faculty.h"
#include "employee.h"
#include <cstdlib>
#include <iostream>

using namespace std;

faculty::faculty(string name, double pay_rate, int hours, string type) : employee(name, pay_rate, hours, type),
grad_students(set_grad_students()) {}

int faculty::get_salary() {
double pay_rate_return = 0;
if(grad_students > 0){
double percent;
pay_rate_return = pay_rate;
for(int i = 0; i < grad_students; i++){
percent += 0.05;
}
pay_rate_return += pay_rate*percent;
return pay_rate_return;
}
return pay_rate;
}

int faculty::get_grad_students() {
return grad_students;
}
int faculty::set_grad_students() { //gets random number of grad students for faculty
srand(time(NULL));
int students = rand() % 5;
return students;
}

+ 19
- 0
cs142/final/faculty.h View File

@@ -0,0 +1,19 @@
#ifndef __FACULTY_H__
#define __FACULTY_H__

#include <iostream>
#include "employee.h"

using namespace std;

class faculty : public employee {
private:
int grad_students;
public:
faculty(string name, double pay_rate, int hours, string type);
int get_grad_students();
int set_grad_students();
int get_salary();
};

#endif

+ 19
- 0
cs142/final/hourly.cpp View File

@@ -0,0 +1,19 @@
#include "hourly.h"
#include "employee.h"

using namespace std;

hourly::hourly(string name, double pay_rate, int hours, string type) : employee(name, pay_rate, hours, type) {}

int hourly::get_salary() {
int salary;
int overtime;
if((hours - 170) > 0) {
overtime = (hours - 170);
salary = 170 * pay_rate;
salary += overtime * (pay_rate * 1.5);
return salary;
}
salary = hours * pay_rate;
return salary;
}

+ 15
- 0
cs142/final/hourly.h View File

@@ -0,0 +1,15 @@
#ifndef __HOURLY_H__
#define __HOURLY_H__

#include <iostream>
#include "employee.h"

using namespace std;

class hourly : public employee {
public:
hourly(string name, double pay_rate, int hours, string type);
int get_salary();
};

#endif

+ 48
- 0
cs142/final/main.cpp View File

@@ -0,0 +1,48 @@
#include <iostream>
#include <fstream>
#include "employee.h"
#include "salaried.h"
#include "hourly.h"
#include "faculty.h"
#include "administrator.h"
#include "util.h"
#include "campus.h"

using namespace std;


int main() { //driver for program
campus c = campus();
bool keep_going = true;
while(keep_going) {
string input;
cout << "(a)dd worker, (r)emove worker, (d)isplay worker, add (h)ours, \n";
cout << " (s)ort workers, advance (m)onth, add (f)ile, (q)uit" << endl;
cin >> input;
cin.ignore();
if(input == "a") {
c.add_worker();
}
if(input == "r") {
c.delete_worker();
}
if(input == "h") {
c.set_hours();
}
if(input == "d") {
c.display_worker();
}
if(input == "s") {
c.sort_worker();
}
if(input == "m") {
c.advance_month();
}
if(input == "f") {
c.add_files(c); //will either take in a file or write out to one
}
if(input == "q") {
keep_going = false;
}
}
}

+ 13
- 0
cs142/final/salaried.cpp View File

@@ -0,0 +1,13 @@
#include "salaried.h"
#include "employee.h"

using namespace std;

salaried::salaried(string name, double pay_rate, int hours, string type) : employee(name, pay_rate, hours, type) {}

int salaried::get_salary() {
if(hours > 170/2) {
return pay_rate;
}
return 0;
}

+ 15
- 0
cs142/final/salaried.h View File

@@ -0,0 +1,15 @@
#ifndef __SALARIED_H__
#define __SALARIED_H__

#include <iostream>
#include "employee.h"

using namespace std;

class salaried : public employee {
public:
salaried(string name, double pay_rate, int hours, string type);
int get_salary();
};

#endif

+ 46
- 0
cs142/final/tags View File

@@ -0,0 +1,46 @@
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
!_TAG_PROGRAM_NAME Exuberant Ctags //
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
!_TAG_PROGRAM_VERSION 5.9~svn20110310 //
CXXFLAGS Makefile /^CXXFLAGS= -Wall -g$/;" m
EXE Makefile /^EXE=main$/;" m
OBJECTS Makefile /^OBJECTS=main.o employee.o hourly.o salaried.o administrator.o faculty.o util.o$/;" m
__ADMINISTRATOR_H__ administrator.h 2;" d
__EMPLOYEE_H__ employee.h 2;" d
__FACULTY_H__ faculty.h 2;" d
__HOURLY_H__ hourly.h 2;" d
__SALARIED_H__ salaried.h 2;" d
__UTIL_H__ util.h 2;" d
administrator administrator.cpp /^administrator::administrator(string name, double pay_rate, int hours) : employee(name, pay_rate, hours) {}$/;" f class:administrator
administrator administrator.h /^class administrator : public employee {$/;" c
employee employee.cpp /^employee::employee(string name, double pay_rate, int hours) :$/;" f class:employee
employee employee.h /^class employee {$/;" c
faculty faculty.cpp /^faculty::faculty(string name, double pay_rate, int hours) : employee(name, pay_rate, hours),$/;" f class:faculty
faculty faculty.h /^class faculty : public employee {$/;" c
get_grad_students faculty.cpp /^int faculty::get_grad_students() {$/;" f class:faculty
get_hours employee.cpp /^int employee::get_hours() {$/;" f class:employee
get_name employee.cpp /^string employee::get_name(){$/;" f class:employee
get_payrate employee.cpp /^double employee::get_payrate() {$/;" f class:employee
get_salary administrator.cpp /^int administrator::get_salary() {$/;" f class:administrator
get_salary faculty.cpp /^int faculty::get_salary() {$/;" f class:faculty
get_salary hourly.cpp /^int hourly::get_salary() {$/;" f class:hourly
get_salary salaried.cpp /^int salaried::get_salary() {$/;" f class:salaried
grad_students faculty.h /^ int grad_students;$/;" m class:faculty
hourly hourly.cpp /^hourly::hourly(string name, double pay_rate, int hours) : employee(name, pay_rate, hours) {}$/;" f class:hourly
hourly hourly.h /^class hourly : public employee {$/;" c
hours employee.h /^ int hours;$/;" m class:employee
main main.cpp /^int main() {$/;" f
name employee.h /^ string name;$/;" m class:employee
parse_n_load util.cpp /^bool parse_n_load(const string & input, string & name, string & type,$/;" f
pay_rate employee.h /^ double pay_rate;$/;" m class:employee
salaried salaried.cpp /^salaried::salaried(string name, double pay_rate, int hours) : employee(name, pay_rate, hours) {}$/;" f class:salaried
salaried salaried.h /^class salaried : public employee {$/;" c
set_grad_students faculty.cpp /^int faculty::set_grad_students() {$/;" f class:faculty
test_admin main.cpp /^void test_admin() {$/;" f
test_faculty main.cpp /^void test_faculty() {$/;" f
test_hourly main.cpp /^void test_hourly() {$/;" f
test_salaried main.cpp /^void test_salaried() {$/;" f
test_util main.cpp /^void test_util() {$/;" f
tokenize util.cpp /^vector<string> tokenize(const string & str, const string & delimiters) {$/;" f

+ 4
- 0
cs142/final/test.txt View File

@@ -0,0 +1,4 @@
A 10.5 zombie
alka alkjsa
F 152 stephen is awesome
H 15.243 cremeballs. creame

+ 46
- 0
cs142/final/util.cpp View File

@@ -0,0 +1,46 @@
#include <vector>
#include <string>
using namespace std;
#include <cstdlib>
#include "util.h"
#include <iostream>

vector<string> tokenize(const string & str, const string & delimiters) {
//tokenizes a string by the delimiter passed in
vector<string> tokens;
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos or string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
return tokens;
}

bool parse_n_load(const string & input, string & name, string & type,
double & pay_rate) {
vector<string> tokens = tokenize(input);
if(tokens.size() == 0) {
return false;
}
type = tokens[0];
if (not ( type == "A"
or type == "F"
or type == "H"
or type == "S")) {
return false;
}
pay_rate = atof(tokens[1].c_str());
if(pay_rate == 0) {
return false;
}
for(unsigned int i = 2; i < tokens.size(); i++) {
name += tokens[i];
if(i < tokens.size() - 1) {
name += " ";
}
}
return true;
}

+ 14
- 0
cs142/final/util.h View File

@@ -0,0 +1,14 @@
#ifndef __UTIL_H__
#define __UTIL_H__

#include <string>
#include <vector>

using namespace std;

void print(const string & in);
vector<string> tokenize(const string & str, const string & delimiters=" ");
bool parse_n_load(const string & input, string & name, string & type,
double & pay_rate);

#endif

+ 6
- 0
cs142/lab00/Makefile View File

@@ -0,0 +1,6 @@
CPPFLAGS=-Wall -g -std=c++0x

all: hello_world

clean:
rm -rv hello_world

+ 10
- 0
cs142/lab00/hello_world.cpp View File

@@ -0,0 +1,10 @@
#include <iostream>

using namespace std;

int main() {

cout << "Hello, world! My name is Derek McQuay."<< endl;
cin.get();
return 0;
}

+ 17
- 0
cs142/lab01/byu.cpp View File

@@ -0,0 +1,17 @@
#include <iostream>

using namespace std;

int main() {


cout << " Basic BYU Trivia\n" << endl;
cout << " Questions Answers \n" << endl;
cout << "What was the original name of BYU? Brigham Young Academy" << endl;
cout << "When was BYA established? 1875" << endl;
cout << "Who was the first \"permanent\" principal of BYA? Karl G. Maeser" << endl;
cout << "When did BYA become BYU? 1903" << endl;
cout << "To what sports conference do we belong? Mountain West Conference(MWC)" << endl;
cout << "When did BYU win the national football title? 1984" << endl;
cout << "Who won the Heisman Trophy in 1990? Ty Detmer" << endl;
}

+ 22
- 0
cs142/lab01/quiz.txt View File

@@ -0,0 +1,22 @@
A
D
B
A
B
C
B
B
B
B
D
B
B
C
A
C
D
A,C
B
B
A,B,C,D
so we can learn by doing rather than let someone else learn.

+ 10
- 0
cs142/lab02/Makefile View File

@@ -0,0 +1,10 @@
CPPFLAGS=-Wall -g -std=c++0x

all: timer temperature binary

binary: binary.cpp
temperature: temperature.cpp
timer: timer.cpp

clean:
rm -rv timer temperature binary

+ 57
- 0
cs142/lab02/binary.cpp View File

@@ -0,0 +1,57 @@
#include <iostream>
#include <vector>

using namespace std;

int main() {

int value;
int first, second, third, fourth, fifth;
vector<int> final;

cout << "please insert your number between 0 and 31" << endl;
cin >> value;
int prin = value%2;
value = value/2;
if(prin >=1)
first = 1;
else
first = 0;
final.push_back(first);
prin = value%2;
value = value/2;
if(prin >=1)
second = 1;
else
second = 0;
final.push_back(second);
prin = value%2;
value = value/2;
if(prin >= 1)
third = 1;
else
third = 0;
final.push_back(third);
prin = value%2;
value = value/2;
if(prin >= 1)
fourth = 1;
else
fourth = 0;
final.push_back(fourth);
prin = value%2;
value = value/2;
if(prin >= 1)
fifth = 1;
else
fifth = 0;
final.push_back(fifth);

cout <<fifth<<fourth<<third<<second<<first << endl;
// for(unsigned int i = 0 ; i<final.size();i++) {

// cout << final[i];
// }
cout << "\n";

}

+ 20
- 0
cs142/lab02/temperature.cpp View File

@@ -0,0 +1,20 @@
#include <iostream>

using namespace std;

int main() {

double temperature;
cout << "please enter a temperature in degress Fahrenheit:" << endl;
cin >> temperature;
double celsius = (temperature-32) * 5/9;
cout << "This is Fahrenheit as an int:" << endl;
cout << (int) temperature << endl;
cout << "This is Celsius as a double:" << endl;
cout << celsius << endl;
cout << "This is Celsius as an int:" << endl;
cout << (int) celsius << endl;



}

+ 30
- 0
cs142/lab02/timer.cpp View File

@@ -0,0 +1,30 @@
#include <iostream>
#include <cmath>

using namespace std;
void calculate (int a) {

int hours, minutes, seconds;

hours = (a / 3600);
minutes = (a / 60) - (hours * 60);
seconds = a % 60;

cout << "hours: " << hours << " minutes: " << minutes << " seconds: " << seconds << endl;

};


int main() {

int time;
cout << "please enter a number of seconds" << endl;
cin >> time;
cout << time << endl;
cout << "absolute value of input " << abs (time) << endl;
calculate(time);
cout << "now with sqrt: " << endl;
calculate(sqrt(time));
}


+ 4
- 0
cs142/lab03/Makefile View File

@@ -0,0 +1,4 @@
CXXFLAGS=-Wall -g -std=c++0x
all: grade
clean:
rm -vf grade

+ 13
- 0
cs142/lab03/class/main.cpp View File

@@ -0,0 +1,13 @@
#include <iostream>

using namespace std;

#include "person.h"

#pragma once

int main(int argc, char * argv[]) {

person p;
p.get_grades();
}

+ 60
- 0
cs142/lab03/class/person.cpp View File

@@ -0,0 +1,60 @@
using namespace std;

#include "person.h"

#pragma once

void person::get_latedays() {

cout << "Please enter if it was turned in earlier or late (earlier with a negative number): " << endl;
cin >> late_days;
total_latedays = total_latedays + late_days;
}

void person::get_exam_latedays(){
cout << "Please enter if it was turned in in late (number of late days): " << endl;
cin >> late_days_exam;
late_days_exam = abs(late_days_exam);
total_latedays_exam = total_latedays_exam + late_days_exam;
}

void person::calculate_grade() {

grade = lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7 + lab8 + lab9 + lab10 + lab11 + exam1 + exam2 + final;
}


void person::get_grades(){

cout << "please enter the name of the student: " << endl;
cout << "now we are going to enter the following grades for the student: " << endl;
cout << "Grade for lab 01: " << endl;
cin >> lab1;
cout << "Grade for lab 02; " << endl;
cin >> lab2;
cout << "Grade for lab 03; " << endl;
cin >> lab3;
cout << "Grade for lab 04; " << endl;
cin >> lab4;
cout << "Grade for lab 05; " << endl;
cin >> lab5;
cout << "Grade for lab 06; " << endl;
cin >> lab6;
cout << "Grade for lab 07; " << endl;
cin >> lab7;
cout << "Grade for lab 08; " << endl;
cin >> lab8;
cout << "Grade for lab 09; " << endl;
cin >> lab9;
cout << "Grade for lab 10; " << endl;
cin >> lab10;
cout << "Grade for lab 11; " << endl;
cin >> lab11;
cout << "Grade for exam 1: " << endl;
cin >> exam1;
cout << "Grade for exam 2: " << endl;
cin >> exam2;
cout << "Grade for final: " << endl;
cin >> final;
cout << "Please enter how many days late the final was: " << endl;
}

+ 24
- 0
cs142/lab03/class/person.h View File

@@ -0,0 +1,24 @@
#include <iostream>
#include <cmath>

using namespace std;

class person
{

private:
string person_name;
int lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9, lab10, lab11, exam1, exam2, final;
int late_days, total_latedays, late_days_exam, total_latedays_exam;
int grade;

public:

person(){};
void get_latedays();
void get_exam_latedays();
void calculate_grade();
void get_grades();

};

+ 304
- 0
cs142/lab03/grade.cpp View File

@@ -0,0 +1,304 @@
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

struct student {
string name;
int grade;
string letter_grade;

};

int main() {
// this is incorrect: typedef struct student1;
struct student student1;
struct student student2;
int lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9, lab10, lab11;
int exam1, exam2, final;
int late_days;
int late_daytotal = 0;
int exam_late_days;
int exam_late_days_total = 0;
string name;

for(int i = 1; i<3; i++){
cout << "Please enter name of student: " << endl;
cin >> name;
cout << "Please enter grade for lab 1: " << endl;
cin >> lab1;
if(lab1 >20 or lab1 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-2 or late_days > 2){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 2: " << endl;
cin >> lab2;
if(lab2 >20 or lab2 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-2 or late_days > 2){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 3: " << endl;
cin >> lab3;
if(lab3 >30 or lab3 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-3 or late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 4: " << endl;
cin >> lab4;
if(lab4 >30 or lab4 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-3 or late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 5: " << endl;
cin >> lab5;
if(lab5 >30 or lab5 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-3 or late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 6: " << endl;
cin >> lab6;
if(lab6 >30 or lab6 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-3 or late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 7: " << endl;
cin >> lab7;
if(lab7 >30 or lab7 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-3 or late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 8: " << endl;
cin >> lab8;
if(lab8 >30 or lab8 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-3 or late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 9: " << endl;
cin >> lab9;
if(lab9 >20 or lab9 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-2 or late_days > 2){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 10: " << endl;
cin >> lab10;
if(lab10 >20 or lab10 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-2 or late_days > 2){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter grade for lab 11: " << endl;
cin >> lab11;
if(lab11 >40 or lab11 < 0){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?: (indicate early days by negative number, late days by positive" << endl;
cin >> late_days;
if(late_days <-4 or late_days > 4){
cout << "This is not in the range!" << endl;
return 0;
}
late_daytotal = late_daytotal + late_days;
cout << "Please enter exam 1 score: " << endl;
cin >> exam1;
if(exam1 < 0 or exam1 > 100){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?" << endl;
cin >> exam_late_days;
if(exam_late_days < 0 or exam_late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
exam_late_days_total = exam_late_days_total + exam_late_days;
cout << "Please enter exam 2 score: " << endl;
cin >> exam2;
if(exam2 < 0 or exam2 > 100){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?" << endl;
cin >> exam_late_days;
if(exam_late_days < 0 or exam_late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
exam_late_days_total = exam_late_days_total + exam_late_days;
cout << "Please enter final score: " << endl;
cin >> final;
if(final < 0 or final > 100){
cout << "This is not in the range!" << endl;
return 0;
}
cout << "Were there late days/early days?" << endl;
cin >> exam_late_days;
if(exam_late_days < 0 or exam_late_days > 3){
cout << "This is not in the range!" << endl;
return 0;
}
exam_late_days_total = exam_late_days_total + exam_late_days;

int total_labs = lab1+lab2+lab3+lab4+lab5+lab6+lab7+lab8+lab9+lab10+lab11;
int total_exam = exam1 + exam2 + final;
int final_grade = total_labs + total_exam;
if(late_daytotal < 0){
final_grade = final_grade + abs(late_daytotal);
}
else{
final_grade = final_grade - late_daytotal;
}
final_grade = final_grade - exam_late_days_total*20;
string letter_grade;
if(final_grade >= 570)
letter_grade = "A";
if(final_grade <570 && final_grade >=540)
letter_grade = "A-";
if(final_grade < 540 && final_grade >= 522)
letter_grade = "B+";
if(final_grade < 522 && final_grade >= 498)
letter_grade = "B";
if(final_grade < 498 && final_grade >= 480)
letter_grade = "B-";
if(final_grade < 480 && final_grade >= 462)
letter_grade = "C+";
if(final_grade < 462 && final_grade >= 438)
letter_grade = "C";
if(final_grade < 438 && final_grade >= 420)
letter_grade = "C-";
if(final_grade < 420 && final_grade >= 402)
letter_grade = "D+";
if(final_grade < 402 && final_grade >= 378)
letter_grade = "D";
if(final_grade < 378 && final_grade >= 360)
letter_grade = "D-";
if(final_grade < 360)
letter_grade = "E";

// something looks iffy here: (check your warnings (read all the words (even 'assignment')))
if (i == 1) {
student1.name = name;
student1.grade = final_grade;
student1.letter_grade = letter_grade;
}
else {
student2.name = name;
student2.grade = final_grade;
student2.letter_grade = letter_grade;
}
}
int order = 0;
cout << "Please enter what order you want to see the students: " << endl;
cout << "enter 1 for alphabetical or 2 for highest score first: " << endl;
cin >> order;
if(order > 2 or order < 1) {
cout << "out of range" <<endl;
return 0;
}
if(order == 1){
if(student1.name < student2.name) {
cout << "name " << student1.name << endl;
cout << "points " << student1.grade << endl;
cout << "letter grade " << student1.letter_grade << endl;
cout << "name " << student2.name << endl;
cout << "points " << student2.grade << endl;
cout << "letter grade " << student2.letter_grade << endl;
}
else {
cout << "name " << student2.name << endl;
cout << "points " << student2.grade << endl;
cout << "letter grade " << student2.letter_grade << endl;
cout << "name " << student1.name << endl;
cout << "points " << student1.grade << endl;
cout << "letter grade " << student1.letter_grade << endl;
}
}
if(order == 2){
if(student1.grade > student2.grade){
cout << "name " << student1.name << endl;
cout << "points " << student1.grade << endl;
cout << "letter grade " << student1.letter_grade << endl;
cout << "name " << student2.name << endl;
cout << "points " << student2.grade << endl;
cout << "letter grade " << student2.letter_grade << endl;
}
else {
cout << "name " << student2.name << endl;
cout << "points " << student2.grade << endl;
cout << "letter grade " << student2.letter_grade << endl;
cout << "name " << student1.name << endl;
cout << "points " << student1.grade << endl;
cout << "letter grade " << student1.letter_grade << endl;
}
}
}

+ 16
- 0
cs142/lab03/quick.cpp View File

@@ -0,0 +1,16 @@
#include <iostream>
#include <string>

using namespace std;

int main () {

string a, b;
cin >> a;
cin >> b;

if(a<b)
cout << "first is before last" << endl;
else
cout << "Second is before first" << endl;
}

+ 59
- 0
cs142/lab03/test.txt View File

@@ -0,0 +1,59 @@
derek
20
-2
20
-2
30
-2
30
-2
30
-2
30
-2
30
-2
30
0
20
0
20
0
40
0
100
2
0
0
100
0
alex
20
-2
20
1
30
1
30
1
30
1
30
1
30
0
30
0
20
0
20
0
40
0
100
0
100
0
100
1
1

+ 7
- 0
cs142/lab04/Makefile View File

@@ -0,0 +1,7 @@
CXXFLAGS=-Wall -g -std=c++0x
all: number
clean:
rm -vf number

test: all
./number

+ 53
- 0
cs142/lab04/loops.py View File

@@ -0,0 +1,53 @@
import random
import time


def get_numbers(pow2, compliment):
numbers = []
constant = compliment
for i in (0, 1):
for j in (0, 1):
for k in (0, 1):
for l in (0, 1):
if pow2 == 0:
cur_digits = (i, j, k, l, constant)
if pow2 == 1:
cur_digits = (i, j, k, constant, l)
if pow2 == 2:
cur_digits = (i, j, constant, k, l)
if pow2 == 3:
cur_digits = (i, constant, j, k, l)
if pow2 == 4:
cur_digits = (constant, i, j, k, l)
numbers.append(cur_digits)
return numbers

print "example of how to use randomness to determine compliment"
a_few_tries = xrange(3)
for i in a_few_tries:
compliment = random.randint(0, 3026) % 2
for i in get_numbers(pow2=0, compliment=compliment):
print i

print "for loop to print out all numbers from 2^0 to 2^5"
for i in (0, 1):
for j in (0, 1):
for k in (0, 1):
for l in (0, 1):
for m in (0, 1):
for n in (0, 1):
print i, j, k, l, m

print "example function to convert a string in binary to integers: (see source)"
def bin2int(s):
total = 0
for i in xrange(len(s)):
if s[i] == '1':
total += 2**(len(s) - i - 1)
return total


print "here I test out the bin2int func defined above:"
test_cases = ('1010', '1011', '0100', '1000', '0111')
for test in test_cases:
print test, bin2int(test), int(test, 2)

+ 111
- 0
cs142/lab04/number.cpp View File

@@ -0,0 +1,111 @@
#include <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
#include <sstream>
#include <cmath>
#include <algorithm>

using namespace std;

int bit2dec(string s);

vector<int> get_numbers(int pow, int compliment) {
string store;
vector<int> numbers;
int constant = compliment;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
for(int l = 0; l < 2; l++){
ostringstream stream;
if(pow == 0){
stream << i << j << k << l << constant;
}
else if(pow == 1){
stream << i << j << k << constant << l;
}
else if(pow == 2){
stream << i << j << constant << k << l;
}
else if(pow == 3){
stream << i << constant << j << k << l;
}
else if(pow == 4){
stream << constant << i << j << k << l;
}
store = stream.str();
numbers.push_back(bit2dec(store));
}
}
}
}
return numbers;
}


ostream & operator<<(ostream & os, const vector<int> & nums) {
for(unsigned int i = 0; i < nums.size(); i++){
if(i % 4 == 0){
os << endl;
}
os << nums[i] << " ";
}
return os;
}

int bit2dec(string s) {

int total = 0;
int power = 0;
for(int i = s.size() - 1; i >= 0; i--) {
if(s[i] == '1') {
total += pow(2,power);
}
power++;
}
return total;
}


int main() {

srand (time(NULL));
int keep_playing = 0;
while(keep_playing == 0){
ostringstream answer;
for(unsigned int i = 0; i < 5 ; i++) {
int switcher = rand() % 2;
string check;
cout << "Is your number in this list? enter y for yes n for no" << endl;
cout << get_numbers(i, switcher) << endl;
cin >> check;
if(switcher == 0){
if(check == "y") {
answer << '0';
}
else {
answer << '1';
}
}
if(switcher == 1) {
if(check == "y") {
answer << '1';
}
else {
answer << '0';
}
}
}
string final = answer.str();
reverse (final.begin(), final.end());
cout << bit2dec(final) << endl;
cout << "Do you want to keep playing? enter y for yes, n for no" << endl;
string check;
cin >> check;
if(check == "n"){
keep_playing++;
}
}
return 0;
}

+ 17
- 0
cs142/lab04/quick.cpp View File

@@ -0,0 +1,17 @@
#include <iostream>
#include <vector>

using namespace std;

int main() {

vector<int> numbers;

numbers.push_back(0);
numbers.push_back(1);
numbers.push_back(0);
numbers.push_back(1);
numbers.push_back(0);

cout << numbers[0] << numbers[1]<< numbers[2]<<numbers[3] << numbers[4] <<endl;
}

+ 10
- 0
cs142/lab05/Makefile View File

@@ -0,0 +1,10 @@
CXXFLAGS=-Wall -g -std=c++0x
all: monopoly
clean:
rm -vf monopoly

test: all
./monopoly

debug: all
gdb monopoly

+ 10
- 0
cs142/lab05/game/Makefile View File

@@ -0,0 +1,10 @@
CXXFLAGS=-Wall -g -std=c++0x
all: monopoly
clean:
rm -vf monopoly

test: all
./monopoly

debug: all
gdb monopoly

+ 18
- 0
cs142/lab05/game/game.cpp View File

@@ -0,0 +1,18 @@
#include <iostream>
#include <vector>

using namespace std;

struct property {
string name;
int value;
}
void create_board(vector<property> & board) {
board.push_back(property{"mediterranean Avenue", 60}
}

int main() {
vector<property> board;
cout << board.name << endl;

}

+ 175
- 0
cs142/lab05/game/monopoly.cpp View File

@@ -0,0 +1,175 @@
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>

using namespace std;


struct property{
string name;
int value;
};

void roll_the_dice() {
srand(time(NULL));
int roll = rand() % 6 +1;
int roll2 = rand() % 6 +1;
cout << roll << " + "<< roll2 << " equals " << roll +roll2 << endl;
}

void report_current_money(int & current_money) {
cout << "this is your current money" << endl;
cout << current_money << endl;
}

void add_money(int & current_money) {
cout << "how much money would you like to add? " << endl;
int add;
cin >> add;
if(add < 0){
cout << "please enter a positive number" << endl;
add_money(current_money);
}
else {
current_money += add;
cout << current_money << endl;
}
}

void spend_money(int & current_money) {
cout << "how much money would you like to spend? " << endl;
int minus;
cin >> minus;
if(current_money - minus < 0) {
cout << "This would result in a negative number!" << endl;
}
else {
current_money -= minus;
cout << current_money << endl;
}
}

void pass_go(int & current_money) {
cout << "you passed go, collect 200 dollars!" << endl;
current_money += 200;
}

bool has_property(vector<property> & props, string name) {
bool has_prop = false;
for(unsigned int i = 0; i < props.size(); i++) {
if(name == props[i].name) {
has_prop = true;
}
}
return has_prop;
}

void buy_property(vector<property> & properties, int & current_money) {
bool success = false;
int property_value = 0;
string input_name;
while(!success) {
cout << "please enter property name: ";
cin >> input_name;
if (not has_property(properties, input_name) ){
cout << "What is the value of your property?" << endl;
cin >> property_value;
while(property_value < 0) {
cout << "incorrect property value. Please try again." << endl;
cin >> property_value;
}
if(current_money - property_value < 0) {
cout << "you dont have enough money to purchase this." << endl;
break;
}
current_money -= property_value;
properties.push_back(property{input_name, property_value});
success = true;
}
else {
cerr << "you already have that property" << endl;
break;
}
}

}

void sell_property(vector<property> & properties, int & current_money) {
string property_to_sell;
cout << "please enter the property name you would like to sell: " << endl;
cin >> property_to_sell;
if(has_property(properties, property_to_sell)) {
for(unsigned int i = 0; i < properties.size(); i++) {
if(properties[i].name == property_to_sell) {
current_money += properties[i].value;
properties.erase(properties.begin()+i);
}
}
}
else {
cout << "you dont own that property to be able to sell it" << endl;
}
}

void quit_game() {
cout << "thanks for playing!" << endl;
exit (0);
}

ostream & operator<<(ostream & os, const property & prop) {
os << "{name: '" << prop.name
<< "', value: " << prop.value
<< "}";
return os;
}

void display_properties(vector<property> & properties) {
cout << "found the following "
<< properties.size() << " properties:" << endl;
for(unsigned int i = 0; i < properties.size(); i++) {
cout << properties[i] << endl;
}
}

void play_game(vector<property> & properties, int & current_money) {

bool keep_playing = true;
while(keep_playing){
int choice;
cout << "Welcome to Monopoly" << endl;
cout << "please enter a number correspoding to what function you would like to run" << endl;
cout << "1) Roll the dice\n2) Report the player's current money\n3) Add money\n4) Spend money" << endl;
cout << "5) Pass GO\n6) Buy Property\n7)Sell Property\n8)Display owned properties\n9) Quit the program" << endl;
cin >> choice;
if(choice == 1)
roll_the_dice();
else if(choice == 2)
report_current_money(current_money);
else if(choice == 3)
add_money(current_money);
else if(choice == 4)
spend_money(current_money);
else if(choice == 5)
pass_go(current_money);
else if(choice == 6)
buy_property(properties, current_money);
else if(choice == 7)
sell_property(properties, current_money);
else if(choice == 8)
display_properties(properties);
else if(choice == 9)
quit_game();
}

}

int main() {
int current_money = 1500;
vector<property> properties;
play_game(properties, current_money);

return 0;
}

+ 40
- 0
cs142/lab05/game/properties/properties_in_order.txt View File

@@ -0,0 +1,40 @@
Mediterranean Avenue -60
Community Chest
Baltic Avenue - 60
Income Tax
Reading Railroad - 200
Oriental Avenue - 100
Chance
Vermont Avenue - 100
Connecticut Avenue - 120
Jail
St. Charles Place - 140
Electric Company - 150
States Avenue - 140
Virginia Avenue - 160
Pennsylvania Railroad - 200
St. James Place - 180
Community Chest
Tennessee Avenue - 180
New York Avenue - 200
Free parking
Kentucky Avenue - 220
Chance
Indiana Avenue - 220
Illinois Avenue - 240
B&O Railroad - 200
Atlantic Avenue - 260
Ventnor Avenue - 260
Water Works - 150
Marvin Gardens - 280
Go to jail
Pacific avenue - 300
North Carolina Avenue - 300
Community chest
Pennsylvania Avenue - 320
Short line - 200
Chance
Park Place - 350
Luxury tax
Boardwalk - 400
Go

+ 182
- 0
cs142/lab05/monopoly.cpp View File

@@ -0,0 +1,182 @@
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;


struct property{
string name;
int value;
};

void roll_the_dice() {
srand(time(NULL));
int roll = rand() % 6 +1;
int roll2 = rand() % 6 +1;
cout << roll << " + "<< roll2 << " equals " << roll +roll2 << endl;
}

void report_current_money(int & current_money) {
cout << "this is your current money" << endl;
cout << current_money << endl;
}

void add_money(int & current_money) {
cout << "how much money would you like to add? " << endl;
int add;
cin >> add;
if(add < 0){
cout << "please enter a positive number" << endl;
add_money(current_money);
}
else {
current_money += add;
cout << current_money << endl;
}
}

void spend_money(int & current_money) {
cout << "how much money would you like to spend? " << endl;
int minus;
cin >> minus;
if(current_money - minus < 0) {
cout << "This would result in a negative number!" << endl;
}
else {
current_money -= minus;
cout << current_money << endl;
}
}

void pass_go(int & current_money) {
cout << "you passed go, collect 200 dollars!" << endl;
current_money += 200;
}

bool has_property(vector<property> & props, string name) {
bool has_prop = false;
for(unsigned int i = 0; i < props.size(); i++) {
if(name == props[i].name) {
has_prop = true;
}
}
return has_prop;
}

void buy_property(vector<property> & properties, int & current_money) {
bool success = false;
int property_value = 0;
string input_name;
while(!success) {
cout << "please enter property name: ";
cin >> input_name;
if (not has_property(properties, input_name) ){
cout << "What is the value of your property?" << endl;
cin >> property_value;
while(property_value < 0) {
cout << "incorrect property value. Please try again." << endl;
cin >> property_value;
}
if(current_money - property_value < 0) {
cout << "you dont have enough money to purchase this." << endl;
break;
}
current_money -= property_value;
properties.push_back(property{input_name, property_value});
success = true;
}
else {
cerr << "you already have that property" << endl;
break;
}
}

}

void sell_property(vector<property> & properties, int & current_money) {
string property_to_sell;
cout << "please enter the property name you would like to sell: " << endl;
cin >> property_to_sell;
if(has_property(properties, property_to_sell)) {
for(unsigned int i = 0; i < properties.size(); i++) {
if(properties[i].name == property_to_sell) {
cout << "enter value for sale" << endl;
int sale;
cin >> sale;
if(sale < 0){
cout << "can't be nagative" << endl;
break;
}
properties[i].value = sale;
current_money += properties[i].value;
properties.erase(properties.begin()+i);
}
}
}
else {
cout << "you dont own that property to be able to sell it" << endl;
}
}

void quit_game() {
cout << "thanks for playing!" << endl;
exit (0);
}

ostream & operator<<(ostream & os, const property & prop) {
os << "{name: '" << prop.name
<< "', value: " << prop.value
<< "}";
return os;
}

void display_properties(vector<property> & properties) {
cout << "found the following "
<< properties.size() << " properties:" << endl;
for(unsigned int i = 0; i < properties.size(); i++) {
cout << properties[i] << endl;
}
}

void play_game(vector<property> & properties, int & current_money) {

bool keep_playing = true;
while(keep_playing){
int choice;
cout << "Welcome to Monopoly" << endl;
cout << "please enter a number correspoding to what function you would like to run" << endl;
cout << "1) Roll the dice\n2) Report the player's current money\n3) Add money\n4) Spend money" << endl;
cout << "5) Pass GO\n6) Buy Property\n7)Sell Property\n8)Display owned properties\n9) Quit the program" << endl;
cin >> choice;
if(choice == 1)
roll_the_dice();
else if(choice == 2)
report_current_money(current_money);
else if(choice == 3)
add_money(current_money);
else if(choice == 4)
spend_money(current_money);
else if(choice == 5)
pass_go(current_money);
else if(choice == 6)
buy_property(properties, current_money);
else if(choice == 7)
sell_property(properties, current_money);
else if(choice == 8)
display_properties(properties);
else if(choice == 9)
quit_game();
}

}

int main() {
int current_money = 1500;
vector<property> properties;
play_game(properties, current_money);

return 0;
}

+ 6
- 0
cs142/lab06/Makefile View File

@@ -0,0 +1,6 @@
CXXFLAGS=-Wall -g -std=c++0x
all: add-display
clean:
rm -vf add-display
test: all
./add-display

+ 211
- 0
cs142/lab06/add-display.cpp View File

@@ -0,0 +1,211 @@