elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Curso de javascript por TickTack


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  [Reto] IsHour
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] 2 Ir Abajo Respuesta Imprimir
Autor Tema: [Reto] IsHour  (Leído 8,180 veces)
Sanlegas

Desconectado Desconectado

Mensajes: 131


https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-


Ver Perfil
[Reto] IsHour
« en: 15 Septiembre 2011, 04:16 am »

Bueno para seguir practicando y calentando, ahora por que no un reto para saber si una expresión es una hora  :laugh:, creo que es mas facil pero bueno  :P
la fecha limite es el 18/09/2011 y como no hay una funcion de vb que haga lo mismo (corregirme si me equivoco) se hara en lo mas logico, que acepte el siguiente formato  HH:MM:SS  (horas,minutos,segundos) y las siguientes reglas

* Debe aceptar de "00:00:00" hasta "23:59:59"
* En base a lo anterior y para hacerlo mas interesante lo correcto es llenar los dos lugares, si es menor que 10 se pondra un cero, ejemplo:
"05:59:59" ----> Correcto
"5:59:59" ---- > Falso
* Debe devolver True si la hora es correcta
* El delimitador para separar los numeros es ":"


Buena suerte a todos.. un saludo !  :D


« Última modificación: 15 Septiembre 2011, 05:29 am por Tenient101 » En línea

x64core


Desconectado Desconectado

Mensajes: 1.908


Ver Perfil
Re: [Reto] IsHour
« Respuesta #1 en: 15 Septiembre 2011, 05:23 am »

creo tener la mia :xD

Código
  1. Function R100(XXX As String) As Boolean
  2. Dim X() As String
  3.  
  4. X = Split(XXX, ":")
  5. If (CInt(X(0)) < 24) And (CInt(X(1) < 60)) And (CInt(X(2) < 60)) Then R100 = True
  6. End Function
  7.  


En línea

Sanlegas

Desconectado Desconectado

Mensajes: 131


https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-


Ver Perfil
Re: [Reto] IsHour
« Respuesta #2 en: 15 Septiembre 2011, 05:30 am »

@Raul100:

R100("aa:aa:aa") --- > Fail

para hacerlo mas interesante acabo de agregar una nueva regla  :P
En línea

BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: [Reto] IsHour
« Respuesta #3 en: 15 Septiembre 2011, 07:03 am »

Es cuestion solo de modificar algunas cosillas de la funcion isDate. Por ejemplo aqui tienen la mia Donde solo Modifique la 2da funcion isDate de mi tutela. solo Modifique los rangos... y cambio el select case por un simple if then para verificar rangos...

Código
  1.  
  2. Option Explicit
  3.  
  4. Private Declare Sub RtlMoveMemory Lib "kernel32" (ByVal pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Long)
  5.  
  6. '   //  Formato aceptado   HH:MM:SS, H:/MM:S, HH:MM:S, etc!&#161;.
  7. Public Function isHour_BlackZX(ByRef sStr As String, Optional bExtrictic As Boolean = True) As Boolean
  8. Dim lChar           As Long
  9. Dim lVal            As Long
  10. Dim lConvert(2)     As Long
  11.  
  12. Dim lDim            As Long
  13. Dim lMult           As Long
  14. Dim pStr            As Long
  15. Dim pChar           As Long
  16.  
  17.    pStr = LenB(sStr)
  18.    If (bExtrictic) Then
  19.        If Not (pStr = &H10) Then Exit Function
  20.    ElseIf (pStr < &H5) And (pStr <= &H8) Then
  21.        Exit Function
  22.    End If
  23.  
  24.    pStr = StrPtr(sStr) + (pStr - &H2)
  25.    pChar = VarPtr(lChar)
  26.  
  27.    lDim = &H2
  28.    lMult = &H1
  29.    lConvert(lDim) = &H0
  30.  
  31.    Do Until StrPtr(sStr) > pStr
  32.        RtlMoveMemory pChar, pStr, &H2  '   //  Dos bytes = char...
  33.        lVal = (lChar And &HFF)
  34.        If (lVal = &H3A) Then
  35.            lDim = (lDim - &H1)
  36.            If ((lDim And &H80000000) = &H80000000) Then Exit Function
  37.            lMult = &H1
  38.        Else
  39.            If ((lVal > &H39) Or (lVal < &H30)) Then Exit Function
  40.            lConvert(lDim) = lConvert(lDim) + ((lVal - &H30) * lMult)
  41.            lMult = (lMult * &HA)
  42.        End If
  43.        pStr = (pStr - &H2)
  44.    Loop
  45.  
  46.    If (lConvert(&H0) > &H17) Or (lConvert(&H1) > &H3B) Or (lConvert(&H2) > &H3B) Then Exit Function
  47.  
  48.    isHour_BlackZX = True
  49.  
  50. End Function
  51.  
  52. Private Sub Form_Load()
  53.    MsgBox isHour_BlackZX("23:59:9") & vbCrLf & _
  54.           isHour_BlackZX("23:59:9", False)
  55. End Sub
  56.  
  57.  

