Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions extra-task-1.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <assert.h>
using namespace std;

double seconds_difference(double time_1, double time_2)
{
// your implementation goes here...

assert((time_1 >= 0) && (time_2 >= 0));
return time_2 - time_1;
/*
Return the number of seconds later that a time in seconds
time_2 is than a time in seconds time_1.
Expand All @@ -22,6 +25,8 @@ double seconds_difference(double time_1, double time_2)

double hours_difference(double time_1, double time_2)
{
assert((time_1 >= 0) && (time_2 >= 0));
return (time_2 - time_1) / 3600.0;
/*
Return the number of hours later that a time in seconds
time_2 is than a time in seconds time_1.
Expand All @@ -42,6 +47,8 @@ double hours_difference(double time_1, double time_2)

double to_float_hours(int hours, int minutes, int seconds)
{
assert((hours >= 0) && (minutes >= 0) && (seconds >= 0));
return hours + minutes / 60.0 + seconds / 3600.0;
/*
Return the total number of hours in the specified number
of hours, minutes, and seconds.
Expand All @@ -61,6 +68,8 @@ double to_float_hours(int hours, int minutes, int seconds)

double to_24_hour_clock(double hours)
{
assert(hours >= 0);
return hours % 24;
/*
hours is a number of hours since midnight. Return the
hour as seen on a 24-hour clock.
Expand Down Expand Up @@ -88,6 +97,23 @@ double to_24_hour_clock(double hours)
*/
}

int get_hours(int seconds)
{
assert(seconds >= 0);
return seconds / 3600;
}

int get_minutes(int seconds)
{
assert(seconds >= 0);
return (seconds % 3600) / 60;
}

int get_seconds(int seconds)
{
assert(seconds >= 0);
return seconds % 60;
}
/*
Implement three functions
* get_hours
Expand All @@ -111,6 +137,11 @@ double to_24_hour_clock(double hours)

double time_to_utc(int utc_offset, double time)
{
assert(time >= 0);
double result = time - utc_offset;
if (result < 0)
result += 24.0;
return result % 24.0;
/*
Return time at UTC+0, where utc_offset is the number of hours away from
UTC+0.
Expand Down Expand Up @@ -139,6 +170,11 @@ double time_to_utc(int utc_offset, double time)

double time_from_utc(int utc_offset, double time)
{
assert(time >= 0);
double result = time + utc_offset;
if (result < 0)
result += 24.0;
return result % 24.0;
/*
Return UTC time in time zone utc_offset.

Expand Down Expand Up @@ -167,3 +203,42 @@ double time_from_utc(int utc_offset, double time)
0.0
*/
}


int main()
{
//���� ��� seconds_difference
assert(fabs(seconds_difference(3600.0, 1800.0) - (-1800.0)) < DBL_EPSILON);
assert(fabs(seconds_difference(1800.0, 2160.0) - 360.0) < DBL_EPSILON);
assert(fabs(seconds_difference(1800.0, 1800.0) - 0.0) < DBL_EPSILON);

//���� ��� hours_difference
assert(fabs(hours_difference(1800.0, 3600.0) - 0.5) < DBL_EPSILON);
assert(fabs(hours_difference(3600.0, 1800.0) - (-0.5)) < DBL_EPSILON);
assert(fabs(hours_difference(1800.0, 1800.0) - 0.0) < DBL_EPSILON);

//���� ��� to_float_hours
assert(fabs(to_float_hours(0, 15, 0) - 0.25) < DBL_EPSILON);
assert(fabs(to_float_hours(2, 45, 9) - 2.7525) < DBL_EPSILON);
assert(fabs(to_float_hours(1, 0, 36) - 1.01) < DBL_EPSILON);

//���� ��� to_24_hour_clock
assert(to_24_hour_clock(48) - 0 < DBL_EPSILON);
assert(to_24_hour_clock(25) - 1 < DBL_EPSILON);
assert(to_24_hour_clock(28.5) - 4.5 < DBL_EPSILON);

//���� ��� get_hours, get_minutes, get_seconds
assert(get_hours(3800) == 1);
assert(get_minutes(3800) == 3);
assert(get_seconds(3800) == 20);

//���� ��� time_to_utc
assert(fabs(time_to_utc(0, 12.0) - 12.0) < DBL_EPSILON);
assert(fabs(time_to_utc(1, 12.0) - 11.0) < DBL_EPSILON);
assert(fabs(time_to_utc(-1, 12.0) - 13.0) < DBL_EPSILON);

//���� ��� time_from_utc
assert(fabs(time_from_utc(0, 12.0) - 12.0) < DBL_EPSILON);
assert(fabs(time_from_utc(6, 6.0) - 12.0) < DBL_EPSILON);
assert(fabs(time_from_utc(-7, 6.0) - 23.0) < DBL_EPSILON);
}