Minimalna róźnica

0

Witam, mam problem z zadaniem.
Przygotować metodę, która wyliczy różnicę czasu pomiędzy czasami w formacie HH:MM lub H:MM w minutach. Z pośród wielu godzin należy znaleźć najmniejszą różnicę między dwoma godzinami.
Mam problem z wyznaczaniem roznicy miedzy np 23:50 a 00:10

//
//
#include <sstream>
#include <regex>
#include <cmath>

#include "MinimalTimeDifference.h"

    unsigned int MinimalTimeDifference(std::vector<std::string> times)
    {

        std::vector<int> times_minutes;

        for(std::string v : times)
        {
            times_minutes.push_back(ToMinutes(v));
        }

        unsigned int smaller=1440;
        int m =0;

        for(int t1 : times_minutes)
        {
            for(int t2: times_minutes)
            {
                {
             
                        m=abs(t1-t2);

                    if(m< smaller) smaller = m;


                }
            }
        }
        return smaller;

    }

    unsigned int ToMinutes(std::string time_HH_MM)
    {

        std::regex searching{R"((\d{1,2}):(\d{2}))"};
        std::smatch matches;
        std::string str_minutes, str_hours;

        if (regex_match(time_HH_MM, matches, searching))
        {
            str_minutes = matches[2];
            str_hours = matches[1];
        }


        int minutes=0;
        std::istringstream ism(str_minutes);
        ism >> minutes;

        int hours=0;
        std::istringstream ish(str_hours);
        ish >> hours;

        minutes += (hours*60);


        return minutes;

}
1

Jeśli porównujesz czas do wszystkich innych w kontenerze, nie masz sposobu by określić czy jest wcześniejszy czy późniejszy. Inną sprawą jest porównanie do "samego siebie" (czyli tego samego czasu) które robisz w pętli. Zawsze wyjdzie 0 bo t1 i t2 to ten sam czas w jednym z przypadków a 0 będzie ... najmniejsze :-)

0

Ktoś coś? Bo nadal nie wiem jak to rozwiązać...
Z góry dziękuję za pomoc :)

1

No masz tu kawałek. Nie ustosunkowałeś się do wcześniejszej mojej uwagi odnośnie różnicy czasu. Stąd przyjąłem to co uważałem za stosowne :-/ Część uwag (np. RegExp czy konwersja ze string'a) możesz bezpośrednio przenieść a w funkcji calcDiffMinutes(), możesz zaimplementować poprawkę obliczającą czas jak tego chcesz.

#include <sstream>
#include <regex>
#include <cmath>
#include <limits>
#include <iostream>
#include <numeric>

unsigned int ToMinutes(const std::string& time_HH_MM);

unsigned int calcDiffMinutes(unsigned x1, unsigned x2) {
    return x1 < x2 ? x2 - x1 : x1 - x2;
}

unsigned int MinimalTimeDifference(const std::vector<std::string>& times) {
    unsigned int smaller = 24 * 60;
   
    std::accumulate(times.cbegin() + 1, times.cend(), *(times.cbegin()),
            [&smaller](const std::string& s1, const std::string& s2) {
                auto diffMinutes = calcDiffMinutes(ToMinutes(s1), ToMinutes(s2));
                if(smaller > diffMinutes) {
                    smaller = diffMinutes;
                }
                return s2;
    });
    auto firstAndLastDiff = calcDiffMinutes(ToMinutes(*(times.cbegin())), ToMinutes(*(times.crbegin())));
    return smaller < firstAndLastDiff ? smaller : firstAndLastDiff;
}

// XXX: Zwraca 0 jeśli konwersja się nie powiodła. Tu powinna być jakaś inna sygnalizacja
// ale nie wiem czy znasz optionale lub czy dopuszczalne są wyjątki.
unsigned int ToMinutes(const std::string& time_HH_MM) {
    // XXX: Miałeś trochę zbyt "liberalny" RegExp
    std::regex searching{R"((0?\d|1\d|2[0-3]):([0-5]\d))"};
    std::smatch matches;
    int minutes = 0;
    int hours = 0;

    if(regex_match(time_HH_MM, matches, searching)) {
        minutes = std::stoi(matches[2]);
        hours = std::stoi(matches[1]);
    }

    return minutes + 60 * hours;
}

int main() {
    auto vec = std::vector<std::string>{"10:11", "10:15", "22:20", "23:45", "10:12"};
    std::cout << MinimalTimeDifference(vec) << std::endl;
}

Nie wiem czy naprawdę porównywać powinieneś każdy z czasów z każdym. Jeśli tak to zrobiłeś to nieprawidłowo (jak pisałem wcześniej). Ja tego nie implementowałem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1