Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <array>
- #include <functional>
- #include <map>
- namespace needs{
- using std::cin;
- using std::cout;
- using std::endl;
- using std::string;
- using std::function;
- using std::array;
- using std::vector;
- using std::map;
- }
- std::vector<std::string> split(const std::string& s,
- const char delimiter,
- bool ignore_empty = false)
- {
- std::vector<std::string> result;
- std::string tmp = s;
- while(tmp.find(delimiter) != std::string::npos)
- {
- std::string new_part = tmp.substr(0, tmp.find(delimiter));
- tmp = tmp.substr(tmp.find(delimiter) + 1, tmp.size());
- if(not (ignore_empty and new_part.empty()))
- {
- result.push_back(new_part);
- }
- }
- if(not (ignore_empty and tmp.empty()))
- {
- result.push_back(tmp);
- }
- return result;
- }
- using namespace needs;
- const string BAD_PARAMS = "Erroneus parameters!";
- const string BAD_COMMAND = "Erroneus command!";
- const string HELP_TEXT =
- "S = store id1 i2\nP = print id\nC = count id\nD = depth id\n";
- map<string, vector<string>> data;
- int counter = 0;
- int dots = 0;
- int deep = 1;
- // converts string to lowercase
- string to_lower(string str) {
- transform(str.begin(), str.end(), str.begin(), ::tolower);
- return str;
- }
- bool is_bad_input(vector<string> &commands, unsigned long int params) {
- if (commands.size() != params) {
- cout << BAD_PARAMS << endl << HELP_TEXT;
- return true;
- } else {
- return false;
- }
- }
- // saves a relative name2 to a person name1
- void save(string name1, string name2) {
- data[name1].push_back(name2);
- }
- // prints out the relation tree of a person
- void print(string name) {
- for (auto &val: data[name]) {
- dots += 2;
- if (val.size() > 0) {
- cout << string(dots, '.') << val << endl;
- print(val);
- dots -= 2;
- }
- }
- }
- // counts all relatives of a person
- void count(string name) {
- for (auto &val: data[name]) {
- if (val.size() > 0) {
- counter++;
- count(val);
- }
- }
- }
- // counts how deep the relation tree is
- void depth(string name) {
- for (auto &val: data[name]) {
- if (val.size() > 0) {
- deep += 1;
- depth(val);
- return;
- }
- }
- }
- int main()
- {
- // debug data
- /*
- data["Hugo"].push_back("Laura");
- data["Hugo"].push_back("Jasper");
- data["Laura"].push_back("Helena");
- data["Jasper"].push_back("Maria");
- data["Laura"].push_back("Elias");
- data["Helena"].push_back("Sofia");
- */
- while(true)
- {
- // ask input from user
- string user_input;
- cout << "> ";
- getline(std::cin, user_input);
- vector<string> commands = split(user_input, ' ', true);
- if(commands.size() == 0) { continue; }
- if (to_lower(commands.at(0)) == "s") {
- if (is_bad_input(commands, 3)) {
- continue;
- }
- string name1 = commands.at(1);
- string name2 = commands.at(2);
- save(name1, name2);
- }
- else if (to_lower(commands.at(0)) == "p") {
- if (is_bad_input(commands, 2)) {
- continue;
- }
- cout << commands.at(1) << endl;
- print(commands.at(1));
- dots = 0;
- }
- else if (to_lower(commands.at(0)) == "c") {
- if (is_bad_input(commands, 2)) {
- continue;
- }
- count(commands.at(1));
- cout << counter << endl;
- counter = 0;
- }
- else if (to_lower(commands.at(0)) == "d") {
- if (is_bad_input(commands, 2)) {
- continue;
- }
- depth(commands.at(1));
- cout << deep << endl;
- deep = 1;
- }
- else if (to_lower(commands.at(0)) == "q") {
- return EXIT_SUCCESS;
- }
- else {
- cout << BAD_COMMAND << endl << HELP_TEXT;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement