Question

In: Computer Science

File Compare Write a program that opens two text files and reads their contents into two...

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

Solutions

Expert Solution

//.........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!


Related Solutions

Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT