Advertisement
Guest User

Untitled

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