How to resolve the algorithm Nautical bell step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Nautical bell step by step in the Nim 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 Nim programming language

Source code in the nim programming language

import os, strformat, times

const
  Watches = ["First", "Middle", "Morning", "Forenoon", "Afternoon", "First dog", "Last dog", "First"]
  WatchEnds = [(0, 0), (4, 0), (8, 0), (12, 0), (16, 0), (18, 0), (20, 0), (23, 59)]
  Bells = array[1..8, string](["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"])
  Ding = "ding!"


proc nb(h, m: Natural) =
  var bell = (h * 60 + m) div 30 mod 8
  if bell == 0: bell = 8
  let hm = (h, m)
  var watch = 0
  while hm > WatchEnds[watch]: inc watch
  let plural = if bell == 1: ' ' else: 's'
  var dings = Ding
  for i in 2..bell:
    if i mod 2 != 0: dings.add ' '
    dings.add Ding
  echo &"{h:02d}:{m:02d} {Watches[watch]:>9} watch {Bells[bell]:>5} bell{plural}  {dings}"


proc simulateOneDay() =
  for h in 0..23:
    for m in [0, 30]:
      nb(h, m)
  nb(0, 0)


when isMainModule:

  simulateOneDay()

  while true:
    let d = getTime().utc()
    var m = d.second + (d.minute mod 30) * 60
    if m == 0:
      nb(d.hour, d.minute)
    sleep((1800 - m) * 1000)  # In milliseconds.


  

You may also check:How to resolve the algorithm Read entire file step by step in the Retro programming language
You may also check:How to resolve the algorithm Additive primes step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Video display modes step by step in the smart BASIC programming language
You may also check:How to resolve the algorithm Stack step by step in the Pascal programming language
You may also check:How to resolve the algorithm The Name Game step by step in the Visual Basic .NET programming language