2da version, leyendo 32bits (la anterior es a 16bits pero es mas legible), esta un tanto ofuscada...

Código:

Option Explicit
 
Private Declare Sub RtlMoveMemory Lib "kernel32" (ByVal pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Long)

Public Function addAscToNumber(ByVal lAsc As Long, ByRef rArrConvert() As Long, ByRef lIndex As Long, ByRef lMult As Long) As Long
    addAscToNumber = (-1)
    If (lAsc = &H3A) Then
        lMult = &H1
        lIndex = (lIndex + &H1)
    Else
        If (lAsc = &H0) Then addAscToNumber = &H1: Exit Function
        If ((lAsc > &H39) Or (lAsc < &H30)) Then addAscToNumber = &H0: Exit Function
        rArrConvert(lIndex) = (rArrConvert(lIndex) * lMult) + (lAsc - &H30)
        lMult = (lMult * &HA)
    End If
End Function

'   //  Formato aceptado   HH:MM:SS, H:/MM:S, HH:MM:S, etc!¡.
Public Function isHour_BlackZX(ByRef sStr As String, Optional bExtrictic As Boolean = True) As Boolean
Dim lChar           As Long
Dim lConvert(2)     As Long
Dim lIndex          As Long
Dim lMult           As Long
Dim lStrLnB         As Long
Dim pStr            As Long
Dim pStrLim         As Long
Dim pChar           As Long
    lStrLnB = LenB(sStr)
    If (bExtrictic) Then
        If Not (lStrLnB = &H10) Then Exit Function
    ElseIf (lStrLnB < &H5) And (lStrLnB <= &H8) Then
        Exit Function
    End If
    pStr = StrPtr(sStr)
    pStrLim = (lStrLnB + pStr - &H2)
    pChar = VarPtr(lChar)
    lMult = &H1
    For pStr = pStr To pStrLim Step &H4
        RtlMoveMemory pChar, pStr, &H4  '   //  Cuatro bytes = 4 --> 2 Char...
        Select Case addAscToNumber((lChar And &HFF), lConvert, lIndex, lMult)
            Case (1): Exit For
            Case (0): Exit Function
        End Select
        If (lIndex > &H2) Then Exit Function
        Select Case addAscToNumber(((lChar And &HFF0000) / &H10000), lConvert, lIndex, lMult)
            Case (1): Exit For
            Case (0): Exit Function
        End Select
        If (lIndex > &H2) Then Exit Function
    Next
    If (lConvert(&H0) > &H17) Or (lConvert(&H1) > &H3B) Or (lConvert(&H2) > &H3B) Then Exit Function
    isHour_BlackZX = True
End Function

Private Sub Form_Load()
    MsgBox isHour_BlackZX("23:59:9") & vbCrLf & _
           isHour_BlackZX("23:59:9", False)
End Sub


Output:

Código:

Falso
Verdadero


Dulces Lunas!¡.
« Última modificación: 16 Septiembre 2011, 01:32 am por BlackZeroX▓▓▒▒░░ » En línea

The Dark Shadow is my passion.
Sanlegas

Desconectado Desconectado

Mensajes: 131


https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-


Ver Perfil
Re: [Reto] IsHour
« Respuesta #4 en: 15 Septiembre 2011, 07:27 am »

@BlackZeroX▓▓▒▒░░: si tienes razon, pero es por que tu función es mas generica... , y claro para resolver un problema puedes usar diferentes soluciones...
Por cierto:
isHour_BlackZX("14:59:09") --- > Fail

