Diff C ++ Algorithm

I am trying to create a C ++ program that can distinguish between two .txt files.

struct line
{
    string text;
    size_t num;
    int status;
};

void compareFiles(vector<line> &buffer_1, vector<line> &buffer_2, size_t index_1, size_t index_2)
{
    while(index_1 < buffer_1.size())
    {
         while(index_2 < buffer_2.size())
         {  
             X = buffer_1[index_1].text;
             Y = buffer_2[index_2].text;
             if(X == Y)
             {
                 ++index_1;
                 ++index_2;
             }
             else
             {
                 LCS();
                 string lcs = printLCS(X.length(), Y.length());

                 /*
                 * Here my problem
                 */

             }
         }
     }
 }

As you can see, I have two buffers (line vectors) previously loaded with the contents of the files. I also have an LCS algorithm, fully functional (tested). LCS works with strings X and Y globally.

So, I really need to compare line-by-line buffers with LCS, but I was not able to do this.

Could you help me?

+3
source share
2 answers

, , . , . , , ftp://mirrors.kernel.org/gnu/diffutils/diffutils-3.0.tar.gz .

, src/analysis.c. diff_2_files 472. , , 512 - 537. :

for (;; cmp->file[0].buffered = cmp->file[1].buffered = 0)
{
    /* Read a buffer worth from both files.  */
    for (f = 0; f < 2; f++)
        if (0 <= cmp->file[f].desc)
            file_block_read (&cmp->file[f],
                buffer_size - cmp->file[f].buffered);

    /* If the buffers differ, the files differ.  */
    if (cmp->file[0].buffered != cmp->file[1].buffered
            || memcmp (cmp->file[0].buffer,
                    cmp->file[1].buffer,
                    cmp->file[0].buffered))
    {
        changes = 1;
        break;
    }

    /* If we reach end of file, the files are the same.  */
    if (cmp->file[0].buffered != buffer_size)
    {
        changes = 0;
        break;
    }
}

, . , memcmp, , - . - , . , , , .

+6

, LCS(), - ​​ , std::string LCS(const line& lhs, const line& rhs). while .

for(int i = 0; i < buffer_1.size(); ++i)
{
    for(int j = 0; j < buffer_2.size(); ++j)
    {  
        std::string lcs = LCS(buffer_1[i].text, buffer_2[j].text);
        std::cout << "LCS[" << i << "][" << j << "]: " << lcs << std::endl;
    }
}

buffer_1 buffer_2. ? ?

0

All Articles