Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Introduction This assignment will be part 1 of 2 of the culmination of your C/

ID: 3687984 • Letter: 1

Question

1 Introduction This assignment will be part 1 of 2 of the culmination of your C/C++ programming experience in this course. You will use C++ classes to model a social network, allowing users to be added, friend connections made, etc. 2 What vou will learn In this assignment you will: 1. Create your own C++ classes and learn the syntax of defining data members and methods 2. Use File I/O and stringstreams to read/write text files containing structured data (records) Understand the abstract representation of graphs (vertices-Users, edges-Friend connections) 3. 3 Background Information and Notes Graph Representation: A graph is an abstract representation of the connections between objects. Each object (known as a vertex) is connected to others via 'edges'. Visually, graphs are quite intuitive: Jane Jimmy Marci Timmy Here we see four users (vertices) and the friend connections (edges) between them. Many interesting problems in computer science can be represented as graphs and algorithms defined to extract information from the connections within the graph. For our purposes we will create a class to represent a single User which contains information about each user (name, etc.) as well as the edge connections (friend relationships) for the user. Note: In our system edges are 'undirected' meaning friend connections are reflexive (i.e. if l'm a friend with you, you are a friend with me). Other problems using graphs may require 'directed' edges (i.e non-reflexive relationships). Thus, when you add a friend connection from user1 to user2, be sure to add the connection from user2 to user1. Defining a File Format: This program requires you to use File I/O techniques to read and write the user database from and to a file. Whenever we write a file containing structured data we need to define a file format (syntax) so we

Explanation / Answer

main.cpp

#include "network.h"
#include "user.h"
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>

using namespace std;

void print_header() {
cout << "ID      Name           Year      Zip" << endl;
cout << "=======================================================" << endl;
}

void print_user(User u) {
cout << u.id << ".     " << u.name << "            " << u.birth_year << "     " << u.zip_code << endl;
}

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

if (argc != 2) {
    cout << "Usage: ./social_network database_filename";
    return 1;
}

string filename = argv[1];

Network network = Network(filename);
network.read_friends(filename);

while (true) {
    cout << "> ";

    int option;
    cin >> option;

    switch( option ) {

      case 1:
      {
        string name, last_name;

        cin >> name;
        name.append(" ");
        cin >> last_name;
        name.append(last_name);

        int born;
        cin >> born;

        int zip;
        cin >> zip;

        User new_user = User(name, born, zip);
        network.add_user(new_user);

      }
        break;
      case 2:
      {
        // Add friend connection
        string fn, ln, fn2, ln2;
        cin >> fn;
        cin >> ln;
        cin >> fn2;
        cin >> ln2;
        int id1 = network.get_id(fn.append(" ").append(ln));
        int id2 = network.get_id(fn2.append(" ").append(ln2));
        network.add_connection(id1, id2);
        break;
      }

      case 3:
      {
        // Remove freind
        string fn, ln, fn2, ln2;
        cin >> fn;
        cin >> ln;
        cin >> fn2;
        cin >> ln2;
        int id1 = network.get_id(fn.append(" ").append(ln));
        int id2 = network.get_id(fn2.append(" ").append(ln2));
        network.remove_connection(id1, id2);
        break;
      }
      case 4:
      {
        // Output user database
        print_header();
        for (int i = 0; i < network.users.size(); i++)
          print_user(network.users[i]);
        break;
      }
      case 5:
      {
        // print users friends
        string fn, ln;
        cin >> fn;
        cin >> ln;
        int id = network.get_id(fn.append(" ").append(ln));

        for (int i = 0; i < network.users.size(); i++)
          for (int j = 0; j < network.users[i].friends.size(); j++)
            if (network.users[i].friends[j] == id)
              print_user(network.users[i]);


        break;

      }
      case 6:
      {
        // Write to file
        string filename;
        cin >> filename;
        network.write_friends(filename);
        break;
      }
      case 7:
        // exit
        return 1;
        break;
    }

}

}

network.cpp
#include "network.h"
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
using namespace std;

