From fbda1c9b2f044b85cd9ad661287a8d9cc49baa67 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 28 Jul 2021 00:30:56 +0100 Subject: - Added Excel VBA solutions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_123.xlsm | Bin 0 -> 31455 bytes challenge-123/eric-cheung/excel-vba/ch-1.bas | 48 +++++++++++ challenge-123/eric-cheung/excel-vba/ch-2.bas | 88 +++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 challenge-123/eric-cheung/excel-vba/Challenge_123.xlsm create mode 100644 challenge-123/eric-cheung/excel-vba/ch-1.bas create mode 100644 challenge-123/eric-cheung/excel-vba/ch-2.bas diff --git a/challenge-123/eric-cheung/excel-vba/Challenge_123.xlsm b/challenge-123/eric-cheung/excel-vba/Challenge_123.xlsm new file mode 100644 index 0000000000..2f269bfc7a Binary files /dev/null and b/challenge-123/eric-cheung/excel-vba/Challenge_123.xlsm differ diff --git a/challenge-123/eric-cheung/excel-vba/ch-1.bas b/challenge-123/eric-cheung/excel-vba/ch-1.bas new file mode 100644 index 0000000000..b4d54ddb6d --- /dev/null +++ b/challenge-123/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,48 @@ +Attribute VB_Name = "ModTask_01" +Option Explicit +Public Const strMyTitle As String = "Eric Cheung" + +Function IsUgly(nNum As Integer) As Boolean + + IsUgly = False + + If _ + nNum = 1 _ + Or nNum = 2 _ + Or nNum = 3 _ + Or nNum = 5 _ + Or nNum Mod 2 = 0 _ + Or nNum Mod 3 = 0 _ + Or nNum Mod 5 = 0 _ + Then + IsUgly = True + Exit Function + End If + +End Function + +Sub Task_01() + + Dim varInputNum + Dim nCount As Integer, nUglyCount As Integer + Dim strMsg As String + + Do + varInputNum = InputBox("Please enter a positive integer", strMyTitle, 1) + If varInputNum = "" Then Exit Sub + Loop Until varInputNum > 0 + + nCount = 0 + nUglyCount = 0 + + Do + nCount = nCount + 1 + If IsUgly(nCount) Then + nUglyCount = nUglyCount + 1 + End If + Loop Until (nUglyCount = varInputNum) + + strMsg = "The " & varInputNum & "th ugly number is: " & nCount + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub diff --git a/challenge-123/eric-cheung/excel-vba/ch-2.bas b/challenge-123/eric-cheung/excel-vba/ch-2.bas new file mode 100644 index 0000000000..062714c37e --- /dev/null +++ b/challenge-123/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,88 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit + +Function DistSq _ +( _ + nX_01 As Double, _ + nY_01 As Double, _ + nX_02 As Double, _ + nY_02 As Double _ +) As Double + DistSq = (nX_01 - nX_02) ^ 2 + (nY_01 - nY_02) ^ 2 +End Function + +Function IsRightAngle _ +( _ + nX_01 As Double, _ + nY_01 As Double, _ + nX_02 As Double, _ + nY_02 As Double, _ + nX_03 As Double, _ + nY_03 As Double _ +) As Boolean + + IsRightAngle = False + + Dim m_01 As Double, m_02 As Double + + If nX_02 = nX_03 Then + If nY_01 = nY_02 Then + IsRightAngle = True + End If + Exit Function + End If + + If nX_01 = nX_02 Then + If nY_02 = nY_03 Then + IsRightAngle = True + End If + Exit Function + End If + + m_01 = (nY_01 - nY_02) / (nX_01 - nX_02) + m_02 = (nY_02 - nY_03) / (nX_02 - nX_03) + + If m_01 * m_02 = -1 Then + IsRightAngle = True + Exit Function + End If + +End Function + +Sub Task_02() + + Const nNumPt As Integer = 4 + Dim nX_Pt(1 To nNumPt) As Double + Dim nY_Pt(1 To nNumPt) As Double + + Dim bIsSq As Boolean + Dim strMsg As String + + bIsSq = False + +'' nX_Pt(1) = 10: nX_Pt(2) = 20: nX_Pt(3) = 20: nX_Pt(4) = 10 +'' nY_Pt(1) = 20: nY_Pt(2) = 20: nY_Pt(3) = 10: nY_Pt(4) = 10 + + nX_Pt(1) = 12: nX_Pt(2) = 16: nX_Pt(3) = 20: nX_Pt(4) = 18 + nY_Pt(1) = 24: nY_Pt(2) = 10: nY_Pt(3) = 12: nY_Pt(4) = 16 + + If _ + DistSq(nX_Pt(1), nX_Pt(2), nY_Pt(1), nY_Pt(2)) = DistSq(nX_Pt(2), nX_Pt(3), nY_Pt(2), nY_Pt(3)) _ + And DistSq(nX_Pt(2), nX_Pt(3), nY_Pt(2), nY_Pt(3)) = DistSq(nX_Pt(3), nX_Pt(4), nY_Pt(3), nY_Pt(4)) _ + And DistSq(nX_Pt(3), nX_Pt(4), nY_Pt(3), nY_Pt(4)) = DistSq(nX_Pt(4), nX_Pt(1), nY_Pt(4), nY_Pt(1)) _ + And IsRightAngle(nX_Pt(1), nY_Pt(1), nX_Pt(2), nY_Pt(2), nX_Pt(3), nY_Pt(3)) _ + And IsRightAngle(nX_Pt(2), nY_Pt(2), nX_Pt(3), nY_Pt(3), nX_Pt(4), nY_Pt(4)) _ + And IsRightAngle(nX_Pt(4), nY_Pt(4), nX_Pt(1), nY_Pt(1), nX_Pt(2), nY_Pt(2)) _ + Then + bIsSq = True + End If + + strMsg = "0 as the given coordinates doesn't form a square." + If bIsSq Then + strMsg = "1 as the given coordinates form a square." + End If + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub + -- cgit