aboutsummaryrefslogtreecommitdiff
path: root/challenge-120/eric-cheung/excel-vba/ch-2.bas
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-07-06 10:46:01 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-07-06 10:46:01 +0100
commit634b07e9f2eff10799300ca2531b161730c95202 (patch)
tree0fd117e6988524adfffebe76ad961b9aec9f73b0 /challenge-120/eric-cheung/excel-vba/ch-2.bas
parentf0f916396726553a917ab2393ab8e87e4d831192 (diff)
downloadperlweeklychallenge-club-634b07e9f2eff10799300ca2531b161730c95202.tar.gz
perlweeklychallenge-club-634b07e9f2eff10799300ca2531b161730c95202.tar.bz2
perlweeklychallenge-club-634b07e9f2eff10799300ca2531b161730c95202.zip
- Added solutions by Cheok-Yin Fung.
Diffstat (limited to 'challenge-120/eric-cheung/excel-vba/ch-2.bas')
-rw-r--r--challenge-120/eric-cheung/excel-vba/ch-2.bas47
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-120/eric-cheung/excel-vba/ch-2.bas b/challenge-120/eric-cheung/excel-vba/ch-2.bas
new file mode 100644
index 0000000000..2fad9a79ea
--- /dev/null
+++ b/challenge-120/eric-cheung/excel-vba/ch-2.bas
@@ -0,0 +1,47 @@
+Attribute VB_Name = "ModTask_02"
+Option Explicit
+'https://www.varsitytutors.com/basic_geometry-help/how-to-find-the-angle-of-clock-hands
+'https://www.omnicalculator.com/math/clock-angle#method-2-use-a-clock-angle-formula
+
+Function ClockAngle(strTime) As Double
+
+ Const nSectorToDegree As Integer = 30
+ Const nDayToHour As Integer = 12
+ Const nDiv As Integer = 5
+
+ Dim nHour As Integer, dMinute As Double, dFactor As Double
+
+ nHour = Val(Left(strTime, 2)) Mod nDayToHour
+ dMinute = Val(Right(strTime, 2)) / nDiv
+
+ ClockAngle = Abs(nHour - dMinute) * nSectorToDegree
+
+ If dMinute > 0 Then
+ dFactor = dMinute * nSectorToDegree / nDayToHour
+ If nHour >= dMinute Then
+ ClockAngle = ClockAngle + dFactor
+ Else
+ ClockAngle = ClockAngle - dFactor
+ End If
+ End If
+
+ ClockAngle = Abs(Round(ClockAngle, 2))
+
+ If ClockAngle > 180 Then
+ ClockAngle = 360 - ClockAngle
+ End If
+
+End Function
+
+Sub Task_02()
+
+ Dim varInputNum
+
+ Do
+ varInputNum = InputBox("Please enter the time", strMyTitle, "04:00")
+ If varInputNum = "" Then Exit Sub
+ Loop Until varInputNum <> ""
+
+ MsgBox "The smallest angle spanned by the time " & varInputNum & " is: " & ClockAngle(varInputNum) & " degrees"
+
+End Sub