RootsLabs

More than a tool ! GitHub Google+ LinkedIn RSS

Encoder une chaine en Morse

Progi1984 - Commentaires (0)

Et voilà, après lecture de cet article, j’ai voulu transformer ce code en PureBasic pour gérer aussi le morse.

;@link http://j-reaux.developpez.com/tutoriel/asp/alphabet-radio-code-morse/index.php
EnableExplicit
;{ Alphabet Morse
  ;- Structures
  Structure S_Morse
    car_normal.s
    car_morse.s
  EndStructure
  ;- Constantes
  #Dim_Morse_Size = 55
  #Morse_Separateur = "|"
  ;-Tableaux
  Global Dim Dim_Morse.S_Morse(#Dim_Morse_Size+1)
  ;- Datasections
  DataSection
    Char_Normal:
    Data.s "0","1","2","3","4","5","6","7","8","9"
    Data.s "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
    Data.s " ",".",",","?","'","!","/","(",")","&",":",";","=","+","-","_",Chr(34),"$","@","EUR"
    Char_Morse:
    Data.s "----",".----","..---","...--","....-","....","-....","--...","---..", "----."
    Data.s ".-","-...","-.-.","-..",".","..-..","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."
    Data.s "|",".-.-.-","--..--","..--..",".----.","-.-.-----.","-..-.","-.--.","-.--.-",".-...","---...","-.-.-.","-...-",".-.-.","-....-","..--.-",".-..-.","...-..-",".--.-.","...-.-.---"
  EndDataSection
  ;-Fonctions
  Procedure.s RemoveAccent(sString.s)
    sString = ReplaceString(sString, "Á", "A")
    sString = ReplaceString(sString, "Â", "A")
    sString = ReplaceString(sString, "Ã", "A")
    sString = ReplaceString(sString, "Ä", "A")
    sString = ReplaceString(sString, "Å", "A")
    sString = ReplaceString(sString, "Ç", "C")
    sString = ReplaceString(sString, "È", "E")
    sString = ReplaceString(sString, "É", "E")
    sString = ReplaceString(sString, "Ê", "E")
    sString = ReplaceString(sString, "Ë", "E")
    sString = ReplaceString(sString, "Ì", "I")
    sString = ReplaceString(sString, "Í", "I")
    sString = ReplaceString(sString, "Î", "I")
    sString = ReplaceString(sString, "Ï", "I")
    sString = ReplaceString(sString, "Ò", "O")
    sString = ReplaceString(sString, "Ó", "O")
    sString = ReplaceString(sString, "Ô", "O")
    sString = ReplaceString(sString, "Õ", "O")
    sString = ReplaceString(sString, "Ö", "O")
    sString = ReplaceString(sString, "Ù", "U")
    sString = ReplaceString(sString, "Ú", "U")
    sString = ReplaceString(sString, "Û", "U")
    sString = ReplaceString(sString, "Ü", "U")
    sString = ReplaceString(sString, "Ý", "Y")
    sString = ReplaceString(sString, "à", "a")
    sString = ReplaceString(sString, "á", "a")
    sString = ReplaceString(sString, "â", "a")
    sString = ReplaceString(sString, "ã", "a")
    sString = ReplaceString(sString, "ä", "a")
    sString = ReplaceString(sString, "å", "a")
    sString = ReplaceString(sString, "ç", "c")
    sString = ReplaceString(sString, "è", "e")
    sString = ReplaceString(sString, "é", "e")
    sString = ReplaceString(sString, "ê", "e")
    sString = ReplaceString(sString, "ë", "e")
    sString = ReplaceString(sString, "ì", "i")
    sString = ReplaceString(sString, "í", "i")
    sString = ReplaceString(sString, "î", "i")
    sString = ReplaceString(sString, "ï", "i")
    sString = ReplaceString(sString, "ð", "o")
    sString = ReplaceString(sString, "ò", "o")
    sString = ReplaceString(sString, "ó", "o")
    sString = ReplaceString(sString, "ô", "o")
    sString = ReplaceString(sString, "õ", "o")
    sString = ReplaceString(sString, "ö", "o")
    sString = ReplaceString(sString, "ù", "u")
    sString = ReplaceString(sString, "ú", "u")
    sString = ReplaceString(sString, "û", "u")
    sString = ReplaceString(sString, "ü", "u")
    sString = ReplaceString(sString, "ý", "y")
    sString = ReplaceString(sString, "ÿ", "y")
    ProcedureReturn sString
  EndProcedure
  ProcedureDLL Init_Morse()
    Protected Inc.l
    Restore Char_Normal
    For Inc = 0 To #Dim_Morse_Size
        Read.s Dim_Morse(Inc)\car_normal
    Next
    Restore Char_Morse
    For Inc = 0 To #Dim_Morse_Size
      Read.s Dim_Morse(Inc)\car_Morse
    Next
  EndProcedure
  ProcedureDLL.s Encode_Morse(sString.s)
    Protected sStringEncoded.s, sChar.s
    Protected lInc.l, lIncA.l
    sString = RemoveAccent(sString)
    sStringEncoded = ""
    For lInc = 0 To Len(sString) -1
      sChar = Mid(sString, lInc + 1, 1)
      For lIncA = 0 To #Dim_Morse_Size
        If Dim_Morse(lIncA)\car_normal = UCase(sChar)
          sStringEncoded + Dim_Morse(lIncA)\car_Morse + #Morse_Separateur
          Break
        EndIf
      Next
    Next
    ProcedureReturn sStringEncoded
  EndProcedure
  ProcedureDLL.s Decode_Morse(sString.s)
    Protected lInc.l, lIncA.l
    Protected sStringDecoded.s, sChar.s
    sString = ReplaceString(sString, #Morse_Separateur+#Morse_Separateur+#Morse_Separateur, #Morse_Separateur+" "+#Morse_Separateur)
    For lInc = 1 To CountString(sString, #Morse_Separateur)
      sChar = StringField(sString, lInc, #Morse_Separateur)
      If sChar <> " "
        For lIncA = 0 To #Dim_Morse_Size
          If Dim_Morse(lIncA)\car_Morse = sChar
            sStringDecoded + Dim_Morse(lIncA)\car_normal
            Break
          EndIf
        Next
      Else
        sStringDecoded + sChar
      EndIf
    Next
    ProcedureReturn sStringDecoded
  EndProcedure
;}

Define.s sInitial, sEncoded.s, sDecoded.s

Init_Morse()
sInitial = "C'est l'idée à 2EUR !"
sEncoded.s = Encode_Morse(sInitial)
sDecoded = Decode_Morse(sEncoded)

Debug sInitial
Debug sEncoded
Debug sDecoded

Ajouter un commentaire

Commentaire :