Task:

The current reading of the electronic clock: m hours (0 <= m <= 23), n min (0 <= n <= 59), k sec (0 <= k <= 59). What time will the clock be shown after p h d min rc?


(The problem on the topic "Linear algorithms", but I, trying to solve it, exceeded the limit).

My trial:

#include <stdio.h> #include <conio.h> int main() { int h, m, s; long int h_, m_, s_; h=23; m=59; s=59; printf("Текущее время: %d : %d : %d",h,m,s); printf("\nВведите поправку: часы, затем минуты и секунды"); scanf("%ld%ld%ld",&h_,&m_,&s_); if ( h+h_>=0 && h+h_<=23 ) printf ("Новое значение часов: %d", h+h_ ) if ( h+h_<=23+24 && h+h_>=0+24 ) printf("Новое значение часов: %d", h+h_-24); if ( h+h_>=24+24 && h+h_<=47+24) printf("Новое значение часов: %d", h+h_-24*2); ... ... // некий цикл 
  • Or simply translate everything in seconds, count in seconds and then translate back to h:m:s . Or carefully implement the summation with the transfer, and first seconds, then minutes and only the last hour are summed up first. - AnT
  • And for the time there is no library, so as not to translate the most ??? ala <time.h> ? - Vitaly Shebanits
  • @ VitaliyShebanits Of course, there is ... Maybe it should be, but I assumed that it was not necessary (we didn’t pass it). I "ll ask. - Oleg Ostapchuk
  • Tag [циклы] ? Why is this tag on the question? And why in the code there is a comment about "a certain cycle"? Where did the cycle come from? - AnT
  • You can remove. It just seemed to me that there was a cycle. I will remove then ... - Oleg Ostapchuk

2 answers 2

Everything is simple - you fold in a column in a 60-level system. I believe that all input numbers are positive. If there are negative ones, it is written in the same way, but with a loan from the next category ...

 printf("\nВведите поправку: часы, затем минуты и секунды"); scanf("%ld%ld%ld",&p,&d,&r); // Начинаем с секунд s += r; // Перенос, остаток: m += s/60; s %= 60; // Минуты m += d; h += m/60; m %= 60; h += p; h %= 24; 
  • What to divide -1 to get 23 in the rest? (Enter -24). Please explain. - Oleg Ostapchuk
  • Did you see my note? "I believe that all numbers entered are positive." ? In general, it is enough to add 24 (if we are talking about hours). - Harry
  • It turns out branching (for positive h% = 24, for negative - another). Secondly, for negative ones: -1 + 24 = 23 - everything is correct, -25 + 24 - no longer works, so it is not enough to add 24. - Oleg Ostapchuk
  • You first work out the rest, and only then add. It is clear that this will work badly. - Harry
  • -48% 24 + 24 = 24, not o; moreover, 1000% 24 = 16 + 24, i.e. branch is still needed ... - Oleg Ostapchuk
 считываем [h_] [m_] [s_]; считаываем на сколько нужно перевести [h] [m] [s]; //переводим и то и то в секунды int tempNextSec = h * 3600 + m * 60 + s; int tempSec = h_ * 3600 + m_ * 60 + s_; // Сложим int curSec = tempSec + tempNextSec; // Отнимем дни и округлим к меньшему if ( floor(curSec / 60*60*24) > 0) curSec = curSec - 60*60*24 * floor(curSec / 60*60*24); // Узнаем сколько часов int tempHour = floor(curSec / 3600); //Можно вывести.. //Теперь отнимем от общего curSec -=tempHour*3600; // Минуты int tempMinutes = floor(curSec / 60); // Так же можно вывести... // Секунды curSec -= tempMinutes * 60 

This algorithm ... would write code if I knew C)))

  • Use the remainder of the division, and the floors will not be needed. - MBo
  • @MBo wrote the same algorithm, and the flor is for understanding ... otherwise it would not be clear at all) I understand that the second is a novice) - Vitaly Shebanits
  • Work in seconds is a good idea, and using spreading real arithmetic where curSec = curSec% is enough (60 * 60 * 24) is bad. - MBo