Salu2 !
En línea

BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: [Reto] IsHour
« Respuesta #5 en: 15 Septiembre 2011, 08:08 am »


isHour_BlackZX("14:59:09") --- > Fail


jaja sorry se me ofusco el LenB()...

Código
  1.  
  2. Dim lMult           As Long
  3. Dim pStr            As Long
  4. Dim pChar           As Long
  5.  
  6.    pStr = LenB(sStr)
  7.    If (bExtrictic) Then
  8.        If Not (pStr = &H8) Then Exit Function
  9.    ElseIf (pStr < &H5) And (pStr <= &H8) Then
  10.        Exit Function
  11.    End If
  12.  
  13.  

El valor &H8 deberia ser &H10 ya lo corregi, gracias por el aviso.

Edito:

Valen espacios en blanco? digo para meterle algo para que no diga False en casos como "23:59:59 " y " 23:59:59" o ambos casos " 23:59:59 ".

Dulces Lunas!¡.
« Última modificación: 15 Septiembre 2011, 08:10 am por BlackZeroX▓▓▒▒░░ » En línea

The Dark Shadow is my passion.
Psyke1
Wiki

Desconectado Desconectado

Mensajes: 1.089



Ver Perfil WWW
Re: [Reto] IsHour
« Respuesta #6 en: 15 Septiembre 2011, 11:05 am »

Ui que interesante, se me ocurren muchas formas de hacerlo, podré todas las que se me ocurran, con RegExp sería sencillísimo:
Código:
^([01]\d|2[0-3])\:[0-5]\d\:[0-5]\d$
(Después lo desarollo antes de que se me adelante raul338) :silbar: :xD

Mi primera forma de hacerlo (dudo que sea la más rápida, pero igual si la más corta):
Código
  1. Option Explicit
  2.  
  3. Public Static Function IsHour_Psyke1(ByRef sHour$) As Boolean
  4. On Error Resume Next
  5.    IsHour_Psyke1 = TimeValue(sHour) And (LenB(sHour) = 16)
  6. End Function

Test:
Código
  1. Private Sub Form_Load()
  2.    Debug.Print IsHour_Psyke1("12:13:12")
  3.    Debug.Print IsHour_Psyke1("24:13:12")
  4. End Sub

Resultado:
Código:
True
False

DoEvents! :P
« Última modificación: 16 Septiembre 2011, 00:55 am por Psyke1 » En línea

Psyke1
Wiki

Desconectado Desconectado

Mensajes: 1.089



Ver Perfil WWW
Re: [Reto] IsHour
« Respuesta #7 en: 15 Septiembre 2011, 12:49 pm »

Mi segunda forma de hacerlo (sé que se puede simplificar código con bucles, pero yo lo elijo hacer así  :rolleyes:)


Código
  1. '//En un módulo de clase.
  2.  
  3. Option Explicit
  4. Private Declare Sub PutMem4 Lib "msvbvm60.dll" (ByVal Ptr&, ByVal Value&)
  5. Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long
  6.  
  7. Private lngAscHeader&(5), intAsc%()
  8.  
  9. Public Static Function IsHour_Psyke12(ByRef sHour$) As Boolean
  10.    If LenB(sHour) = 16 Then
  11.        lngAscHeader(3) = StrPtr(sHour)
  12.  
  13.        '//Check ":"
  14.        If intAsc(2) = 58 And intAsc(5) = 58 Then
  15.            '//Hours
  16.            If intAsc(0) < 48 Or intAsc(0) > 50 Then Exit Function
  17.  
  18.            If intAsc(1) < 48 Then Exit Function
  19.            If intAsc(0) = 50 Then
  20.                If intAsc(1) > 51 Then Exit Function
  21.            Else
  22.                If intAsc(1) > 57 Then Exit Function
  23.            End If
  24.  
  25.            '//Minutes
  26.            If intAsc(3) < 48 Or intAsc(3) > 53 Then Exit Function
  27.            If intAsc(4) < 48 Or intAsc(4) > 57 Then Exit Function
  28.  
  29.            '//Seconds
  30.            If intAsc(6) < 48 Or intAsc(6) > 53 Then Exit Function
  31.            If intAsc(7) < 48 Or intAsc(7) > 57 Then Exit Function
  32.  
  33.            IsHour_Psyke12 = True
  34.        End If
  35.    End If
  36. End Function
  37.  
  38. Private Sub Class_Initialize()
  39.    lngAscHeader(0) = 1
  40.    lngAscHeader(1) = 2
  41.    lngAscHeader(4) = &H7FFFFFFF
  42.    PutMem4 VarPtrArray(intAsc), VarPtr(lngAscHeader(0))
  43. End Sub
  44.  
  45. Private Sub Class_Terminate()
  46.    PutMem4 VarPtrArray(intAsc), 0
  47. End Sub

