Код: Выделить всё
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
struct Time {
int hours, minutes;
double seconds;
// Конструктор для удобного создания времени
Time(int h = 0, int m = 0, double s = 0.0) : hours(h), minutes(m), seconds(s) {}
// Метод для вывода времени в нужном формате
std::string toString() const {
std::stringstream ss;
ss << std::setw(2) << std::setfill('0') << hours << ":"
<< std::setw(2) << std::setfill('0') << minutes << ":"
<< std::fixed << std::setprecision(6) << std::setfill('0') << seconds;
return ss.str();
}
// Метод для сравнения времени
bool operator<(const Time& other) const {
if (hours != other.hours) return hours < other.hours;
if (minutes != other.minutes) return minutes < other.minutes;
return seconds < other.seconds;
}
bool operator>(const Time& other) const {
if (hours != other.hours) return hours > other.hours;
if (minutes != other.minutes) return minutes > other.minutes;
return seconds > other.seconds;
}
// Метод для вычитания 0.1 секунды
void subtract(double sec) {
seconds -= sec;
if (seconds < 0) {
seconds += 60;
minutes--;
}
if (minutes < 0) {
minutes += 60;
hours--;
}
if (hours < 0) {
hours = 0;
minutes = 0;
seconds = 0.0;
}
}
};
int main() {
std::string input_time;
std::cout << "Enter the desired time in format 0:0:0: ";
std::getline(std::cin, input_time);
// Разделение введенной строки на часы, минуты и секунды
std::istringstream ss(input_time);
std::string segment;
std::vector<Time> times;
size_t foundColon;
while ((foundColon = input_time.find(':')) != std::string::npos) {
// Извлечение часов
int h = std::stoi(input_time.substr(0, foundColon));
input_time.erase(0, foundColon + 1);
// Извлечение минут
foundColon = input_time.find(':');
int m = std::stoi(input_time.substr(0, foundColon));
input_time.erase(0, foundColon + 1);
// Извлечение секунд
double s = std::stod(input_time); // Остаток строки — это секунды.
times.emplace_back(h, m, s);
}
Time user_time = times.back(); // Используем только последнее введенное время по вашему запросу
Time max_time(0, 0, 0.0);
// Получаем временные метки из файла (здесь просто пример)
std::vector<Time> timestamps = {
// Замените это на реальное чтение из файла
Time(0, 0, 5.0),
Time(0, 0, 8.333333),
Time(0, 1, 2.0),
Time(1, 0, 0.1)
};
// Поиск max_time, меньше user_time
for (const auto& timestamp : timestamps) {
if (timestamp < user_time && timestamp > max_time) {
max_time = timestamp;
}
}
// Вывод результата
if (max_time.seconds > 0) {
std::cout << "The largest time less than " << user_time.toString() << " is " << max_time.toString() << std::endl;
max_time.subtract(0.1); // Вычитаем 0.1 секунды
std::cout << "The time after subtracting 0:0:0.1 is " << max_time.toString() << std::endl;
} else {
std::cout << "No time found less than " << user_time.toString() << "." << std::endl;
}
return 0;
}