training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

getsecs.c (1059B)


      1 /* Esercizio 5.23 */
      2 
      3 #include <stdio.h>
      4 
      5 int getsecs(int, int, int);
      6 
      7 int main()
      8 {
      9    int h, m, s;
     10    int t_1, t_2;
     11 
     12    printf("Give me the first time (hh mm ss): ");
     13    scanf("%d%d%d", &h, &m, &s);
     14    if( !(t_1 = getsecs(h, m, s)) ) {
     15       printf("Wrong date: %d:%d:%d\n", h, m, s);
     16       return -1;
     17    }
     18 
     19    printf("Give me the first time (hh mm ss): ");
     20    scanf("%d%d%d", &h, &m, &s);
     21    if( !(t_2 = getsecs(h, m, s)) ) {
     22       printf("Wrong date: %d:%d:%d\n", h, m, s);
     23       return -1;
     24    }
     25 
     26    printf("Difference in second it: %ds\n", t_1 >= t_2 ? t_1 - t_2 : t_2 - t_1);
     27 
     28    return 0;
     29 } /* E0F main */
     30 
     31 /* Count the seconds between 12:00 and "hh:mm:ss" */
     32 int getsecs(int hh, int mm, int ss)
     33 {
     34    int i;
     35 
     36    if(hh > 12 || (hh == 12 && (mm || ss)) ) {
     37       printf("Date is deleted!\n");
     38       return 0;
     39    }
     40 
     41    /* count seconds from hh to 12 */
     42    for(i = hh; i < 12; i++) {
     43       ss += 3600;
     44    } /* eof for (i) */
     45 
     46    /* count the seconds of mm */
     47    for(i = mm; i >= 1; i--) {
     48       ss += 60;
     49    }
     50 
     51    return 43200 - ss;
     52 
     53 } /* eof getsecs() */
     54