Test:
Código
  1. Option Explicit
  2.  
  3. Private Sub Form_Load()
  4. Dim c As New Class1
  5.  
  6.    Debug.Print String$(50, "=")
  7.    Debug.Print c.IsHour_Psyke12(Time$)      'True
  8.    Debug.Print c.IsHour_Psyke12("23:59:59") 'True
  9.    Debug.Print c.IsHour_Psyke12("00:00:00") 'True
  10.    Debug.Print c.IsHour_Psyke12("34:54:13") 'False
  11.    Debug.Print c.IsHour_Psyke12("14:64:24") 'False
  12.    Debug.Print c.IsHour_Psyke12("22:07:70") 'False
  13.  
  14. Set c = Nothing
  15. End Sub

Resultados:
Código:
==================================================
True
True
True
False
False
False


EDIT:

Mi tercera forma de hacerlo (no creo que sea muy rápida, tan solo doy más opciones...  ;) ):
Código
  1. '//En un módulo de clase.
  2.  
  3. Option Explicit
  4. Private oRegExp As Object
  5.  
  6. Public Static Function IsHour_Psyke13(ByRef sHour$) As Boolean
  7.    IsHour_Psyke13 = oRegExp.Test(sHour)
  8. End Function
  9.  
  10. Private Sub Class_Initialize()
  11.    Set oRegExp = CreateObject("VBScript.RegExp")
  12.    With oRegExp
  13.        .Pattern = "^([01]\d|2[0-3])\:[0-5]\d\:[0-5]\d$"
  14.        .Global = True
  15.    End With
  16. End Sub
  17.  
  18. Private Sub Class_Terminate()
  19.    Set oRegExp = Nothing
  20. End Sub

Código
  1. Option Explicit
  2.  
  3. Private Sub Form_Load()
  4. Dim c As New Class1
  5.  
  6.    Debug.Print String$(50, "=")
  7.    Debug.Print c.IsHour_Psyke13(Time$)      'True
  8.    Debug.Print c.IsHour_Psyke13("23:59:59") 'True
  9.    Debug.Print c.IsHour_Psyke13("00:00:00") 'True
  10.    Debug.Print c.IsHour_Psyke13("34:54:13") 'False
  11.    Debug.Print c.IsHour_Psyke13("14:64:24") 'False
  12.    Debug.Print c.IsHour_Psyke13("22:07:70") 'False
  13.  
  14. Set c = Nothing
  15. End Sub

Resultados:
Código:
==================================================
True
True
True
False
False
False


EDIT2:

Mi cuarta forma de hacerlo:
Código
  1. Option Explicit
  2.  
  3. Public Static Function IsHour_Psyke14(ByRef sHour$) As Boolean
  4. Dim h As Byte, m As Byte, s As Byte
  5. On Error GoTo NoHour:
  6.    If LenB(sHour) = 16 Then
  7.        If InStrB(1, ":", MidB$(sHour, 5, 2), vbBinaryCompare) = 0 Then Exit Function
  8.        If InStrB(1, ":", MidB$(sHour, 11, 2), vbBinaryCompare) = 0 Then Exit Function
  9.  
  10.        h = LeftB$(sHour, 4) + 0
  11.        If h > 23 Then Exit Function
  12.        m = MidB$(sHour, 7, 4) + 0
  13.        If m > 59 Then Exit Function
  14.        s = RightB$(sHour, 4) + 0
  15.        If s > 59 Then Exit Function
  16.  
  17.        IsHour_Psyke14 = True
  18.    End If
  19.    Exit Function
  20. NoHour:
  21. End Function

Test:
Código
  1. Option Explicit
  2.  
  3. Private Sub Form_Load()
  4.    Debug.Print String$(50, "=")
  5.    Debug.Print IsHour_Psyke14(Time$)      'True
  6.    Debug.Print IsHour_Psyke14("23:59:59") 'True
  7.    Debug.Print IsHour_Psyke14("00:00:00") 'True
  8.    Debug.Print IsHour_Psyke14("34:54:13") 'False
  9.    Debug.Print IsHour_Psyke14("14:64:24") 'False
  10.    Debug.Print IsHour_Psyke14("22:04:70") 'False
  11. End Sub

Resultados:
Código:
==================================================
True
True
True
False
False
False

PD: Lo próximo que tenga que decir haré un comentario nuevo que sino hago una pág kilométrica. :-X :laugh:

DoEvents! :P
« Última modificación: 15 Septiembre 2011, 22:36 pm por Psyke1 » En línea

raul338


Desconectado Desconectado

Mensajes: 2.633


La sonrisa es la mejor forma de afrontar las cosas


Ver Perfil WWW
Re: [Reto] IsHour
« Respuesta #8 en: 15 Septiembre 2011, 15:02 pm »

Buena idea con el reto :D yo me olvide que iba a proponer de reto y después se me paso :xD

Hice lo mismo que BlackZeroX, agarre mi IsDate y lo transforme a IsHour!! :xD
Ademas de que lo optimize (y puedo optimizar tambien el IsDate, pero ya fue xD)
Código
  1. Public Function IsHour_r338(str As String) As Boolean
  2. If str = vbNullString Then Exit Function
  3. If LenB(str) <> 16 Then Exit Function
  4.  
  5.    Dim j As Long, k As Long, vTemp As Byte, jp As Long
  6.    Dim strp As Long
  7.    strp = StrPtr(str)
  8.    jp = VarPtr(j)
  9.    For k = 0 To 14 Step 2
  10.        Call RtlMoveMemory(jp, strp + k, 1)
  11.        Select Case k
  12.            Case 0
  13.                If j < 48 And j > 50 Then Exit Function
  14.                vTemp = (j - 48) * 10
  15.            Case 2
  16.                If j < 48 And j > 57 Then Exit Function
  17.                vTemp = vTemp + (j - 48)
  18.                If vTemp > 23 Then Exit Function
  19.            Case 4, 10: If j <> 58 Then Exit Function
  20.            Case 6, 12
  21.                If j < 48 And j > 53 Then Exit Function
  22.                vTemp = (j - 48) * 10
  23.            Case 8, 14
  24.                If j < 48 And j > 57 Then Exit Function
  25.                vTemp = vTemp + (j - 48)
  26.                If vTemp > 59 Then Exit Function
  27.        End Select
  28.    Next
  29.    IsHour_r338 = True
  30. End Function
  31.  
« Última modificación: 15 Septiembre 2011, 15:04 pm por raul338 » En línea

BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: [Reto] IsHour
« Respuesta #9 en: 15 Septiembre 2011, 19:53 pm »

.
No me agradan los BadTypeConvert/EvilTypeConvert que hace Psyke1 aun que en fin de cuentas funciona pero no me agradan...  :¬¬

Dulces Lunas!¡.
En línea

The Dark Shadow is my passion.
Páginas: [1] 2 Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Reto ;)
Ingeniería Inversa
NeoKiller 3 2,912 Último mensaje 15 Agosto 2004, 23:12 pm
por NeoKiller
Reto!!
Ingeniería Inversa
HaCkZaTaN 2 2,962 Último mensaje 10 Septiembre 2004, 09:30 am
por Ðevastador
Reto vB
Ingeniería Inversa
nhouse 2 3,525 Último mensaje 16 Marzo 2005, 09:41 am
por 4rS3NI(
reto en VB6
Ingeniería Inversa
ellolo 1 2,644 Último mensaje 15 Abril 2005, 10:03 am
por UnpaCker!
Un reto !!! « 1 2 3 »
Programación Visual Basic
VirucKingX 24 8,506 Último mensaje 8 Mayo 2006, 23:36 pm
por Kizar
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines