运行长度编码 - VB.NET

时间:2018-06-10 23:27:16

标签: vb.net run-length-encoding

我只是想知道人们是否可以帮助我理解代码的某些方面。我提前感谢你,因为这将帮助我更好地理解RLE如何工作以及如何结合循环。

Sub Main()
    Dim Text As String
    Dim LastChar As String
    Dim CountOfLastChar As Integer
    Console.Write("Enter the text to compress: ")
    Text = Console.ReadLine()
    Console.Write("The compressed text is: ")
    LastChar = ""
    CountOfLastChar = 0
    For Count = 0 To Len(Text) - 1

这条线和以下我发现很难遵循。计数是否给出了文本长度的值,但是 - 1?

        If Text(Count) = LastChar Then
            CountOfLastChar += 1
        Else
            If LastChar <> "" Then
                Console.Write(LastChar & " " & CountOfLastChar & " ")

我认为这是为了确保如果没有输入任何东西,它就能够处理它。

            End If
            LastChar = Text(Count)
            CountOfLastChar = 1

为什么CountOfLastChar需要= 1?

        End If
    Next
    Console.Write(LastChar & " " & CountOfLastChar & " ")
    Console.ReadLine()
End Sub

2 个答案:

答案 0 :(得分:0)

我对RLE一无所知,但我可以解释一下循环。在vb.net中,For ... Next循环将以Count = 0开始并在每次迭代时递增1,直到并包括(Text.Lenght -1)您可以更改金额 通过添加步骤2(或-5或其他)增加或减少的数量 它以这种方式完成,因为字符串中的第一个字符位于零位置。 新代码使用Len(Text)中的VB6遗留了Text.Length 注意:它与Nothing vs String.Empty

的行为不同
For Count = 0 To Len(Text) - 1
'When you add an indexer to a string you get the character at that index
        If Text(Count) = LastChar Then

答案 1 :(得分:0)

正确的代码对齐确实可以帮到你。

我在代码中添加了注释来解释每个细节,下面将您的具体要点称为#1,#2和#3。

Sub Main()
    Dim Text As String
    Dim LastChar As String
    Dim CountOfLastChar As Integer
    Console.Write("Enter the text to compress: ")
    Text = Console.ReadLine()
    Console.Write("The compressed text is: ")
    LastChar = ""
    CountOfLastChar = 0
    ' #1: Loop until the end of the string.
    For Count = 0 To Len(Text) - 1
        ' increase count if the current character equals the character to check.
        If Text(Count) = LastChar Then
            CountOfLastChar += 1
        Else
            ' only execute this step if it's not the start of the loop.
            If LastChar <> "" Then
                ' #2: Display the found amount of the previous character.
                Console.Write(LastChar & " " & CountOfLastChar & " ")
            End If
            ' Set new character to find the amount of.
            LastChar = Text(Count)
            ' #3: Set counter of the new encountered character to 1.
            CountOfLastChar = 1
        End If
    Next
    ' Since loop ended without that final 'display found amount' step
    ' (since no new character was encountered after the last one, obviously)
    ' execute that final step and display the repeat of the last found char.
    ' This should actually also have the [If LastChar <> ""] check to work with empty strings.
    Console.Write(LastChar & " " & CountOfLastChar & " ")
    Console.ReadLine()
End Sub

特别是你提到的三点:

  1. VB中的循环从一个值变为另一个值,包括结束值。但是,字符串内的字符索引仅比字符串长度少一个值(例如,长度为&#39; 2&#39;只有索引&#的字符串) 39; 0&#39;和&#39; 1&#39;),因此从该长度中减去一个。

    在下一行,Text(Count)实际上是数组操作; Text被视为一个字符数组,索引Count处的字符取自它,并与当前正在计算的LastChar进行比较。

  2. 这是循环检测到字符串中的当前字符与到目前为止计数的字符不同的点。它在这里做的是打印出前一个字符及其被发现的次数。
  3. 同样,当找到新字符时执行此代码。新字符存储在LastChar中。显然,既然你实际上是在处理一个角色,那么这个角色已经有一定数量的&#39; 1&#39;那里。然后循环将确定是否需要将更多内容添加到该数量。
  4. 只需在程序开始时设置一个断点,然后在调试模式下循环它;你会看到它是如何运作的。这是一个简单的例子:

      

    输入要压缩的文本:mooncell

         

    压缩文本为:m 1 o 2 n 1 c 1 e 1 l 2

    这个功能显然只是这个概念的演示。它实际上并没有压缩数据; &#34; m1o2n1c1e1l2&#34;比'月亮'和#34;长得多。 RLE通常不会在没有大量重复的情况下处理数据(例如,具有相同颜色的像素行的图像),并且存在特定的机制,例如重复指示符标记或命令/长度令牌,其允许跳过不可压缩的数据。 / p>

    这里有一篇非常好的文章:

    http://www.shikadi.net/moddingwiki/RLE_Compression

    虽然请注意,这主要来自数据压缩的观点,而不是 text 压缩。正如我在上面的示例中已经指出的那样,RLE对文本很少有用,因为语言通常不会包含两个以上连续的相同字符。