Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
1
Indexable
Never
#include "internship.h"

#include <fstream>
#include <algorithm>
#include <iomanip>

namespace internship {

    using date::year_month_day;
    using date::sys_days;

    void solution(const std::string& jsonFileName, int elementsCount) {
        std::ifstream file(jsonFileName);
        if (!file.is_open()) {
            std::cerr << "Failed to open JSON file " << jsonFileName << std::endl;
            return;
        }

        json jsonData;
        try {
            file >> jsonData;
        } catch (const json::parse_error& e) {
            std::cerr << "Failed to parse JSON file " << jsonFileName << ": " << e.what() << std::endl;
            return;
        }

        std::vector<std::pair<std::string, std::chrono::days>> osList;
        for (const auto& os : jsonData["operating_systems"]) {
            std::string name = os["name"];
            auto start = sys_days(year_month_day::from_string(os["release_date"]));
            auto end = start + std::chrono::hours(os["support_period"].get<int>() * 24);
            osList.emplace_back(name, end - start);
        }

        std::sort(osList.begin(), osList.end(), [](const auto& lhs, const auto& rhs) {
            return lhs.second > rhs.second;
        });

        std::cout << "Top " << elementsCount << " operating systems with longest support period:" << std::endl;
        for (int i = 0; i < elementsCount && i < osList.size(); ++i) {
            const auto& os = osList[i];
            auto end = os.second;