Nautical bell: Difference between revisions
Content added Content deleted
(correcting code→{{header|C++}}) |
No edit summary |
||
Line 5: | Line 5: | ||
It is permissible for the program to [[Run as a daemon or service|daemonize]], or to slave off a scheduler, and it is permissible to use alternative notification methods (such as producing a written notice "Two Bells Gone"), if these are more usual for the system type. |
It is permissible for the program to [[Run as a daemon or service|daemonize]], or to slave off a scheduler, and it is permissible to use alternative notification methods (such as producing a written notice "Two Bells Gone"), if these are more usual for the system type. |
||
=={{header|C++}}== |
|||
This version uses local time. |
|||
<lang cpp> |
|||
#include <iostream> |
|||
#include <iomanip> |
|||
#include <string> |
|||
#include <windows.h> |
|||
//-------------------------------------------------------------------------------------------------- |
|||
using namespace std; |
|||
//-------------------------------------------------------------------------------------------------- |
|||
class bells |
|||
{ |
|||
public: |
|||
void start() |
|||
{ |
|||
_inst = this; _w = 5; |
|||
watch[0] = "Middle"; watch[1] = "Morning"; watch[2] = "Forenoon"; watch[3] = "Afternoon"; watch[4] = "Dog"; watch[5] = "First"; |
|||
count[0] = "One"; count[1] = "Two"; count[2] = "Three"; count[3] = "Four"; count[4] = "Five"; count[5] = "Six"; count[6] = "Seven"; count[7] = "Eight"; |
|||
CreateThread( NULL, 0, bell, NULL, 0, NULL ); |
|||
} |
|||
private: |
|||
static DWORD WINAPI bell( LPVOID p ) |
|||
{ |
|||
DWORD wait = _inst->whatTime(); |
|||
while( true ) |
|||
{ |
|||
Sleep( wait ); |
|||
_inst->playBell(); |
|||
wait = _inst->whatTime(); |
|||
} |
|||
return 0; |
|||
} |
|||
DWORD whatTime() |
|||
{ |
|||
SYSTEMTIME ts; GetLocalTime( &ts ); |
|||
int m = ts.wMinute >= 30 ? ts.wMinute - 30 : ts.wMinute; |
|||
return( 1800000 - ( ( m * 60 + ts.wSecond ) * 1000 + ts.wMilliseconds ) ); |
|||
} |
|||
void playBell() |
|||
{ |
|||
SYSTEMTIME st; GetLocalTime( &st ); |
|||
int b = ( 2 * st.wHour + st.wMinute / 30 ) % 8; b = b == 0 ? 8 : b; |
|||
char hr[32]; wsprintf( hr, "%.2d:%.2d", st.wHour, st.wMinute ); |
|||
cout << left << hr << " - " << setw( 9 ) << watch[w] << " watch - " << setw( 5 ) << count[b - 1] << " Bell"; |
|||
if( b > 1 ) cout << "s"; else cout << " "; cout << " Gone." << endl; |
|||
if( b == 8 ) if( ++_w > 5 ) _w = 0; |
|||
for( int x = 0, c = 0; x < b; x++, c++ ) |
|||
{ |
|||
cout << "\7"; |
|||
Sleep( 500 ); |
|||
if( !( c % 2 ) ) Sleep( 300 ); |
|||
} |
|||
} |
|||
int _w; |
|||
string watch[7], count[8]; |
|||
static bells* _inst; |
|||
}; |
|||
//-------------------------------------------------------------------------------------------------- |
|||
bells* bells::_inst = 0; |
|||
//-------------------------------------------------------------------------------------------------- |
|||
int main( int argc, char* argv[] ) |
|||
{ |
|||
bells b; |
|||
b.start(); |
|||
while( 1 ); |
|||
return 0; |
|||
} |
|||
//-------------------------------------------------------------------------------------------------- |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
00:00 - First watch - Eight Bells Gone. |
|||
00:30 - Middle watch - One Bell Gone. |
|||
01:00 - Middle watch - Two Bells Gone. |
|||
01:30 - Middle watch - Three Bells Gone. |
|||
02:00 - Middle watch - Four Bells Gone. |
|||
02:30 - Middle watch - Five Bells Gone. |
|||
03:00 - Middle watch - Six Bells Gone. |
|||
03:30 - Middle watch - Seven Bells Gone. |
|||
04:00 - Middle watch - Eight Bells Gone. |
|||
04:30 - Morning watch - One Bell Gone. |
|||
05:00 - Morning watch - Two Bells Gone. |
|||
05:30 - Morning watch - Three Bells Gone. |
|||
06:00 - Morning watch - Four Bells Gone. |
|||
06:30 - Morning watch - Five Bells Gone. |
|||
07:00 - Morning watch - Six Bells Gone. |
|||
07:30 - Morning watch - Seven Bells Gone. |
|||
08:00 - Morning watch - Eight Bells Gone. |
|||
08:30 - Forenoon watch - One Bell Gone. |
|||
09:00 - Forenoon watch - Two Bells Gone. |
|||
09:30 - Forenoon watch - Three Bells Gone. |
|||
10:00 - Forenoon watch - Four Bells Gone. |
|||
10:30 - Forenoon watch - Five Bells Gone. |
|||
11:00 - Forenoon watch - Six Bells Gone. |
|||
11:30 - Forenoon watch - Seven Bells Gone. |
|||
12:00 - Forenoon watch - Eight Bells Gone. |
|||
12:30 - Afternoon watch - One Bell Gone. |
|||
13:00 - Afternoon watch - Two Bells Gone. |
|||
13:30 - Afternoon watch - Three Bells Gone. |
|||
14:00 - Afternoon watch - Four Bells Gone. |
|||
14:30 - Afternoon watch - Five Bells Gone. |
|||
15:00 - Afternoon watch - Six Bells Gone. |
|||
15:30 - Afternoon watch - Seven Bells Gone. |
|||
16:00 - Afternoon watch - Eight Bells Gone. |
|||
16:30 - Dog watch - One Bell Gone. |
|||
17:00 - Dog watch - Two Bells Gone. |
|||
17:30 - Dog watch - Three Bells Gone. |
|||
18:00 - Dog watch - Four Bells Gone. |
|||
18:30 - Dog watch - Five Bells Gone. |
|||
19:00 - Dog watch - Six Bells Gone. |
|||
19:30 - Dog watch - Seven Bells Gone. |
|||
20:00 - Dog watch - Eight Bells Gone. |
|||
20:30 - First watch - One Bell Gone. |
|||
21:00 - First watch - Two Bells Gone. |
|||
21:30 - First watch - Three Bells Gone. |
|||
22:00 - First watch - Four Bells Gone. |
|||
22:30 - First watch - Five Bells Gone. |
|||
23:00 - First watch - Six Bells Gone. |
|||
23:30 - First watch - Seven Bells Gone. |
|||
</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |