Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* fscanf, fprintf */
- #include <errno.h> /* errno */
- #include <string.h> /* strerror */
- #include <stdbool.h>
- typedef struct
- {
- unsigned h;
- unsigned m;
- unsigned s;
- } HMS;
- /* format time */
- bool convert(HMS* hms)
- {
- bool over24 = false;
- if (hms->s >= 60) { hms->m++; hms->s%=60; }
- if (hms->m >= 60) { hms->h++; hms->m%=60; }
- if (hms->h >= 24) { hms->h%=24; over24=true; }
- return over24;
- }
- int main(int argc, char** argv)
- {
- HMS t;
- int n = 0;
- errno = 0;
- if (argc> 1)
- {
- n = sscanf( argv[1] , "%2u:%2u:%2u", &t.h, &t.m, &t.s);
- }
- else
- {
- fprintf(stdout, "Enter the time (hh::mm:ss):\t" );
- n = fscanf( stdin, "%2u:%2u:%2u", &t.h, &t.m, &t.s);
- }
- if( n==3 && !errno)
- {
- if ( convert(&t) ) fprintf( stdout, "clock turned over (>24 hrs)\n\n");
- fprintf( stdout, "hours: %d \n" , t.h);
- fprintf( stdout, "minutes: %d \n" , t.m);
- fprintf( stdout, "seconds: %d \n" , t.s);
- }
- else
- fprintf(stdout, "invalid format. %s\n", strerror(errno) );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement