Advertisement
Guest User

Untitled

a guest
May 14th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Private Sub CommandButton1_Click()
  3.     Dim input, out As String
  4.     Dim k, n As Integer
  5.     Dim textArray() As String
  6.  
  7.     input = ReadFromFile("Текст1.txt")
  8.     n = Len(input)
  9.     ReDim textArray(n - 1)
  10.  
  11.     For k = 0 To n - 1
  12.         ' textArray(k) = Mid(a, k + 1, 1)
  13.        textArray(k) = input.charAt(k)
  14.         Next k
  15.    
  16.     If IsDigitsOnly(textArray) Then
  17.         WriteToFile("Text contains only digits")
  18.  
  19.     Else
  20.         If HasMoreLatins(textArray) Then
  21.             ReplaceLatins(textArray)
  22.         Else
  23.             ReplaceNonLatins(textArray)
  24.         End If
  25.    
  26.         For k = 0 To Len(input) - 1
  27.             out = out & textArray(k)
  28.         Next k
  29.    
  30.         Call WriteToFile("Текст2.txt", out)
  31.     End If
  32.    
  33. End Sub
  34.  
  35.  
  36. Private Function IsDigitsOnly(ByRef text() as String) as Boolean
  37.     For k = 0 To Len(text)
  38.         If Not (text(k) Like "[0-9]") Then
  39.             IsDigitsOnly = True
  40.         End If
  41.     Next k
  42.  
  43.     IsDigitsOnly = False
  44. End Function
  45.  
  46.  
  47. Private Function HasMoreLatins(ByRef text() As String) as Boolean
  48.     Dim currentChar as String
  49.     Dim latinCount, nonLatinCount, k as Integer
  50.  
  51.     latinCount = nonLatinCount = 0;
  52.     For k = 0 To Len(text) - 1
  53.         currentChar = text(k)
  54.         If currentChar Like "[A-Z]" Or currentChar Like "[a-z]" Then
  55.             latinCount = latinCount + 1
  56.  
  57.         ElseIf Not (currentChar Like " " Or currentChar Like ".") Then
  58.             nonLatinCount = nonLatinCount + 1
  59.  
  60.         End If
  61.     Next k
  62.     IsMoreLatins = (latinCount > nonLatinCount)
  63. End Function
  64.  
  65.  
  66. Private Sub ReplaceLatins(ByRef text() as String)
  67.     Dim k as Integer
  68.  
  69.     For k = 0 to Len(text) - 1
  70.         If Lcase(text(k)) Like "[a-z]" Then
  71.             text(k) = "4"
  72.         End If
  73.     Next k
  74. End Sub
  75.  
  76.  
  77. Private Sub ReplaceNonLatins(ByRef text() as String)
  78.     Dim k as Integer
  79.  
  80.     For k = 0 to Len(text) - 1
  81.         If Lcase(text(k)) Like "[!a-z .]" Then
  82.             text(k) = "4"
  83.         End If
  84.     Next k
  85. End Sub
  86.  
  87.  
  88. Private Function ReadFromFile(ByRef fileName As String) As String
  89.     Dim fso
  90.     Dim file
  91.     Dim readedString as String
  92.  
  93.     Set fso = CreateObject("Scripting.FileSystemObject")
  94.     Set file = fso.OpenTextFile(fileName, 1)
  95.     readedString = file.ReadAll
  96.     file.Close
  97.  
  98.     ReadFromFile = readedString
  99. End Function
  100.  
  101.  
  102. Private Sub WriteToFile(ByRef fileName As String, ByRef s As String)
  103.     Dim fso
  104.     Dim file
  105.  
  106.     Set fso = CreateObject("Scripting.FileSystemObject")
  107.     Set file = fso.CreateTextFile(fileName, True, True)
  108.  
  109.     file.WriteLine(s)
  110.     file.Close
  111. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement