void Menu::buildConnectionBetweenPeople(const char *filename, vector<Person> people) { vector<Connection> connections = utils.getPeopleConnections(filename); //For each connection... for(int i = 0; i < connections.size(); i++) { Person src = findPersonById(connections[i].getSource(), people); Person dest = findPersonById(connections[i].getDest(), people); /** * Kind of bidirectional graph * * Our graph will be a complete graph(each person is connected to all other people) * Because of that we need to create a edge that connected the current source/destination * and vice-versa. The cost will be obviously the same */ personGraph.addEdge(src, dest, connections[i].getCost()); personGraph.addEdge(dest, src, connections[i].getCost()); } //Calculate the best cost for the current graph vector<Vertex<Person> *> pp = personGraph.calculatePrim(desiredSkills); //Show the best team showBestCost(pp); //Back to the main menu getchar(); performUserAction(); }
// suppose every pointer argument points to some location on the heap bool changePerson(List* list, int id, char* newFirstName, int newFirstNameLength, char* newLastName, int newLastNameLength, char* newCity, int newCityLength, char newPhoneNumber[15]) { Person* person = findPersonById(list, id); if (person == NULL) { return false; } person->firstName = newFirstName; person->firstNameLength = newFirstNameLength; person->lastName = newLastName; person->lastNameLength = newLastNameLength; int newFullNameLength = newFirstNameLength + newLastNameLength; person->fullName = malloc(sizeof(char) * (newFullNameLength + 1)); memcpy(person->fullName, newFirstName, newFirstNameLength); memcpy(person->fullName + newFirstNameLength, newLastName, newLastNameLength + 1); person->fullNameLength = newFullNameLength; person->city = newCity; person->cityLength = newCityLength; memcpy(person->phoneNumber, newPhoneNumber, sizeof newPhoneNumber); return true; }
void Menu::createGraphFile() { vector<Person> people; vector<Connection> connections; vector<Skill> parsedSkills; //First of all we want to clear the previous data in the graph(by removing all the vertices) clearGraph(); bool adding = true; string line; cout << "[*] Ok, first of all tell me the skills you're looking for(separated by a ',', 'n' to go to the next step): " << endl; while(adding) { getline(cin, line); while(line.empty()) { cerr << "[!] You have to provide some data... Try again: " << endl; getline(cin, line); } checkOrClear(line, adding); if(!adding) break; //Only the last line will be parsed parsedSkills = utils.parseSkills(line); } adding = true; cout << "[*] Ok, let's add some people to the graph..." << endl; cout << "[*] Enter the information in the following way: personID,personName,listOfSkills(n/N to go the next step):" << endl; while(adding) { getline(cin, line); while(line.empty()) { cerr << "[!] You have to provide some data... Try again: " << endl; getline(cin, line); } checkOrClear(line, adding); if(!adding) break; Person p = utils.extractPerson(line); if(p.getName() != "invalid") { personGraph.addVertex(p); people.push_back(p); } //Show the list of people that we have till now for(int i = 0; i < personGraph.getNumVertex(); i++) cout << personGraph.getVertexIndex(i)->getInfo() << endl; cout << endl; } //The next information is to create connections between them adding = true; cout << "[*] Ok, you added " << people.size() << " people to the graph!" << endl; cout << "[*] Let's build the connections between them now(firstPersonID,secondPersonID,communicationCost) ('n' to go to the next step): " << endl; while(adding) { getline(cin, line); while(line.empty()) { cerr << "[!] You have to provide some data... Try again: " << endl; getline(cin, line); } checkOrClear(line, adding); if(!adding) break; //Build the connection in the graph Connection conn = utils.readConnection(line); connections.push_back(conn); Person src = findPersonById(conn.getSource(), people); Person dest = findPersonById(conn.getDest(), people); personGraph.addEdge(src, dest, conn.getCost()); } adding = true; cout << "[*] Provide a valid filename so we can save the data: " << endl; getline(cin, line); while(line.empty()) { cerr << "[!] You have to provide some data... Try again: " << endl; getline(cin, line); } //Before saving lets check the skills checkSkillsFile(parsedSkills, people, "[!] I'm sorry. The graph you're trying to save is invalid! Please try again!"); if(utils.saveDataToFile(line.c_str(), people, connections, parsedSkills)) { cout << "[*] The file was successfully saved as " << line << endl << endl; getchar(); performUserAction(); } else { cerr << "[!] The file could not be saved!" << endl << endl; getchar(); performUserAction(); } }