59 lines
1.6 KiB
C
59 lines
1.6 KiB
C
#include <stdio.h>
|
|
|
|
#define SIXTY 60
|
|
//---------------------------------------------------------------------------------------
|
|
// Exercise 3
|
|
// ----------
|
|
//
|
|
// General : The program calculates difference between 2 times.
|
|
//
|
|
// Input : 2 times by HH:MM:SS format.
|
|
//
|
|
// Process : The program coverts the hours and minutes to seconds and calculate
|
|
// the seconds by subtraction.
|
|
//
|
|
// Output : Time by format HH:MM:SS.
|
|
//
|
|
//---------------------------------------------------------------------------------------
|
|
// Programmer : Cohen Idan
|
|
// Student No : None
|
|
// Date : 09.09.2019
|
|
//---------------------------------------------------------------------------------------
|
|
void main(void)
|
|
{
|
|
short hours1,
|
|
minutes1,
|
|
hours2,
|
|
minutes2;
|
|
int seconds1,
|
|
seconds2;
|
|
printf("Enter the first time (HH:MM:SS): ");
|
|
scanf("%hd:%hd:%d", &hours1, &minutes1, &seconds1);
|
|
printf("Enter the second time (HH:MM:SS): ");
|
|
scanf("%hd:%hd:%d", &hours2, &minutes2, &seconds2);
|
|
|
|
// Change the hours to minutes
|
|
minutes1 += hours1 * SIXTY;
|
|
minutes2 += hours2 * SIXTY;
|
|
|
|
// Change the minutes to seconds
|
|
seconds1 += minutes1 * SIXTY;
|
|
seconds2 += minutes2 * SIXTY;
|
|
|
|
// Subtraction between times as seconds
|
|
seconds1 -= seconds2;
|
|
|
|
// Change the seconds to minutes
|
|
minutes1 = seconds1 / SIXTY;
|
|
|
|
// Rest for seconds
|
|
seconds1 %= SIXTY;
|
|
|
|
// Change the minutes to hours
|
|
hours1 = minutes1 / SIXTY;
|
|
|
|
// Rest for minutes
|
|
minutes1 %= SIXTY;
|
|
|
|
printf("%02hd:%02hd:%02d\n", hours1, minutes1, seconds1);
|
|
} |