Advertisement
Ihmemies

10 book class

Nov 18th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.01 KB | Source Code | 0 0
  1. #include "book.hh"
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. Book::Book() {
  6.  
  7. }
  8.  
  9. Book::~Book(){
  10.  
  11. }
  12.  
  13. void Book::addNewChapter(const std::string &id,
  14.                          const std::string &fullName, int length) {
  15.  
  16.     shared_ptr<Chapter> chapter {new Chapter};
  17.     chapter -> id_ = id;
  18.     chapter -> fullName_ = fullName;
  19.     chapter -> length_ = length;
  20.     chapter -> running_id_ = running_id;
  21.     running_id++;
  22.  
  23.     // try insert for a change instad of map::operator[]
  24.     idstore.insert(pair<string, shared_ptr<Chapter>>(id, chapter));
  25. }
  26.  
  27. void Book::addRelation(const std::string &subchapter,
  28.                        const std::string &parentChapter) {
  29.     if (parentChapter.length() > 0) {
  30.         shared_ptr<Chapter> s_ch = findChapter(subchapter);
  31.         shared_ptr<Chapter> p_ch = findChapter(parentChapter);
  32.  
  33.         p_ch-> subchapters_.push_back(s_ch);
  34.         s_ch -> parentChapter_ = p_ch;
  35.     }
  36. }
  37.  
  38. void Book::printIds(Params) const {
  39.     cout << "Book has " << idstore.size() << " chapters:" << endl;
  40.  
  41.     vector <string> to_sort;
  42.     for (auto& [id, chapter] : idstore) {
  43.         to_sort.push_back(chapter->fullName_ + " --- " + id);
  44.     }
  45.     // sort vector to ascii order
  46.     std::sort (to_sort.begin(), to_sort.end(), compare);
  47.  
  48.     for (auto& chapter : to_sort) {
  49.         cout << chapter << endl;
  50.     }
  51. }
  52.  
  53. void Book::printContents(Params) const {
  54.  
  55.     // get a list of top chapters
  56.     // map sorts running id's automatically to asc order
  57.     std::map<int, std::string> top_chapters;
  58.     for (auto& [id, chapter] : idstore) {
  59.         if (chapter->parentChapter_ == nullptr) {
  60.             top_chapters.insert(
  61.                 pair<int, std::string> (chapter->running_id_,
  62.                                         chapter->id_));
  63.         }
  64.     }
  65.  
  66.     // iterate through top chapters, printing them and their subchapters
  67.     int i = 1;
  68.     for (auto& val : top_chapters) {
  69.         // avoid typing idstore.at(val.second) repeatedly
  70.         auto& chapter = idstore.at(val.second);
  71.  
  72.         // is chapter open or closed?
  73.         string status = chapter->isopen_ ? "- " : "+ ";
  74.  
  75.         // print chapter details
  76.         cout << status << i << ". " << chapter->fullName_
  77.              << " ( " << chapter->length_ << " )" << endl;
  78.         i++;
  79.  
  80.         // recursively call func again if chapter has subchapters
  81.         if(chapter->subchapters_.size() > 0 and chapter->isopen_) {
  82.             print_subchapters(chapter->id_, 2);
  83.         }
  84.     }
  85. }
  86.  
  87. // Recursively closes all chapters and subchapters
  88. // starting from the given chapter.
  89. void Book::close(Params params) const
  90. {
  91.     string to_close = params.front();
  92.  
  93.     // check if chapter exists
  94.     shared_ptr<Chapter> chapter = findChapter(to_close);
  95.     if(chapter == nullptr) { return; }
  96.  
  97.     // close chapter if open
  98.     if (chapter->isopen_)
  99.     {
  100.         chapter->isopen_ = false;
  101.  
  102.         // if subchapters exist, check if they need closing too
  103.         if (chapter->subchapters_.size() > 0) {            
  104.             for (auto& val : chapter->subchapters_) {
  105.                 close(vector<string> {val->id_});
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. void Book::open(Params params) const {
  112.     string to_open = params.front();
  113.  
  114.     // check if chapter exists
  115.     shared_ptr<Chapter> chapter = findChapter(to_open);
  116.     if(chapter == nullptr) { return; }
  117.  
  118.     chapter->isopen_ = true;
  119.  
  120.     // open subchapters if they don't have subchapters
  121.     if (chapter->subchapters_.size() > 0) {
  122.         for (auto& val : chapter->subchapters_) {
  123.             if (val->subchapters_.size() == 0 )
  124.                 val->isopen_ = true;
  125.         }
  126.     }
  127. }
  128.  
  129. void Book::openAll(Params) const {
  130.     for (auto& [id, chapter] : idstore) {
  131.         chapter->isopen_ = true;
  132.     }
  133. }
  134.  
  135. void Book::printParentsN(Params params) const {
  136.     string id = params.at(0);
  137.     int level = stoi(params.at(1));
  138.  
  139.     // level must be at least 1.
  140.     if (level < 1) {
  141.         cout << "Error. Level can't be less than 1." << endl;
  142.         return;
  143.     }
  144.  
  145.     shared_ptr<Chapter> chapter = findChapter(id);
  146.  
  147.     // no chapter?
  148.     if (chapter == nullptr) { return; }
  149.  
  150.     // no parent chapter?
  151.     if (chapter->parentChapter_ == nullptr) {
  152.         cout << id << " has no parent chapters." << endl;
  153.         return;
  154.     }
  155.  
  156.     // else print subchapters to depth n
  157.     auto list = make_shared<vector<string>>();
  158.     get_parents(list, chapter, 0, level);
  159.  
  160.     // sort to ASCII order
  161.     std::sort (list->begin(), list->end(), compare);
  162.  
  163.     cout << id << " has " << list->size() << " parent chapters:" << endl;
  164.     for (auto& val : *list) {
  165.         cout << val << endl;
  166.     }
  167. }
  168.  
  169. void Book::printSubchaptersN(Params params) const {
  170.     string id = params.at(0);
  171.     int max_depth = stoi(params.at(1));
  172.  
  173.     // level must be at least 1.
  174.     if (max_depth < 1) {
  175.         cout << "Error. Level can't be less than 1." << endl;
  176.         return;
  177.     }
  178.  
  179.     shared_ptr<Chapter> chapter = findChapter(id);
  180.  
  181.     // no chapter?
  182.     if (chapter == nullptr) { return; }
  183.  
  184.     // no subchapters?
  185.     if (chapter->subchapters_.size() == 0) {
  186.         cout << id << " has no subchapters." << endl;
  187.         return;
  188.     }
  189.  
  190.     // else print subchapters to depth n
  191.     auto list = make_shared<vector<string>>();
  192.     get_subchapters(list, id, 0, max_depth);
  193.  
  194.     // sort to ASCII order
  195.     std::sort (list->begin(), list->end(), compare);
  196.  
  197.     cout << id << " has " << list->size() << " subchapters:" << endl;
  198.     for (auto& val : *list) {
  199.         cout << val << endl;
  200.     }
  201. }
  202.  
  203. void Book::printSiblingChapters(Params params) const {
  204.     string id = params.at(0);
  205.     shared_ptr<Chapter> chapter = findChapter(id);
  206.  
  207.     // no chapter?
  208.     if (chapter == nullptr) { return; }
  209.  
  210.     // top tier chapters have no siblings
  211.     if (chapter->parentChapter_ == nullptr) {
  212.         cout << id << " has no sibling chapters." << endl;
  213.         return;
  214.     }
  215.  
  216.     // info msg about findings
  217.     cout << id << " has " << chapter->parentChapter_->subchapters_.size() - 1
  218.          << " sibling chapters:" << endl;
  219.  
  220.     // get parent chapter and print its subchapters except id
  221.     for (auto& val : chapter->parentChapter_->subchapters_) {
  222.         if (val->id_ != id) {
  223.             cout << val-> id_ << endl;
  224.         }
  225.     }
  226. }
  227.  
  228. void Book::printTotalLength(Params params) const{
  229.     string id = params.at(0);
  230.     shared_ptr<Chapter> chapter = findChapter(id);
  231.     // no chapter?
  232.     if (chapter == nullptr) { return; }
  233.  
  234.     int length = 0;
  235.  
  236.     // add provided chapter to length
  237.     length += idstore.at(id)->length_;
  238.  
  239.     // get subchapters to depth n
  240.     // TODO get_subchapters to work with inf depth
  241.     auto list = make_shared<vector<string>>();
  242.     get_subchapters(list, id, 0, 9999);
  243.  
  244.     // add subchapters to length
  245.     for (auto& val : *list) {
  246.         length += idstore.at(val)->length_;
  247.     }
  248.  
  249.     cout << "Total length of " << id << " is " << length << "." << endl;
  250. }
  251.  
  252. void Book::printLongestInHierarchy(Params params) const {
  253.     string id = params.at(0);
  254.     string longest_id = id;
  255.  
  256.     // intialize provided chapter as longest
  257.     shared_ptr<Chapter> chapter = findChapter(id);
  258.  
  259.     // no chapter?
  260.     if (chapter == nullptr) { return; }
  261.  
  262.     int longest_len = chapter->length_;
  263.  
  264.     // get subchapters to depth n
  265.     // TODO get_subchapters to work with inf depth
  266.     auto list = make_shared<vector<string>>();
  267.     get_subchapters(list, id, 0, 9999);
  268.  
  269.     for (auto& val : *list) {
  270.         if (idstore.at(val)->length_ > longest_len) {
  271.             longest_len = idstore.at(val)->length_;
  272.             longest_id = val;
  273.         }
  274.     }
  275.  
  276.     // print result
  277.     cout << "With the length of " << longest_len << ", "
  278.          << longest_id << " is the longest chapter in ";
  279.  
  280.     if (longest_id == id) {
  281.         cout << "their ";
  282.     } else {
  283.         cout << id << "'s ";
  284.     }
  285.     cout << "hierarchy." << endl;
  286. }
  287.  
  288. void Book::printShortestInHierarchy(Params params) const {
  289.     string id = params.at(0);
  290.     string shortest_id = id;
  291.  
  292.     // intialize provided chapter as longest
  293.     shared_ptr<Chapter> chapter = findChapter(id);
  294.  
  295.     // no chapter?
  296.     if (chapter == nullptr) { return; }
  297.  
  298.     int shortest_len = chapter->length_;
  299.  
  300.     // get subchapters to depth n
  301.     // TODO get_subchapters to work with inf depth
  302.     auto list = make_shared<vector<string>>();
  303.     get_subchapters(list, id, 0, 9999);
  304.  
  305.     for (auto& val : *list) {
  306.         if (idstore.at(val)->length_ < shortest_len) {
  307.             shortest_len = idstore.at(val)->length_;
  308.             shortest_id = val;
  309.         }
  310.     }
  311.  
  312.     // print result
  313.     cout << "With the length of " << shortest_len << ", "
  314.          << shortest_id << " is the shortest chapter in ";
  315.  
  316.     if (shortest_id == id) {
  317.         cout << "their ";
  318.     } else {
  319.         cout << id << "'s ";
  320.     }
  321.     cout << "hierarchy." << endl;
  322. }
  323.  
  324. std::shared_ptr<Chapter> Book::findChapter(const std::string &id) const {
  325.     auto iter = idstore.find(id);
  326.     if (iter != idstore.end()) {
  327.       return iter->second;
  328.     }
  329.     cout << "Error: Not found: " << id << endl;
  330.     return nullptr;
  331. }
  332.  
  333. void Book::print_subchapters(std::string id, int padding) const
  334. {
  335.     int i = 1;
  336.     for (auto& val : idstore.at(id)->subchapters_) {
  337.         // is chapter open or closed?
  338.         string status = val->isopen_ ? "- " : "+ ";
  339.         cout << status;
  340.  
  341.         // string() prints padding n times without a loop
  342.         cout << string(padding, ' ');
  343.  
  344.         // print chapter details
  345.         cout << i << ". " << val->fullName_
  346.              << " ( " << val->length_ << " )" << endl;
  347.         i++;
  348.  
  349.         // recursively call func again if chapter has subchapters
  350.         if (val->subchapters_.size() > 0 and val->isopen_) {
  351.             print_subchapters(val->id_, padding+2);
  352.         }
  353.     }
  354.     return;
  355. }
  356.  
  357. void Book::get_subchapters(shared_ptr<vector<string>> list,
  358.                            string id,
  359.                            int cur_depth,
  360.                            int max_depth) const
  361. {
  362.     cur_depth++;
  363.  
  364.     // look for subchapters at current depth
  365.     shared_ptr<Chapter> chapter = findChapter(id);
  366.     for (auto& val : chapter->subchapters_) {
  367.         list->push_back(val->id_);
  368.         // go deeper if needed
  369.         if (cur_depth < max_depth) {
  370.             get_subchapters(list, val->id_, cur_depth, max_depth);
  371.         }
  372.     }
  373. }
  374.  
  375. void Book::get_parents(shared_ptr<vector<string>> list,
  376.                        std::shared_ptr<Chapter> chapter,
  377.                        int cur_level,
  378.                        int max_level) const
  379. {
  380.     // add current level's parent if one exists
  381.     if (chapter->parentChapter_ != nullptr) {
  382.         list->push_back(chapter->parentChapter_->id_);
  383.         cur_level++;
  384.         // go higher if needed
  385.         if (cur_level < max_level) {
  386.             get_parents(list, chapter->parentChapter_, cur_level, max_level);
  387.         }
  388.     }
  389. }
  390.  
  391. bool Book::compare(std::string& a, std::string& b) {
  392.     if (a < b) {
  393.         return true;
  394.     }
  395.     return false;
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement