Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- difference_of_two_times_in_minutes_v1.c
- Task from Choo Yong Han :
- https://web.facebook.com/albertchooyonghan
- https://web.facebook.com/photo.php?fbid=3902250669845726&set=pcb.1588682431290601&type=3&theater&ifg=1
- 1030 - 0830 = 120 minutes
- I see that you succeed to read data (numbers) to matrix,
- but difference of time is wrong.
- Example:
- 1030 - 0830
- You have to extract first two digits, make number of them,
- multiply that number (represent hours) by 60 minutes and
- add to it minutes made of third and fourth digit.
- 10*60+30 - 8*60+30 = 120 minutes
- Then convert 120 minutes to hours and minutes.
- 120 = 120/60 + 120%60
- First / is integer division,
- second is rest (reminder) of division.
- So,
- 120 minutes is 2 hours and 0 minutes.
- At last, if needed, convert that to
- 0200.
- This will work if both times are from same day.
- Here is a better solution using structures with integers:
- https://www.includehelp.com/code-snippets/print-difference-of-two-times-in-hours-minutes-and-seconds-in-c-language.aspx
- https://www.programiz.com/c-programming/examples/time-structure
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h> // for atoi()
- // Returns difference end_time - start_time in minutes
- int difference_of_two_times_in_minutes( char start_time[], char end_time[] )
- {
- char str_h_end_time[3], str_m_end_time[3], str_h_start_time[3], str_m_start_time[3]; // time strings
- int time2_hours, time2_minutes, time1_hours, time1_minutes; // time integers
- int difference_in_minutes;
- str_h_end_time[0] = end_time[0];
- str_h_end_time[1] = end_time[1];
- str_h_end_time[2] = '\0'; // finish the string
- time2_hours = atoi(str_h_end_time);
- str_m_end_time[0] = end_time[2];
- str_m_end_time[1] = end_time[3];
- str_m_end_time[2] = '\0'; // finish the string
- time2_minutes = atoi(str_m_end_time);
- str_h_start_time[0] = start_time[0];
- str_h_start_time[1] = start_time[1];
- str_h_start_time[2] = '\0'; // finish the string
- time1_hours = atoi(str_h_start_time);
- str_m_start_time[0] = start_time[2];
- str_m_start_time[1] = start_time[3];
- str_m_start_time[2] = '\0'; // finish the string
- time1_minutes = atoi(str_m_start_time);
- difference_in_minutes = (time2_hours - time1_hours)*60 + (time2_minutes - time1_minutes);
- return(difference_in_minutes);
- }
- int main(void)
- {
- char end_time[] = "1030";
- char start_time[] = "0830";
- int difference_in_minutes, h, m;
- difference_in_minutes = difference_of_two_times_in_minutes(start_time, end_time);
- printf("\n The difference %s - %s in minutes is %d minutes. \n",
- end_time, start_time, difference_in_minutes);
- h = difference_in_minutes / 60;
- m = difference_in_minutes % 60;
- printf("\n %d minutes is %d hours and %d minutes. \n", difference_in_minutes, h, m);
- printf("\n %d minutes is ", difference_in_minutes);
- if( h < 10 )
- printf("0%d:", h);
- else
- printf("%2d:", h);
- if( m < 10 )
- printf("0%d h \n", m);
- else
- printf("%2d h \n", m);
- return 0;
- } // main()
Advertisement
RAW Paste Data
Copied
Advertisement