Monday, April 20, 2015

C++ compare two different txt files

//============================================================================
// Name        : Dynqueue
// Author      : Zachary Standridge
// Version     :over a billion
// Copyright   : You wouldn't download a car would you?
// Description : opens and compare two different txt files.
//============================================================================#
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Dynqueue.h"

using namespace std;

int main()
{

   //creates an input file stream object

   fstream file1 ("file1.txt", ios::in);

   //creates another input file stream object

   fstream file2 ("file2.txt", ios::in);

   //creates two queues to hold characters.

    Dynque<char>queue1;
    Dynque<char>queue2;
    char ch1, ch2;

    //read all the characters from file 1 and puts them in queue 1.

    file1.get (ch1);
    while (!file1.eof())
    {
        queue1.enqueue(ch1);
        file1.get(ch1);
    }

    //read all the characters from file 2 and puts them in queue 2.

     file2.get (ch2);
    while (!file2.eof())
    {
        queue2.enqueue(ch2);
        file2.get(ch2);
    }

    //close

    file1.close();
    file2.close();

    //compare the queues

    while(!queue1.isEmpty()&& !queue2.isEmpty())
    {
    queue1.dequeue(ch1);
    queue2.dequeue(ch2);
    cout << ch1 << "\t"<< ch2 <<endl;
        if (ch1 != ch2)
       {
        cout << "\nThe files are not identical.\n";
        return 0;
       }
    }
        cout <<"\nThe files are identical. \n" << endl << endl;
        return 0;
}
----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------.h file------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
#ifndef DYNQUEUE_H_INCLUDED
#define DYNQUEUE_H_INCLUDED
#include <iostream>

using namespace std;
//stock template

template<class T>
class Dynque
{
    private:
        struct QueueNode
        {
            T value;
            QueueNode *next;
        };

        QueueNode *front;
        QueueNode *rear;
        int numItems;
    public:
        Dynque();
        ~Dynque();
        void enqueue (T);
        void dequeue (T &);
        bool isEmpty ();
        bool isFull ();
        void clear ();
};
//empty queue//
template<class T>
Dynque<T>::Dynque()
{
    front = NULL;
    rear = NULL;
    numItems = 0;
}
//deconstructor
template <class T>
Dynque<T>::~Dynque()
{
    clear();
}
//Inserts
template <class T>
void Dynque<T>::enqueue (T item )
{
    QueueNode*newNode;
    //new node
    newNode = new QueueNode;
    newNode->value = item;
    newNode->next = NULL;
    //adjust front rear
    if (isEmpty())
    {
        front = newNode;
        rear = newNode;
    }
    else
    {
        rear->next = newNode;
        rear = newNode;
    }
    //update
    numItems++;
}
template <class T>
void Dynque<T>::dequeue(T &item)
{
    QueueNode *temp;
    if (isEmpty())
    {
        cout << "The queue is empty.\n";
    }
    else
    {
        item = front -> value;
        temp = front;
        front = front->next;
        delete temp;
        //update
        numItems--;
    }
}
template<class T>
bool Dynque<T>::isEmpty()
{
    bool status;

    if (numItems >0)
        status = false;
    else
        status = true;
    return status;
}
template <class T>
void Dynque<T>::clear()
{
    T value;

    while (!isEmpty())
        dequeue(value);
}

#endif // DYNQUEUE_H_INCLUDED
---------------------------------------------------------------------------------------------------------------------
----------------------------------------------file1.txt---------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
The quick brown fox jumps over the lazy dog.

---------------------------------------------------------------------------------------------------------------------
----------------------------------------------file2.txt---------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
'I never forget a face, but in your case I’d be glad to make an exception.'




------------------------------------------------------------------------------------------------------------------------
create all files in the same folder, compile the cpp and enjoy

No comments:

Post a Comment