Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* Using a struct so that we never dereference a NULL pointer if someone passes in too short of an int * */
- typedef struct myTime
- {
- int *arr; // must have three integers only
- } myTime;
- myTime *timeConstruct(void); // returns an initialized myTime structure pointer
- void timeSet(myTime *time,int h,int m,int s); // sets the structure to a valid time
- int secConvert(myTime *time); // convert hour:min:sec to seconds
- int *timeConvert(int seconds); // convert seconds into a three position int *
- void timePrint(myTime *time); // print time in hr:min:sec format
- void timePrintSecs(myTime *time); // print time in seconds
- myTime *timeAdd(myTime *t1,myTime *t2);
- int main(void)
- {
- myTime *t1=timeConstruct();
- myTime *t2=timeConstruct();
- timeSet(t1,1,2,3);
- timeSet(t2,4,5,6);
- timePrint(t1);
- timePrintSecs(t1);
- timePrint(t2);
- timePrintSecs(t2);
- myTime *t3=timeAdd(t1,t2);
- timePrint(t3);
- timePrintSecs(t3);
- return 0;
- }
- myTime *timeAdd(myTime *t1,myTime *t2)
- {
- if(t1==NULL)
- {
- fprintf(stderr,"Error: t1 points to NULL in timeAdd.\n");
- exit(EXIT_FAILURE);
- }
- if(t2==NULL)
- {
- fprintf(stderr,"Error: t2 points to NULL in timeAdd.\n");
- exit(EXIT_FAILURE);
- }
- int seconds=0;
- seconds+=secConvert(t1);
- seconds+=secConvert(t2);
- myTime *ret=timeConstruct();
- free(ret->arr); // no memory leaks!
- ret->arr=timeConvert(seconds);
- return ret;
- }
- void timePrintSecs(myTime *time)
- {
- if(time==NULL)
- {
- fprintf(stderr,"Error: time points to NULL in timePrintSecs.\n");
- exit(EXIT_FAILURE);
- }
- printf("%d seconds\n",secConvert(time));
- }
- void timePrint(myTime *time)
- {
- if(time==NULL)
- {
- fprintf(stderr,"Error: time points to NULL in timePrint.\n");
- exit(EXIT_FAILURE);
- }
- printf("%d:%d:%d\n",time->arr[0],time->arr[1],time->arr[2]);
- }
- void timeSet(myTime *time,int h,int m,int s)
- {
- if(time==NULL)
- {
- fprintf(stderr,"Error: time points to NULL in timeSet.\n");
- exit(EXIT_FAILURE);
- }
- if(m>60)
- {
- fprintf(stderr,"Error: m>60 in timeSet.\n");
- exit(EXIT_FAILURE);
- }
- if(s>60)
- {
- fprintf(stderr,"Error: s>60 in timeSet.\n");
- exit(EXIT_FAILURE);
- }
- time->arr[0]=h;
- time->arr[1]=m;
- time->arr[2]=s;
- }
- myTime *timeConstruct(void)
- {
- myTime *ret=malloc(sizeof(myTime));
- if(ret==NULL)
- {
- fprintf(stderr,"Error: malloc returns NULL for ret in timeConstruct.\n");
- exit(EXIT_FAILURE);
- }
- int *arr=calloc(3,sizeof(int));
- if(arr==NULL)
- {
- fprintf(stderr,"Error: calloc returns NULL for arr in timeConstruct.\n");
- exit(EXIT_FAILURE);
- }
- ret->arr=arr;
- return ret;
- }
- int secConvert(myTime *time)
- {
- if(time==NULL)
- {
- fprintf(stderr,"Error: time points to NULL in secConvert.\n");
- exit(EXIT_FAILURE);
- }
- int ret=0;
- ret+=3600*(time->arr[0]);
- ret+=60*(time->arr[1]);
- ret+=(time->arr[2]);
- return ret;
- }
- int *timeConvert(int seconds)
- {
- int *ret=calloc(3,sizeof(int));
- if(ret==NULL)
- {
- fprintf(stderr,"Error: calloc returns NULL for ret in timeConvert.\n");
- exit(EXIT_FAILURE);
- }
- ret[0]=seconds/3600;
- ret[1]=(seconds%3600)/60;
- ret[2]=seconds%60;
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement