16 Март 2011

Определить, является ли дробь периодической

Задачи |  Таги: , ,

Определить, является ли дробь периодической

Private Function RoundDigit(digit As Double) As Integer
If (digit - Int(digit) > 0) Then
    RoundDigit = Int(digit) + 1
Else
    RoundDigit = Int(digit)
End If
End Function

Private Function splitstr(ByVal str As String, ByVal lenght As Integer) As String()
Dim arr() As String
Dim s As String

ReDim arr(RoundDigit(Len(str) / lenght) - 1)
s = str
For i = LBound(arr) To UBound(arr)
    arr(i) = Left(s, lenght)
    s = Right(s, Len(s) - Len(arr(i)))
Next
splitstr = arr
End Function

Private Function GetPeriod(str As String) As String
Dim arr

a = Right(str, Len(str) - InStr(Replace(str, ",", "."), "."))

For i = 1 To Len(a) / 2
    flag = True
    arr = splitstr(a, i)
    For j = LBound(arr) To (UBound(arr) - 1)

        If arr(j) <> arr(j + 1) Then
           flag = False
           Exit For
        End If
    Next
        If flag = True Then
           GetPeriod = "(" & arr(0) & ")"
           Exit Function
        End If
Next
GetPeriod = "None"
End Function

Private Sub Command1_Click()
Select Case GetPeriod(Text1)
  Case Is = "None"
            Label1 = Replace(Text1, ",", ".")
  Case Else
            Label1 = Left(Text1, InStr(Replace(Text1, ",", "."), ".")) & GetPeriod(Text1)
End Select
End Sub


Оставить комментарий

Я не робот.