int Network::read_friends(string filename) {

ifstream db (filename.c_str());
string line;
users_count = -1;
if (db.is_open()) {
    while (getline(db, line)) {

      if (users_count == -1) {
        users_count = atoi(line.c_str());
        continue;
      }

      string buffer;
      User new_user = User();
      new_user.id = atoi(line.c_str());
      getline(db, buffer);
      buffer.erase(remove(buffer.begin(), buffer.end(), ' '), buffer.end());
      new_user.name = buffer;

      // Birth parse
      getline(db, buffer);
      new_user.birth_year = atoi(buffer.c_str());

      getline(db, buffer);
      new_user.zip_code = atoi(buffer.c_str());

      getline(db, buffer);
      // stringstream ids(buffer);
      string friend_id;
      // cout << "new user has friends ";
      for (stringstream s(buffer); s >> friend_id;) {
        new_user.friends.push_back(atoi(friend_id.c_str()));
        // cout << friend_id;
      }
      // cout << endl;
      users.push_back(new_user);
    }

}

return 0;

}

int Network::write_friends(string filename) {

ofstream db (filename.c_str());
string buffer;

if (db.is_open()) {
    db << users_count << endl;
    for (int i = 0; i < users.size(); i++) {
      db << users[i].id << endl;
      db << " " << users[i].name << endl;
      db << " " << users[i].birth_year << endl;
      db << " " << users[i].zip_code << endl;
      db << " ";
      for (int j = 0; j < users[i].friends.size(); j++) {
        db << users[i].friends[j] << " ";
      }
      db << endl;
    }
}
db.close();
return 0;

}

int Network::add_user(User user) {
user.id = users_count;
users.push_back(user);
users_count++;
return 0;
}

int Network::add_connection(int id1, int id2) {
for (int i = 0; i < users.size(); i++) {

    if (users[i].id == id1)
      users[i].add_friend(id2);

    if (users[i].id == id2)
      users[i].add_friend(id1);

}

return 0;
}

int Network::remove_connection(int id1, int id2) {
for (int i = 0; i < users.size(); i++) {

    if (users[i].id == id1)
      users[i].delete_friend(id2);

    if (users[i].id == id2)
      users[i].delete_friend(id1);

}

return 0;
}

int Network::get_id(string username) {
for (int i = 0; i < users.size(); i++)
    if (users[i].name == username)
      return users[i].id;

return -1;
}


network.h

#ifndef NETWORK_H
#define NETWORK_H

#include "user.h"
#include <string>

class Network {
public:
   string filename;
   vector<User> users;
   int users_count;
   int read_friends(string filename);
   int write_friends(string filename);
   int add_user(User user);
   int add_connection(int id1, int id2);
   int remove_connection(int id1, int id2);
   int get_id(string username);

   Network(string filename) {
     this->filename = filename;
   }

private:


};


#endif


db.txt
5
0
   Mark Redekopp
   1978
   90018
   1 2 3
1
   Marquis Lee
   1990
   90271
   0 2
2
   Tommy Trojan
   1885
   90089
   0 1
3
   Max Nikias
   1945
   91103
   0 4
4
   Jane Doe
   1988
   90007
   3

user.cpp
#include "user.h"
#include <string>
using namespace std;

int User::delete_friend(int friend_id) {
for (int i = 0; i< friends.size(); i++)
    if (friends[i] == friend_id)
      friends.erase(friends.begin() + i);

return 0;
}

int User::add_friend(int friend_id) {

for (int i = 0; i < this->friends.size(); i++)
    if (this->friends[i] == friend_id)
      return 0;

this->friends.push_back(friend_id);

return 0;
}

user.h
#ifndef USER_H
#define USER_H
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

class User {

public:
    int id;
    string name;
    int birth_year;
    int zip_code;
    vector<int> friends;
    int add_friend(int);
    int delete_friend(int);

    User(string name, int born, int zip_code) {
      this->zip_code = zip_code;
      this->name = name;
      this->birth_year = born;
    }

    User() {
    
    }


private:

};


#endif