Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do bonus task #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
241 changes: 100 additions & 141 deletions extra-task-1.cpp
Original file line number Diff line number Diff line change
@@ -1,169 +1,128 @@
#include <iostream>
#include <cassert>
#include <cmath>
#include <cfloat>
using namespace std;

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

/*
Return the number of seconds later that a time in seconds
time_2 is than a time in seconds time_1.

>>> seconds_difference(1800.0, 3600.0)
1800.0

>>> seconds_difference(3600.0, 1800.0)
-1800.0

>>> seconds_difference(1800.0, 2160.0)
360.0
return time_2 - time_1;
}

>>> seconds_difference(1800.0, 1800.0)
0.0
*/
void test_seconds_difference()
{
assert(fabs(seconds_difference(1800.0, 3600.0) - 1800.0) < DBL_EPSILON);
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)) < DBL_EPSILON);
}

double hours_difference(double time_1, double time_2)
{
/*
Return the number of hours later that a time in seconds
time_2 is than a time in seconds time_1.

>>> hours_difference(1800.0, 3600.0)
0.5
return (time_2 - time_1) / 3600.0;
}

>>> hours_difference(3600.0, 1800.0)
-0.5
void test_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, 2160.0) - 0.1) < DBL_EPSILON);
assert(fabs(hours_difference(1800.0, 1800.0)) < DBL_EPSILON);
}

>>> hours_difference(1800.0, 2160.0)
0.1
double to_float_hours(int hours, int minutes, int seconds)
{
return hours + minutes / 60.0 + seconds / 3600.0;
}

>>> hours_difference(1800.0, 1800.0)
0.0
*/
void test_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);
}

double to_float_hours(int hours, int minutes, int seconds)
double to_24_hour_clock(double hours)
{
/*
Return the total number of hours in the specified number
of hours, minutes, and seconds.
return fmod(hours, 24.0);
}

Precondition: 0 <= minutes < 60 and 0 <= seconds < 60
void test_to_24_hour_clock()
{
assert(fabs(to_24_hour_clock(24) - 0) < DBL_EPSILON);
assert(fabs(to_24_hour_clock(48) - 0) < DBL_EPSILON);
assert(fabs(to_24_hour_clock(25) - 1) < DBL_EPSILON);
assert(fabs(to_24_hour_clock(4) - 4) < DBL_EPSILON);
assert(fabs(to_24_hour_clock(28.5) - 4.5) < DBL_EPSILON);
}

>>> to_float_hours(0, 15, 0)
0.25
int get_hours(int seconds)
{
return seconds / 3600;
}

>>> to_float_hours(2, 45, 9)
2.7525
int get_minutes(int seconds)
{
return (seconds % 3600) / 60;
}

>>> to_float_hours(1, 0, 36)
1.01
*/
int get_seconds(int seconds)
{
return seconds % 60;
}

double to_24_hour_clock(double hours)
void test_get_time_parts()
{
/*
hours is a number of hours since midnight. Return the
hour as seen on a 24-hour clock.

Precondition: hours >= 0

>>> to_24_hour_clock(24)
0

>>> to_24_hour_clock(48)
0

>>> to_24_hour_clock(25)
1

>>> to_24_hour_clock(4)
4

>>> to_24_hour_clock(28.5)
4.5

You may wish to inspect various function in <cmath> to work
with integer and fractional part of a hours separately.

*/
}

/*
Implement three functions
* get_hours
* get_minutes
* get_seconds
They are used to determine the hours part, minutes part and seconds part
of a time in seconds. E.g.:

>>> get_hours(3800)
1

>>> get_minutes(3800)
3

>>> get_seconds(3800)
20

In other words, if 3800 seconds have elapsed since midnight,
it is currently 01:03:20 (hh:mm:ss).
*/
assert(get_hours(3800) == 1);
assert(get_minutes(3800) == 3);
assert(get_seconds(3800) == 20);
}

double time_to_utc(int utc_offset, double time)
{
/*
Return time at UTC+0, where utc_offset is the number of hours away from
UTC+0.
You may be interested in:
https://en.wikipedia.org/wiki/Coordinated_Universal_Time

>>> time_to_utc(+0, 12.0)
12.0

>>> time_to_utc(+1, 12.0)
11.0

>>> time_to_utc(-1, 12.0)
13.0

>>> time_to_utc(-11, 18.0)
5.0

>>> time_to_utc(-1, 0.0)
1.0

>>> time_to_utc(-1, 23.0)
0.0
*/
return fmod((24 + time - utc_offset), 24.0);
}

void test_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);
assert(fabs(time_to_utc(-11, 18.0) - 5.0) < DBL_EPSILON);
assert(fabs(time_to_utc(-1, 0.0) - 1.0) < DBL_EPSILON);
assert(fabs(time_to_utc(-1, 23.0) - 0.0) < DBL_EPSILON);
}

double time_from_utc(int utc_offset, double time)
{
/*
Return UTC time in time zone utc_offset.

>>> time_from_utc(+0, 12.0)
12.0

>>> time_from_utc(+1, 12.0)
13.0

>>> time_from_utc(-1, 12.0)
11.0

>>> time_from_utc(+6, 6.0)
12.0

>>> time_from_utc(-7, 6.0)
23.0

>>> time_from_utc(-1, 0.0)
23.0

>>> time_from_utc(-1, 23.0)
22.0

>>> time_from_utc(+1, 23.0)
0.0
*/
return fmod((24 + time + utc_offset), 24.0);
}

void test_time_from_utc()
{
assert(fabs(time_from_utc(+0, 12.0) - 12.0) < DBL_EPSILON);
assert(fabs(time_from_utc(+1, 12.0) - 13.0) < DBL_EPSILON);
assert(fabs(time_from_utc(-1, 12.0) - 11.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);
assert(fabs(time_from_utc(-1, 0.0) - 23.0) < DBL_EPSILON);
assert(fabs(time_from_utc(-1, 23.0) - 22.0) < DBL_EPSILON);
assert(fabs(time_from_utc(+1, 23.0) - 0.0) < DBL_EPSILON);
}

int main()
{
setlocale(LC_ALL, "RUSSIAN");

test_seconds_difference();
test_hours_difference();
test_to_float_hours();
test_to_24_hour_clock();
test_get_time_parts();
test_time_to_utc();
test_time_from_utc();

cout << "����� ������ �������" << endl;

return 0;
}