How to resolve the algorithm Nautical bell step by step in the C++ programming language
How to resolve the algorithm Nautical bell step by step in the C++ programming language
Table of Contents
Problem Statement
Write a small program that emulates a nautical bell producing a ringing bell pattern at certain times throughout the day. The bell timing should be in accordance with Greenwich Mean Time, unless locale dictates otherwise. It is permissible for the program to 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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Nautical bell step by step in the C++ programming language
The provided C++ code is a simple program that simulates the ringing of bells every hour and half-hour, announcing the time using traditional nautical terminology. Let's break down the code and explain its functionality step by step:
-
Class Definition: The code defines a class named
bells
that encapsulates the logic for simulating the bell ringing. -
Class Member Functions:
- start(): This function is responsible for initializing the program. It defines the time format for displaying the hour and minute and creates a separate thread that will handle the bell ringing.
- bell(): This is a static function that runs on a separate thread. It calculates the time until the next bell should ring, waits for that duration, and then calls
playBell()
to simulate the bell sound. - waitTime(): This function calculates the amount of time to wait until the next bell is due to ring based on the current system time.
- playBell(): This function plays a simulated bell sound and displays a message indicating the time, watch period, and number of bells.
-
Global Variables:
watch[7]
andcount[8]
: These are arrays that store the names of watch periods (e.g., "Middle", "Morning") and the count of bells (e.g., "One", "Two")._inst
: This is a static pointer to an instance of thebells
class. It's used to access class member functions from thebell()
thread.
-
Main Function:
- In the
main
function, an instance of thebells
class is created and itsstart()
function is called. - The
while(1)
loop keeps the program running indefinitely. This is necessary to keep the bell ringing thread alive.
- In the
In summary, this program simulates the ringing of bells at specific times using a separate thread to handle the timing. It displays the time using traditional nautical terminology, indicating the watch period and the number of bells that have rung.
Source code in the cpp programming language
#include <iostream>
#include <string>
#include <windows.h>
//--------------------------------------------------------------------------------------------------
using namespace std;
//--------------------------------------------------------------------------------------------------
class bells
{
public:
void start()
{
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";
_inst = this; CreateThread( NULL, 0, bell, NULL, 0, NULL );
}
private:
static DWORD WINAPI bell( LPVOID p )
{
DWORD wait = _inst->waitTime();
while( true )
{
Sleep( wait );
_inst->playBell();
wait = _inst->waitTime();
}
return 0;
}
DWORD waitTime()
{
GetLocalTime( &st );
int m = st.wMinute >= 30 ? st.wMinute - 30 : st.wMinute;
return( 1800000 - ( ( m * 60 + st.wSecond ) * 1000 + st.wMilliseconds ) );
}
void playBell()
{
GetLocalTime( &st );
int b = ( 2 * st.wHour + st.wMinute / 30 ) % 8; b = b == 0 ? 8 : b;
int w = ( 60 * st.wHour + st.wMinute );
if( w < 1 ) w = 5; else w = ( w - 1 ) / 240;
char hr[32]; wsprintf( hr, "%.2d:%.2d", st.wHour, st.wMinute );
cout << hr << " - " << watch[w] << " watch - " << count[b - 1] << " Bell";
if( b > 1 ) cout << "s"; else cout << " "; cout << " Gone." << endl;
for( int x = 0, c = 1; x < b; x++, c++ )
{
cout << "\7"; Sleep( 500 );
if( !( c % 2 ) ) Sleep( 300 );
}
}
SYSTEMTIME st;
string watch[7], count[8];
static bells* _inst;
};
//--------------------------------------------------------------------------------------------------
bells* bells::_inst = 0;
//--------------------------------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
bells b; b.start();
while( 1 ); // <- runs forever!
return 0;
}
//--------------------------------------------------------------------------------------------------
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the REXX programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the Rust programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Deceptive numbers step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Fusc sequence step by step in the Processing programming language