kolioi

01. Pointers and References - Homework C++

Jan 29th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. // TryParse.h
  2.  
  3. #include <sstream>
  4.  
  5. bool tryParse(const std::string& str, int& n)
  6. {
  7.     std::istringstream in(str);
  8.     return !(in >> n).fail();
  9. }
  10.  
  11. // Find.h
  12.  
  13. #include <vector>
  14. #include <algorithm>
  15.  
  16. #include "Company.h"
  17.  
  18. inline Company* find(const std::vector<Company*>& companies, int id)
  19. {
  20.     auto it = std::find_if(companies.begin(), companies.end(), [id](Company* c) { return c->getId() == id; });
  21.     if (it != companies.end())
  22.         return *it;
  23.  
  24.     return nullptr;
  25. }
  26.  
  27. // OrderedInserter.h
  28.  
  29. #include <vector>
  30. #include <algorithm>
  31.  
  32. #include "Company.h"
  33.  
  34. class OrderedInserter
  35. {
  36. public:
  37.     OrderedInserter(std::vector<const Company*>& v) : companies(v) {};
  38.     void insert(const Company* c)
  39.     {
  40.         auto it = std::lower_bound(companies.begin(), companies.end(), c,
  41.             [](const Company* a, const Company* b) { return a->getId() < b->getId(); });
  42.         companies.insert(it, c);
  43.     }
  44.  
  45. private:
  46.     std::vector<const Company*>& companies;
  47. };
  48.  
  49. // ProfitReport.h
  50.  
  51. #include <map>
  52. #include <sstream>
  53.  
  54. #include "Company.h"
  55. #include "ProfitCalculator.h"
  56.  
  57. std::string getProfitReport(const Company* first, const Company* last, const std::map<int, ProfitCalculator>& calc)
  58. {
  59.     std::ostringstream out;
  60.     std::string newline;
  61.     for (const Company* c = first; c <= last; c++)
  62.     {
  63.         auto calculator = const_cast<std::map<int, ProfitCalculator>&>(calc)[c->getId()];
  64.         //auto calculator = calc.at(c->getId());
  65.         //auto calculator = calc.find(c->getId())->second;
  66.         out << newline << c->getName() << " = " << calculator.calculateProfit(*c);
  67.         newline = "\n";
  68.     }
  69.  
  70.     return out.str();
  71. }
  72.  
  73. // CompanyMemoryUtils.h
  74.  
  75. #include "Company.h"
  76.  
  77. using byte = unsigned char;
  78.  
  79. std::vector<Company> readCompaniesFromMemory(const byte* ptr, int n)
  80. {
  81.     std::vector<Company> companies;
  82.     for (size_t i = 0; i < n; i++)
  83.     {
  84.         int id = *ptr++;
  85.  
  86.         std::string name(reinterpret_cast<const char*>(ptr));
  87.         //std::string name((char*)ptr);
  88.         ptr += name.length() + 1;
  89.  
  90.         int empl = *ptr++;
  91.         std::vector<std::pair<char, char>> employees;
  92.         for (size_t i = 0; i < empl; i++)
  93.         {
  94.             char ch1 = *ptr++, ch2 = *ptr++;
  95.             employees.push_back(std::make_pair(ch1,ch2));
  96.         }
  97.  
  98.         Company comp(id, name, employees);
  99.         companies.push_back(comp);
  100.     }
  101.  
  102.     return companies;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment