adding cs142 code
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
CXX=g++
|
||||
CPPFLAGS=-Wall -g -std=c++0x
|
||||
|
||||
all: people
|
||||
|
||||
people: main.o person.o
|
||||
g++ $(CPPFLAGS) person.o main.o -o people
|
||||
|
||||
main.o: main.cpp person.o person.h
|
||||
person.o: person.cpp person.h
|
||||
test: people
|
||||
./people < test.txt
|
||||
|
||||
clean:
|
||||
rm -fv people *.o
|
||||
|
||||
debug: people
|
||||
gdb people
|
||||
@@ -0,0 +1,106 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "person.h"
|
||||
|
||||
void display_list_of_friends(vector<person> & p) {
|
||||
for(unsigned int i = 0; i < p.size(); i++) {
|
||||
cout << p[i].name << endl;
|
||||
cout << "\t" << p[i].fav_food << endl;
|
||||
cout << "\tlist of friends: " << endl;
|
||||
for(unsigned int j = 0; j < p[i].friends.size(); j++) {
|
||||
cout << "\t\t" << p[i].friends[j] << endl;;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int get_person(vector<person> & p, string name) {
|
||||
int position = -1;
|
||||
for(unsigned int i = 0; i < p.size(); i++) {
|
||||
if(p[i].name == name) {
|
||||
position = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<person> people;
|
||||
string selection;
|
||||
while(1) {
|
||||
cout << "1)add person\n2)display all people\n3)add friend\n4)remove friend" << endl;
|
||||
cout << "5)modify food\n6)display friends/food" << endl;
|
||||
cin >> selection;
|
||||
if(selection[0] == '1') { //optimized
|
||||
string name, food;
|
||||
cout << "what is the persons name followed by favorite food on a new line " << endl;
|
||||
cin.ignore();
|
||||
getline(cin, name);
|
||||
getline(cin, food);
|
||||
person p = person(name, food);
|
||||
int p1 = get_person(people, p.name);
|
||||
if(p1 == -1) {
|
||||
people.push_back(p);
|
||||
}
|
||||
}
|
||||
else if(selection[0] == '2') { //optimized
|
||||
display_list_of_friends(people);
|
||||
}
|
||||
else if(selection[0] == '3') { //optimized
|
||||
cout << "which relationship do you want to start (enter two names, seperated by newlines)?" << endl;
|
||||
string first, second;
|
||||
cin.ignore();
|
||||
getline(cin, first);
|
||||
getline(cin, second);
|
||||
int p1 = get_person(people, first);
|
||||
int p2 = get_person(people, second);
|
||||
if(p1 != -1 and p2 != -1 and first!= second) {
|
||||
people[p1].add_friend(second);
|
||||
people[p2].add_friend(first);
|
||||
}
|
||||
}
|
||||
else if(selection[0] == '4') { //optimized
|
||||
cout << "which relationship do you want to end (enter two names, seperated by newlines)?" << endl;
|
||||
string first, second;
|
||||
cin.ignore();
|
||||
getline(cin, first);
|
||||
getline(cin, second);
|
||||
int p1 = get_person(people, first);
|
||||
int p2 = get_person(people, second);
|
||||
if (p1 != -1 and p2 != -1) {
|
||||
people[p1].delete_friend(people[p2].name);
|
||||
people[p2].delete_friend(people[p1].name);
|
||||
}
|
||||
}
|
||||
else if(selection[0] == '5') { //optimized
|
||||
string name;
|
||||
cout << "what is the name of the person to whom you would like to modify fav food?" << endl;
|
||||
cin.ignore();
|
||||
getline(cin, name);
|
||||
int p1 = get_person(people, name);
|
||||
if(p1 != -1) {
|
||||
string new_food;
|
||||
cout << "what food do you want to add?" << endl;
|
||||
getline(cin, new_food);
|
||||
people[p1].modify_food(new_food);
|
||||
}
|
||||
}
|
||||
else if(selection[0] == '6') {
|
||||
cout << "What is the name of the person to whom you would like to view friends with same food?" << endl;
|
||||
string name;
|
||||
cin.ignore();
|
||||
getline(cin, name);
|
||||
int p1 = get_person(people, name);
|
||||
if(p1 != -1) {
|
||||
people[p1].display_person(people);
|
||||
}
|
||||
}
|
||||
else if(selection[0] == 'q') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "person.h"
|
||||
|
||||
void person::modify_food(string new_food) {
|
||||
fav_food = new_food;
|
||||
}
|
||||
|
||||
void person::display_person(vector<person> & p) {
|
||||
for(unsigned int i = 0; i < friends.size(); i++) {
|
||||
for(unsigned int j = 0; j < p.size(); j++) {
|
||||
if(p[j].name == friends[i] and p[j].fav_food == fav_food) {
|
||||
cout << p[j].name << " also likes " << fav_food << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void person::add_friend(string name) {
|
||||
if(not already_friends(name)) {
|
||||
friends.push_back(name);
|
||||
}
|
||||
}
|
||||
|
||||
bool person::already_friends(string name) {
|
||||
for(unsigned int i = 0; i < friends.size(); i++) {
|
||||
if(friends[i] == name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void person::delete_friend(string name) {
|
||||
for(unsigned int i = 0; i < friends.size(); i++) {
|
||||
if(friends[i] == name) {
|
||||
friends.erase((friends.begin())+i);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
cerr << "this person does not exist" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef __PERSON_H__
|
||||
#define __PERSON_H__
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class person {
|
||||
public:
|
||||
string name;
|
||||
string fav_food;
|
||||
vector<string> friends;
|
||||
person(string new_name, string food) {
|
||||
name = new_name;
|
||||
fav_food = food;
|
||||
};
|
||||
void modify_food(string new_food);
|
||||
void display_person(vector<person> & p);
|
||||
void add_friend(string name);
|
||||
bool already_friends(string name);
|
||||
void delete_friend(string name);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ostream & operator<<(ostream & os, vector<string> & v) {
|
||||
os << "[";
|
||||
for(auto &i: v) {
|
||||
os << i << ", ";
|
||||
}
|
||||
os << "]";
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
vector<string> people = {
|
||||
"bilbo",
|
||||
"frodo",
|
||||
"samwise",
|
||||
"gandalf",
|
||||
"aragorn",
|
||||
"aowen",
|
||||
"gollum",
|
||||
"gimli",
|
||||
"legalos",
|
||||
};
|
||||
|
||||
cout << people << endl;
|
||||
vector<string>::iterator it = find(people.begin(), people.end(), "gandalf");
|
||||
if(it != people.end())
|
||||
people.erase(it);
|
||||
cout << people << endl;
|
||||
it = find(people.begin(), people.end(), "frodo");
|
||||
if(it != people.end())
|
||||
people.erase(it);
|
||||
cout << people << endl;
|
||||
it = find(people.begin(), people.end(), "Lord Voldemort");
|
||||
if(it != people.end())
|
||||
people.erase(it);
|
||||
cout << people << endl;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
2
|
||||
1
|
||||
stephen
|
||||
apples
|
||||
1
|
||||
derek
|
||||
panda
|
||||
2
|
||||
3
|
||||
stephen
|
||||
derek
|
||||
2
|
||||
1
|
||||
colleen
|
||||
bananna
|
||||
2
|
||||
3
|
||||
colleen
|
||||
derek
|
||||
2
|
||||
q
|
||||
Reference in New Issue
Block a user