Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /* Using a struct so that we never dereference a NULL pointer if someone passes in too short of an int * */
  4. typedef struct myTime
  5. {
  6.     int *arr; // must have three integers only
  7. } myTime;
  8. myTime *timeConstruct(void); // returns an initialized myTime structure pointer
  9. void timeSet(myTime *time,int h,int m,int s); // sets the structure to a valid time
  10. int secConvert(myTime *time); // convert hour:min:sec to seconds
  11. int *timeConvert(int seconds); // convert seconds into a three position int *
  12. void timePrint(myTime *time); // print time in hr:min:sec format
  13. void timePrintSecs(myTime *time); // print time in seconds
  14. myTime *timeAdd(myTime *t1,myTime *t2);
  15. int main(void)
  16. {
  17.     myTime *t1=timeConstruct();
  18.     myTime *t2=timeConstruct();
  19.     timeSet(t1,1,2,3);
  20.     timeSet(t2,4,5,6);
  21.     timePrint(t1);
  22.     timePrintSecs(t1);
  23.     timePrint(t2);
  24.     timePrintSecs(t2);
  25.     myTime *t3=timeAdd(t1,t2);
  26.     timePrint(t3);
  27.     timePrintSecs(t3);
  28.     return 0;
  29. }
  30. myTime *timeAdd(myTime *t1,myTime *t2)
  31. {
  32.     if(t1==NULL)
  33.     {
  34.         fprintf(stderr,"Error: t1 points to NULL in timeAdd.\n");
  35.         exit(EXIT_FAILURE);
  36.     }
  37.     if(t2==NULL)
  38.     {
  39.         fprintf(stderr,"Error: t2 points to NULL in timeAdd.\n");
  40.         exit(EXIT_FAILURE);
  41.     }
  42.     int seconds=0;
  43.     seconds+=secConvert(t1);
  44.     seconds+=secConvert(t2);
  45.     myTime *ret=timeConstruct();
  46.     free(ret->arr); // no memory leaks!
  47.     ret->arr=timeConvert(seconds);
  48.     return ret;
  49. }
  50. void timePrintSecs(myTime *time)
  51. {
  52.     if(time==NULL)
  53.     {
  54.         fprintf(stderr,"Error: time points to NULL in timePrintSecs.\n");
  55.         exit(EXIT_FAILURE);
  56.     }
  57.     printf("%d seconds\n",secConvert(time));
  58. }
  59. void timePrint(myTime *time)
  60. {
  61.     if(time==NULL)
  62.     {
  63.         fprintf(stderr,"Error: time points to NULL in timePrint.\n");
  64.         exit(EXIT_FAILURE);
  65.     }
  66.     printf("%d:%d:%d\n",time->arr[0],time->arr[1],time->arr[2]);
  67. }
  68. void timeSet(myTime *time,int h,int m,int s)
  69. {
  70.     if(time==NULL)
  71.     {
  72.         fprintf(stderr,"Error: time points to NULL in timeSet.\n");
  73.         exit(EXIT_FAILURE);
  74.     }  
  75.     if(m>60)
  76.     {
  77.         fprintf(stderr,"Error: m>60 in timeSet.\n");
  78.         exit(EXIT_FAILURE);
  79.     }
  80.     if(s>60)
  81.     {
  82.         fprintf(stderr,"Error: s>60 in timeSet.\n");
  83.         exit(EXIT_FAILURE);
  84.     }
  85.     time->arr[0]=h;
  86.     time->arr[1]=m;
  87.     time->arr[2]=s;
  88. }
  89. myTime *timeConstruct(void)
  90. {
  91.     myTime *ret=malloc(sizeof(myTime));
  92.     if(ret==NULL)
  93.     {
  94.         fprintf(stderr,"Error: malloc returns NULL for ret in timeConstruct.\n");
  95.         exit(EXIT_FAILURE);
  96.     }
  97.     int *arr=calloc(3,sizeof(int));
  98.     if(arr==NULL)
  99.     {
  100.         fprintf(stderr,"Error: calloc returns NULL for arr in timeConstruct.\n");
  101.         exit(EXIT_FAILURE);
  102.     }
  103.     ret->arr=arr;
  104.     return ret;
  105. }
  106. int secConvert(myTime *time)
  107. {
  108.     if(time==NULL)
  109.     {
  110.         fprintf(stderr,"Error: time points to NULL in secConvert.\n");
  111.         exit(EXIT_FAILURE);
  112.     }
  113.     int ret=0;
  114.     ret+=3600*(time->arr[0]);
  115.     ret+=60*(time->arr[1]);
  116.     ret+=(time->arr[2]);
  117.     return ret;
  118. }
  119. int *timeConvert(int seconds)
  120. {
  121.     int *ret=calloc(3,sizeof(int));
  122.     if(ret==NULL)
  123.     {
  124.         fprintf(stderr,"Error: calloc returns NULL for ret in timeConvert.\n");
  125.         exit(EXIT_FAILURE);
  126.     }
  127.     ret[0]=seconds/3600;
  128.     ret[1]=(seconds%3600)/60;
  129.     ret[2]=seconds%60;
  130.     return ret;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement