-1

如果给定的字符串是01111和文本是0111101111,位填充所需的位数是多少?

4

1 回答 1

0
#include <iostream>
#include <string>

int main() {
    // read bits '0'/'1' into a string; terminate on any other char
    std::string data_stream ;
    char bit ;
    while( std::cin >> bit && ( bit == '0' || bit == '1' ) ) data_stream += bit ;

    // stuff a zero bit after four consecutive bits of the same value.
    std::string stuffed_stream ;
    int cnt = 0 ;
    char bit_last_seen = 0 ;
    int stuff_bits_added = 0 ;

    for each bit in the data_stream
    {
        if( it is the same as the previous bit ) increment cnt

    else // it is a different bit 
    {
        bit_last_seen := this bit ;
        cnt := 1 ; // restart count at 1
    }

    stuffed_stream += bit ; // add the bit 

        if( cnt == 4 ) // there are four consecutive bits of the same value
        {
            stuffed_stream += '0' ; // stuff with a zero bit
            cnt := 0 ; // and reset cnt to zero
            ++stuff_bits_added ; // increment the count of stuff bits added
        }
    }

    // print out the results  
    std::cout << "     data stream: " << data_stream << '\n'
           << "  stuffed stream: " << stuffed_stream << '\n'
           << "stuff bits added: " << stuff_bits_added << '\n' ;


    // TODO:
    // The de-stuffing code to process the stuffed data to recreate the original 
    // and verify that the original data is recovered correctly.           
}

来自http://www.cplusplus.com/forum/general/98631/#msg533723的一些伪代码

现在你的问题似乎是什么?

于 2014-12-08T17:36:13.117 回答