Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TryParse.h
- #include <sstream>
- bool tryParse(const std::string& str, int& n)
- {
- std::istringstream in(str);
- return !(in >> n).fail();
- }
- // Find.h
- #include <vector>
- #include <algorithm>
- #include "Company.h"
- inline Company* find(const std::vector<Company*>& companies, int id)
- {
- auto it = std::find_if(companies.begin(), companies.end(), [id](Company* c) { return c->getId() == id; });
- if (it != companies.end())
- return *it;
- return nullptr;
- }
- // OrderedInserter.h
- #include <vector>
- #include <algorithm>
- #include "Company.h"
- class OrderedInserter
- {
- public:
- OrderedInserter(std::vector<const Company*>& v) : companies(v) {};
- void insert(const Company* c)
- {
- auto it = std::lower_bound(companies.begin(), companies.end(), c,
- [](const Company* a, const Company* b) { return a->getId() < b->getId(); });
- companies.insert(it, c);
- }
- private:
- std::vector<const Company*>& companies;
- };
- // ProfitReport.h
- #include <map>
- #include <sstream>
- #include "Company.h"
- #include "ProfitCalculator.h"
- std::string getProfitReport(const Company* first, const Company* last, const std::map<int, ProfitCalculator>& calc)
- {
- std::ostringstream out;
- std::string newline;
- for (const Company* c = first; c <= last; c++)
- {
- auto calculator = const_cast<std::map<int, ProfitCalculator>&>(calc)[c->getId()];
- //auto calculator = calc.at(c->getId());
- //auto calculator = calc.find(c->getId())->second;
- out << newline << c->getName() << " = " << calculator.calculateProfit(*c);
- newline = "\n";
- }
- return out.str();
- }
- // CompanyMemoryUtils.h
- #include "Company.h"
- using byte = unsigned char;
- std::vector<Company> readCompaniesFromMemory(const byte* ptr, int n)
- {
- std::vector<Company> companies;
- for (size_t i = 0; i < n; i++)
- {
- int id = *ptr++;
- std::string name(reinterpret_cast<const char*>(ptr));
- //std::string name((char*)ptr);
- ptr += name.length() + 1;
- int empl = *ptr++;
- std::vector<std::pair<char, char>> employees;
- for (size_t i = 0; i < empl; i++)
- {
- char ch1 = *ptr++, ch2 = *ptr++;
- employees.push_back(std::make_pair(ch1,ch2));
- }
- Company comp(id, name, employees);
- companies.push_back(comp);
- }
- return companies;
- }
Advertisement
Add Comment
Please, Sign In to add comment