Guest User

Reddit Clock CS Question in C

a guest
Nov 19th, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct{
  4.   int h,m,s;
  5. }Clock;
  6. void startClock(Clock *clock){
  7.   clock->h = 0;
  8.   clock->m = 0;
  9.   clock->s = 0;
  10. }
  11. void incClock(Clock *clock){
  12.   clock->s ++;
  13.   if (clock->s>=60){
  14.     clock->s = 0;
  15.     clock->m ++;
  16.     if (clock->m>=60){
  17.       clock->m = 0;
  18.       clock->h ++;
  19.       if (clock->h>12){
  20.     clock->h = 1;
  21.       }
  22.     }
  23.   }
  24. }
  25. void decClock(Clock *clock){
  26.   clock->s --;
  27.   if (clock->s<0){
  28.     clock->s = 59;
  29.     clock->m --;
  30.     if (clock->m<0){
  31.       clock->m = 59;
  32.       clock->h --;
  33.       if (clock->h<=0){
  34.     clock->h = 12;
  35.       }
  36.     }
  37.   }
  38. }
  39. int getSecs(Clock *clock){
  40.   return clock->s + clock->m*60 + clock->h*3600;
  41. }
  42. int main(){
  43.   Clock clock1;
  44.   Clock clock2;
  45.   startClock(&clock1);
  46.   startClock(&clock2);
  47.   int i,count=0;
  48.   for (i=0;i<60*60*24;i++){
  49.     if (getSecs(&clock1) == getSecs(&clock2)){
  50.       count++;
  51.     }
  52.     incClock(&clock1);
  53.     decClock(&clock2);
  54.   }
  55.   printf("Count : %d\n",count);
  56.   return 0;
  57. }
  58.  
  59.  
Add Comment
Please, Sign In to add comment