adding cs142 code
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
CXX=g++
|
||||
CPPFLAGS=-Wall -g -std=c++0x
|
||||
SOURCES=main.cc menu.cc person.cc people.cc
|
||||
OBJECTS=$(SOURCES:.cc=.o)
|
||||
EXE=app
|
||||
|
||||
all: $(EXE)
|
||||
|
||||
main.o: main.cc person.o people.o menu.o
|
||||
menu.o: menu.h menu.cc
|
||||
person.o: person.cc person.h
|
||||
people.o: people.cc people.h
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(LDFLAGS) $(OBJECTS) -o $@
|
||||
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -rvf *.dSYM
|
||||
@rm -vf app
|
||||
|
||||
|
||||
run: $(EXE)
|
||||
./$(EXE)
|
||||
|
||||
test: $(EXE)
|
||||
./$(EXE) < test.txt
|
||||
|
||||
debug: $(EXE)
|
||||
gdb $(EXE)
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
||||
|
||||
autov: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE) < test.txt
|
||||
@@ -0,0 +1,102 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "menu.h"
|
||||
#include "person.h"
|
||||
#include "people.h"
|
||||
|
||||
int main() {
|
||||
people peeps;
|
||||
peeps.add_person("Kevin Bacon", "bacon");
|
||||
|
||||
menu m;
|
||||
string option;
|
||||
bool done = false;
|
||||
while(not done) {
|
||||
cout << endl << m;
|
||||
getline(cin, option);
|
||||
if(option[0] == '#')
|
||||
continue;
|
||||
if (option[0] == 'a') {
|
||||
cout << "adding a person" << endl;
|
||||
string name, food;
|
||||
cout << "name: ";
|
||||
getline(cin, name);
|
||||
cout << "food: ";
|
||||
getline(cin, food);
|
||||
peeps.add_person(name, food);
|
||||
}
|
||||
else if (option[0] == 'd') {
|
||||
cout << "displaying people" << endl;
|
||||
cout << peeps << endl;
|
||||
}
|
||||
else if (option[0] == 'm') {
|
||||
cout << "make connection" << endl;
|
||||
cout << peeps << endl;
|
||||
try {
|
||||
unsigned int a, b;
|
||||
get_two_indices(peeps, a, b);
|
||||
peeps.connect(a, b);
|
||||
}
|
||||
catch(string e) {
|
||||
cerr << e << endl;
|
||||
}
|
||||
}
|
||||
else if (option[0] == 'b') {
|
||||
cout << "break connection" << endl;
|
||||
cout << peeps << endl;
|
||||
try {
|
||||
unsigned int a, b;
|
||||
get_two_indices(peeps, a, b);
|
||||
peeps.unfriend(a, b);
|
||||
}
|
||||
catch(string e) {
|
||||
cerr << e << endl;
|
||||
}
|
||||
}
|
||||
else if (option[0] == 'c') {
|
||||
cout << "change food" << endl;
|
||||
cout << peeps << endl;
|
||||
try {
|
||||
unsigned int a;
|
||||
get_index(peeps, a, "enter index of person who needs changing: ");
|
||||
cout << "new food: ";
|
||||
cin.ignore();
|
||||
getline(cin, option);
|
||||
peeps.food_update(a, option);
|
||||
}
|
||||
catch(string e) {
|
||||
cerr << e << endl;
|
||||
}
|
||||
}
|
||||
else if (option[0] == 's') {
|
||||
cout << "show food friends" << endl;
|
||||
try {
|
||||
unsigned int a;
|
||||
get_index(peeps, a, "enter index of person for food friend: ");
|
||||
peeps[a]->food_friend();
|
||||
}
|
||||
catch(string e) {
|
||||
cerr << e << endl;
|
||||
}
|
||||
}
|
||||
else if (option[0] == 'e') {
|
||||
try {
|
||||
unsigned int a;
|
||||
get_index(peeps, a, "enter index of person for who I shall calculate the Bacon Number: ");
|
||||
cout << peeps[a]->bacon_number() << endl;
|
||||
}
|
||||
catch(string e) {
|
||||
cerr << e << endl;
|
||||
}
|
||||
}
|
||||
else if (option[0] == 'q') {
|
||||
cout << "quitting" << endl;
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include "menu.h"
|
||||
|
||||
void get_index(people & p, unsigned int & a, string msg="") {
|
||||
cout << msg;
|
||||
cin >> a;
|
||||
if(a >= p.size()) {
|
||||
throw string("Not in range!!");
|
||||
}
|
||||
}
|
||||
|
||||
void get_two_indices(people & p, unsigned int & a, unsigned int & b) {
|
||||
if(p.size() < 2) {
|
||||
throw string("Not enough people to perform this operation.");
|
||||
}
|
||||
get_index(p, a, "enter first index: ");
|
||||
get_index(p, b, "enter second index: ");
|
||||
if(a == b) {
|
||||
throw string("indices need to be different");
|
||||
}
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, const menu & m) {
|
||||
for(auto i: menu_items) {
|
||||
os << " " << i << endl;
|
||||
}
|
||||
os << "your selection: ";
|
||||
return os;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef __MENU_H__
|
||||
#define __MENU_H__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <ostream>
|
||||
using namespace std;
|
||||
|
||||
#include "people.h"
|
||||
|
||||
const vector<string> menu_items = {
|
||||
"(a)dd person",
|
||||
"(d)isplay",
|
||||
"(m)ake connection",
|
||||
"(b)reak connection",
|
||||
"(c)hange food",
|
||||
"(s)how food friends",
|
||||
"(e)xtra credit (bacon number)",
|
||||
"",
|
||||
"(q)uit",
|
||||
};
|
||||
|
||||
class menu {};
|
||||
|
||||
void get_index(people &, unsigned int &, string);
|
||||
void get_two_indices(people &, unsigned int &, unsigned int &);
|
||||
|
||||
ostream & operator<<(ostream & os, const menu & m);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "people.h"
|
||||
|
||||
people::~people() {
|
||||
for(auto i: persons) {
|
||||
delete i;
|
||||
}
|
||||
}
|
||||
|
||||
bool people::contains(string name) {
|
||||
for(auto i: persons) {
|
||||
if(i->name == name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void people::add_person(string name, string food) {
|
||||
if(not contains(name)) {
|
||||
person * tmp = new person(name, food);
|
||||
persons.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
void people::connect(unsigned int a, unsigned int b) {
|
||||
cout << *this << endl;
|
||||
person * p1 = persons[a];
|
||||
person * p2 = persons[b];
|
||||
p1->friends.insert(p2);
|
||||
p2->friends.insert(p1);
|
||||
}
|
||||
|
||||
void people::unfriend(unsigned int a, unsigned int b) {
|
||||
cout << *this << endl;
|
||||
person * p1 = persons[a];
|
||||
person * p2 = persons[b];
|
||||
|
||||
auto p2it = p1->friends.find(p2);
|
||||
if(p2it != p1->friends.end())
|
||||
p1->friends.erase(p2it);
|
||||
|
||||
auto p1it = p2->friends.find(p1);
|
||||
if(p1it != p2->friends.end())
|
||||
p2->friends.erase(p1it);
|
||||
}
|
||||
|
||||
void people::food_update(unsigned int a, string new_food) {
|
||||
persons[a]->update_food(new_food);
|
||||
}
|
||||
|
||||
unsigned int people::size() {
|
||||
return persons.size();
|
||||
}
|
||||
|
||||
person * people::operator[](unsigned int i) {
|
||||
return persons[i];
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, people & p) {
|
||||
os << "people (" << p.persons.size() << "):" << endl;
|
||||
for(unsigned int i=0; i < p.persons.size(); i++) {
|
||||
os << i << " : " << *p.persons[i] << endl;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef __PEOPLE_H__
|
||||
#define __PEOPLE_H__
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include "person.h"
|
||||
|
||||
class people {
|
||||
vector<person *> persons;
|
||||
public:
|
||||
|
||||
~people();
|
||||
|
||||
unsigned int size();
|
||||
bool contains(string);
|
||||
void add_person(string, string);
|
||||
void food_update(unsigned int, string);
|
||||
|
||||
void connect(unsigned int, unsigned int);
|
||||
void unfriend(unsigned int, unsigned int);
|
||||
person * operator[](unsigned int);
|
||||
|
||||
friend ostream & operator<<(ostream &, people &);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
using namespace std;
|
||||
#include "person.h"
|
||||
|
||||
void person::update_food(string new_food) {
|
||||
if(name != "Kevin Bacon")
|
||||
favorite_food = new_food;
|
||||
}
|
||||
|
||||
void person::food_friend() {
|
||||
cout << "freinds for " << this->name << " with similar tastes: [";
|
||||
for(auto i = friends.begin(); i != friends.end(); i++) {
|
||||
if((*i)->favorite_food == this->favorite_food) {
|
||||
cout << "'" << (*i)->name << "', ";
|
||||
}
|
||||
}
|
||||
cout << "]" << endl;
|
||||
}
|
||||
|
||||
int person::bacon_number() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ostream &operator<<(ostream &stream, person & p) {
|
||||
stream << "{name: '" << p.name
|
||||
<< "', favorite_food: '" << p.favorite_food
|
||||
<< "'";
|
||||
|
||||
if(p.friends.size() > 0) {
|
||||
stream << ", friends: [";
|
||||
for(auto i = p.friends.begin(); i != p.friends.end(); i++) {
|
||||
if (i != p.friends.begin()) {
|
||||
stream << ", ";
|
||||
}
|
||||
stream << "'" << (*i)->name << "'";
|
||||
}
|
||||
stream << "]";
|
||||
}
|
||||
|
||||
stream << "}";
|
||||
return stream;
|
||||
}
|
||||
|
||||
istream &operator>>(istream &stream, person &o) {
|
||||
string buffer;
|
||||
cout << "name: ";
|
||||
stream.ignore();
|
||||
getline(stream, buffer);
|
||||
o.name = buffer;
|
||||
cout << "food: ";
|
||||
getline(stream, buffer);
|
||||
o.favorite_food = buffer;
|
||||
return stream;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef __PERSON_H__
|
||||
#define __PERSON_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <set>
|
||||
using namespace std;
|
||||
|
||||
class person {
|
||||
private:
|
||||
friend class people;
|
||||
string name;
|
||||
string favorite_food;
|
||||
set<person *> friends;
|
||||
public:
|
||||
person(string name, string food): name(name), favorite_food(food) {};
|
||||
void update_food(string);
|
||||
void food_friend();
|
||||
int bacon_number();
|
||||
friend ostream & operator<<(ostream &, person &);
|
||||
friend istream & operator>>(istream &, person &);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,138 @@
|
||||
# stephen is 1
|
||||
a
|
||||
stephen mcquay
|
||||
panda steaks
|
||||
|
||||
# vanessa is 2
|
||||
a
|
||||
vanessa mcquay
|
||||
pringles
|
||||
|
||||
# stephen <-> vanessa
|
||||
m
|
||||
1
|
||||
2
|
||||
|
||||
# no-op
|
||||
m
|
||||
1
|
||||
2
|
||||
|
||||
# derek is 3
|
||||
a
|
||||
derek mcquay
|
||||
panda steaks
|
||||
|
||||
# colleen is 4
|
||||
a
|
||||
colleen
|
||||
panda steaks
|
||||
|
||||
# out of range
|
||||
m
|
||||
12
|
||||
|
||||
# derek <-> colleen
|
||||
m
|
||||
3
|
||||
4
|
||||
|
||||
# stephen <-> derek
|
||||
m
|
||||
1
|
||||
3
|
||||
|
||||
# stephen <-> colleen
|
||||
m
|
||||
1
|
||||
4
|
||||
|
||||
d
|
||||
|
||||
# no-op
|
||||
b
|
||||
0
|
||||
1
|
||||
|
||||
# stephen <-> Kevin Bacon
|
||||
m
|
||||
0
|
||||
1
|
||||
|
||||
d
|
||||
|
||||
# not allowed:
|
||||
c
|
||||
0
|
||||
Bacon Bits
|
||||
|
||||
# Stephen's food is now Bacon Bits
|
||||
c
|
||||
1
|
||||
Bacon Bits
|
||||
|
||||
d
|
||||
|
||||
# michael is 5
|
||||
a
|
||||
Michael
|
||||
sushi
|
||||
|
||||
# bilbo is 6
|
||||
a
|
||||
Bilbo Baggins
|
||||
Bacon Bits
|
||||
|
||||
# frodo is 7
|
||||
a
|
||||
Frodo Baggins
|
||||
Bacon Bits
|
||||
|
||||
d
|
||||
|
||||
|
||||
# stephen <-> michael
|
||||
m
|
||||
1
|
||||
5
|
||||
|
||||
# stephen <-> bilbo
|
||||
m
|
||||
1
|
||||
6
|
||||
|
||||
# stephen <-> frodo
|
||||
m
|
||||
1
|
||||
7
|
||||
|
||||
d
|
||||
|
||||
# show stephen has a few friends who love bacon bits
|
||||
s
|
||||
1
|
||||
|
||||
c
|
||||
1
|
||||
bacon
|
||||
|
||||
# show Kevin Bacon's friends
|
||||
s
|
||||
0
|
||||
|
||||
# remove friendship: stephen <-> bilbo
|
||||
b
|
||||
0
|
||||
6
|
||||
|
||||
# show kevin bacon's friends
|
||||
s
|
||||
0
|
||||
|
||||
# bacon number for stephen:
|
||||
e
|
||||
1
|
||||
|
||||
d
|
||||
|
||||
q
|
||||
Reference in New Issue
Block a user