In: Computer Science
File Compare
Write a program that opens two text files and reads their contents
into two separate
queues. The program should then determine whether the files are
identical by comparing
the characters in the queues. When two nonidentical characters are
encountered,
the program should display a message indicating that the files are
not the same. If both
queues contain the same set of characters, a message should be
displayed indicating
that the files are identical.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** ADT queue: Link-based implementation.
Listing 14-3.
@file LinkedQueue.h */
#ifndef _LINKED_QUEUE
#define _LINKED_QUEUE
#include "QueueInterface.h"
#include "Node.h"
#include "PrecondViolatedExcep.h"
template<class ItemType>
class LinkedQueue : public QueueInterface<ItemType>
{
private:
// The queue is implemented as a chain of linked nodes that has
// two external pointers, a head pointer for front of the queue and
// a tail pointer for the back of the queue.
Node<ItemType>* backPtr;
Node<ItemType>* frontPtr;
public:
LinkedQueue();
LinkedQueue (const LinkedQueue& aQueue);
~LinkedQueue();
bool isEmpty() const;
bool enqueue(const ItemType& newEntry);
bool dequeue();
/** @throw PrecondViolatedExcep if the queue is empty */
ItemType peekFront() const throw(PrecondViolatedExcep);
}; // end LinkedQueue
#include "LinkedQueue.cpp"
#endif
//.........DynamicQueue......................
//DynamicQueue.h(h means header.)
#ifndef DYNAMICQUEUE_H
#define DYNAMICQUEUE_H
#include <iostream>
#include <new> //for bad_alloc
using namespace std;
template<class T>
class DynamicQueue{
private:
//struct for Node
struct Node{
//to hold node value
T value;
//pointer to next Node
Node *next;
};
//pointer to front of queue
Node *frontNode;
Node *rearNode;
public:
//constructor
DynamicQueue(){
//make frontNode null pointer
//marking empty queue
frontNode = nullptr;
}
//enqueue function
void enqueue(T val){
try{
//pointer to a new node
//and create new node
Node *newNode = new Node;
//store input value in newNode
newNode->value = val;
//make newNode to be last node in queue
newNode->next = nullptr;
//first check if queue is empty
//because in this case newNode
//becomes first AND last node
if(isEmpty()){
//make front and rear to point
//to newNode thus making it
// first and last
frontNode = newNode;
rearNode = newNode;
}
//otherwise if queue not empty
else{
//link current rear to newNode
rearNode->next = newNode;
//update rear to point to new Node
rearNode = newNode;
}
} //try block ends here
//catch bad_alloc object
catch(bad_alloc){
cout << "Exception caught! Bad allocation!\n";
}
}
//dequeue function
void dequeue(T &var){
//first check if queue is empty
//do not proceed if so
if(isEmpty()){
cout << "Queue is empty! Cannot dequeue!\n";
}
//otherwise if queue not empty
else{
//save value of current front
//to argument variable
var = frontNode->value;
//create temp Node to point to current front
Node *temp = frontNode;
//make front point to next node in line
frontNode = temp->next;
//delete previous front, which is now
//pointed by temp
delete temp;
}
}
//function isEmpty
//determines whether queue is empty or not
bool isEmpty(){
//check if front points to null
if(frontNode == nullptr)
//if so it means queue is empty
//thus return true
return true;
//otherwise return false
return false;
}
//destructor
~DynamicQueue(){
//dummy variable
T dummy;
//dequeue until queue is empty
while(!isEmpty()){
dequeue(dummy);
}
}
};
#endif
//.........Filecompare.............................
//filecompare.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include "DynamicQueue.h"
using namespace std;
int main()
{
fstream input1, input2;
input1.open("input1.txt", ios::in);
input2.open("input2.txt", ios::in);
//create two Dynamic queue objects of chars
DynamicQueue<char> charQueue1;
DynamicQueue<char> charQueue2;
char ch1, ch2;
//read input files char by char
//until no more chars
cout << "Now reading from file1:\n";
while(input1.get(ch1)){
cout << ch1;
//for each iteration, enqueue char
charQueue1.enqueue(ch1);
}
cout << "\n\nNow reading from file2:\n";
while(input2.get(ch2)){
cout << ch2;
//for each iteration, enqueue char
charQueue2.enqueue(ch2);
}
//bool variable to hold status of comparison
bool status = true;
//dequeue chars one by one from queues
//until either one is empty or until two
//different chars are different
cout << "\n\nNow comparing...\n";
while(!charQueue1.isEmpty() && !charQueue2.isEmpty()
&& status){
//pop chars from queues
charQueue1.dequeue(ch1);
charQueue2.dequeue(ch2);
//compare the two
if(ch1 != ch2)
status = false;
}
//print final message
if(status && charQueue1.isEmpty() &&
charQueue2.isEmpty())
cout << "Files are identical!\n";
else
cout << "Files are not the same!\n";
//return 0 to mark successful termination
return 0;
}
//input1.txt
hii good morning .
//input2.txt
hii good evng..
Output:
Now comparing...
Files are not the same!