Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction Successive duplicated words are common in a draft text document. In this project you will write principled object-oriented C++ codes to read a text

Introduction

Successive duplicated words are common in a draft text document. In this project you will write principled object-oriented C++ codes to read a text file which contains duplicated words into a doubly-linked list, find duplicated texts and remove them from the linked list.

Objectives

You are given two text files (one with smaller size and the other a larger text file) containing duplicated words. Write the required codes to achieve the followings objectives utilizing the C++ STL container list and its associated iterator:

Create a linked list to store string objects by using the C++ Standard Template Library list container and iterator

Add entry at the back of the linked list

Print all words in the linked list

Print duplicated words in the linked list

Remove duplicated words in the linked list

Determine the number of words stored in the linked list

Determine the number of duplicated words in the linked list

Determine if the linked list is empty

Empty the linked list

The code

You are given skeleton code with many blank spaces. Your assignment is to fill in the missing parts so that the code is complete and works properly. The codes are in two files:

main.cpp (DO NOT CHANGE THIS FILE) contains the main function and a set of unit tests, implemented using assert statements - each assert statement compares the output of your code with the expected correct answer. Since the provided code is only a template, all the tests will fail immediately with runtime errors. As you add functionality and fix any bugs that may arise, the tests will be able to get further and further.

------main.cpp code----

// Project 2
// main.cpp
#include
#include
#include
#include
#include "Texttool.h"
using namespace std;
int i = 0; // for test step numbering.
#define SMALL_FILE_WORDS 36
#define SMALL_FILE_DUPS 6
#define LARGE_FILE_WORDS 1345
#define LARGE_FILE_DUPS 45
int main() {
try {
{
cout << "CPSC131 PROJECT 2" << endl << endl;
cout << "TESTING: BASIC LIST OPERATIONS" << endl << endl;
// constructor test
cout << "\t" << ++i << ". Testing: constructor and function totalWords()... ";
TextTool test;
assert(test.totalWords() == 0);
cout << "--> PASSED." << endl;
cout << "\t" << ++i << ". Testing: list insertion and function printAll()... ";
test.addEntryBack("This");
assert(test.totalWords() == 1);
test.addEntryBack("is");
assert(test.totalWords() == 2);
assert(test.listToString() == "This is ");
cout << "--> PASSED." << endl;
cout << "\t" << ++i << ". Testing: list erase... ";
test.makeEmpty();
assert(test.totalWords() == 0);
cout << "--> PASSED." << endl << endl;
}
{ // test full code with a small text file
cout << "TESTING: USING SMALL TEXT FILE:" << endl << endl;
cout << "\t" << ++i << ". Testing: verify number of words loaded... ";
TextTool test;
test.loadListFromTextfile("small_file.txt"); // load linked list with words from a text file
assert(test.totalWords() == SMALL_FILE_WORDS); // verify number of words loaded
cout << "--> PASSED." << endl << endl;
cout << "\t" << "Print the list:" << endl << endl;
cout << test.listToString() << endl << endl; // convert linked list to a string and then output
cout << "\t" << ++i << ". Testing: find number of duplicated words... ";
assert(test.totalDuplicates() == SMALL_FILE_DUPS); // determine number of duplicates in the linked list
cout << "--> PASSED." << endl;
test.printDuplicates(); // output the duplicates found
cout << "\t" << ++i << ". Testing: removal of duplicated words... ";
test.removeDuplicates(); // remove all duplicated words from the linked list
assert(test.totalDuplicates() == 0);
cout << "--> PASSED." << endl << endl;
cout << "\t" << "Print the file after removal of duplicated words:" << endl << endl;
cout << test.listToString() << endl << endl; // display the final linked list contents
cout << "FINISHED TESTING USING SMALL TEXT FILE." << endl << endl;
}
{ // test full code with a large text file
cout << "TESTING: USING LARGE TEXT FILE:" << endl << endl;
cout << "\t" << ++i << ". Testing: verify number of words loaded... ";
TextTool test;
test.loadListFromTextfile("large_file.txt"); // load linked list with words from a text file
assert(test.totalWords() == LARGE_FILE_WORDS); // verify number of words loaded
cout << "--> PASSED." << endl << endl;
cout << "\t" << "Print the list:" << endl << endl;
cout << test.listToString() << endl << endl; // convert linked list to a string and then output
cout << "\t" << ++i << ". Testing: find number of duplicated words... ";
assert(test.totalDuplicates() == LARGE_FILE_DUPS); // determine number of duplicates in the linked list
cout << "--> PASSED." << endl;
test.printDuplicates(); // output the duplicates found
cout << "\t" << ++i << ". Testing: removal of duplicated words... ";
test.removeDuplicates(); // remove all duplicated words from the linked list
assert(test.totalDuplicates() == 0);
cout << "--> PASSED." << endl << endl;
cout << "\t" << "Print the file after removal of duplicated words:" << endl << endl;
cout << test.listToString() << endl << endl; // display the final linked list contents
cout << "FINISHED TESTING USING LARGE TEXT FILE." << endl << endl;
}
}
catch (exception &e) {
cout << e.what() << endl;
}
// system("pause");
}

Texttool.h contains code for a TextTool data class. This class is incomplete and you should complete it. STL container list and its iterator should be used. Only one linked list is to be used in the implementation.

-----Texttool.h code-------

// Project 2 // Texttool.h

#pragma once

#include #include #include #include #include

using namespace std;

class TextTool { public: // default constructor TextTool() { // TO BE COMPLETED }

// destructor ~TextTool() { // TO BE COMPLETED }

// Load information from a text file with the given filename. void loadListFromTextfile(string filename) { ifstream ifs; ifs.open(filename);

if (ifs.is_open()) { string aword; while (ifs >> aword) { addEntryBack(aword); } ifs.close(); } else throw invalid_argument("Could not open file " + filename); }

// return the number of words in the linked list int totalWords() { return -1; // TO BE COMPLETED }

// add entry at the back of the linked list void addEntryBack(const string& aword) { // TO BE COMPLETED }

// print all words stored in the linked list, separated by a space const string listToString() { // TO BE COMPLETED }

// print duplicated words in the linked list void printDuplicates() { // TO BE COMPLETED }

// remove duplicated words in the linked void removeDuplicates() { // TO BE COMPLETED }

// determine the total number of duplicated words in the list int totalDuplicates() { // TO BE COMPLETED }

// check if the list is empty bool isEmpty() { // TO BE COMPLETED }

// Empty the list void makeEmpty() { // TO BE COMPLETED }

private:

list *wordlist; };

Assumptions

For simplicity we defined the followings:

Each word is defined as a group of characters which may contain any letters as well as punctuation marks.

Each word is separated by a white space.

There can be at most two consecutive occurrences of the same word in the provided text files.

Each word is stored as an element in each of the list nodes. The element in each node does not store the space character which separates words.

Hints

Texttool.h requires the use of an STL list data structure of string objects.

STL list iterator is used to facilitate iterating and navigating the list.

There is more than one way to solve this problem!

Expected Program Output Sample

CPSC131 PROJECT 2

TESTING: BASIC LIST OPERATIONS

1. Testing: constructor and function totalWords()... --> PASSED.

2. Testing: list insertion and function printAll()... --> PASSED.

3. Testing: list erase... --> PASSED.

TESTING: USING SMALL TEXT FILE:

4. Testing: verify number of words loaded... --> PASSED.

Print the list:

In In time past there lived lived a King King and Queen, who said to to each other every every day of their lives, "Would that that we had a child!" and yet they had none.

5. Testing: find number of duplicated words... --> PASSED.

duplicated word found: In

duplicated word found: lived

duplicated word found: King

duplicated word found: to

duplicated word found: every

duplicated word found: that

6. Testing: removal of duplicated words... --> PASSED.

Print the file after removal of duplicated words:

In time past there lived a King and Queen, who said to each other every day of their lives, "Would that we had a child!" and yet they had none.

FINISHED TESTING USING SMALL TEXT FILE.

TESTING: USING LARGE TEXT FILE:

7. Testing: verify number of words loaded... --> PASSED.

Print the list:

THE THE SLEEPING BEAUTY IN TIMES PAST there lived a King and Queen, who said to each other every day of their their lives, "Would that we had a child!" and yet they had none. But it happened once that when the Queen was bathing, there came a frog out of the the water, and he squatted on the ground, and said to her, "Thy wish shall be fulfilled; before a year has gone by, thou shalt bring bring a daughter into the world." And as the frog foretold, so it happened; and the Queen bore a daughter so beautiful that the King could not contain himself for joy, and he ordained a great feast. Not only did he bid to it his relations, friends, and acquaintances, but also the wise wise women, that they might be kind and favorable to the child. There There were thirteen of of them in his kingdom, but as he had only provided twelve golden golden plates for them them to eat from, one of them had to be left out. However, the feast was celebrated with all splendor; and as it drew to an end, the wise women stood stood forward to present to the child their wonderful gifts: one bestowed virtue, one beauty, a third riches, and so on, whatever there is in the world to wish for. And when eleven of them had said their say, in came the uninvited thirteenth, burning to revenge revenge herself, and without greeting or respect, she cried with a loud voice, "In the fifteenth year of her age the Princess shall prick herself with a spindle and shall fall down dead." And without speaking one more word she turned away and left the hall. Every one was terrified at her saying, when the twelfth came forward, for she had not yet bestowed her gift, and though she could not do away with the evil prophecy, yet she could soften it, so she said, "The Princess shall not die, but fall into a deep sleep for a hundred years." Now the King, being desirous desirous of saving his child even from this misfortune, gave commandment that all the spindles in his kingdom should be burnt up. The maiden grew grew up, adorned with all the gifts of the wise women; and she was so lovely, modest, sweet, and kind and clever, that no one who saw her her could help loving her. It happened one day, she she being already fifteen years old, that the King and Queen rode abroad; and the maiden was left behind alone in in the castle. She wandered about into all the nooks and corners, and into all the chambers and parlors, as the fancy took her, till till at last she she came to an old tower. She climbed the narrow winding stair which led to a little door, with a rusty key sticking out of the lock; she turned the key, and the door opened, and there in the little room sat an old old woman with a spindle, diligently spinning her flax. "Good day, mother," said the Princess, "what are you doing?" "I am spinning," answered the old woman, nodding her head. "What thing is that that twists round so briskly?" asked the maiden, and taking the spindle into her hand she began to spin; but no sooner sooner had she touched it than the evil prophecy was fulfilled, and she pricked her finger with it. In that that very moment she fell back upon the bed that stood there, and lay in a deep sleep, and this sleep fell upon the whole castle. The King and Queen, who had returned and were in the great hall fell fast asleep, and with them the whole court. The horses in their stalls, the dogs in the yard, the pigeons on the roof, the flies on the wall, the very fire that flickered on the hearth, became still, and slept like the rest; and the meat on the spit ceased roasting, and the cook, who was was going to pull the scullions hair for some mistake he had made, let him go, and went to sleep. And the wind ceased, and not a leaf fell from the trees about the castle. Then round about that place there grew a hedge of thorns thicker every year, until at at last the whole castle was hidden from view, and nothing nothing of it could be seen but the vane on the roof. And a rumor went abroad in all that country of the beautiful sleeping Rosamond, for so was the Princess called; and from time to time many Kings sons came and tried tried to force their way through the hedge; but it was impossible for them to do so, for the thorns held fast together like strong hands, and the young men were caught by them, and not being able to get free, there died a lamentable death. Many a long long year afterwards there came a Kings son into that country, and heard an old man tell how there should be a castle standing behind the hedge of thorns, and and that there a beautiful enchanted Princess named named Rosamond had slept for a hundred years, and with her the King and Queen, and the whole court. The old man had been told by his grandfather that many Kings sons sons had sought to pass the thorn-hedge, but had been caught and pierced by the thorns, and had died a miserable death. Then said the young man, "Nevertheless, I do not fear to try; I shall win through and see the lovely Rosamond." The good old man tried to dissuade him, but he would not listen to his words. For now the hundred years were at at an end, and the day had come when Rosamond should be awakened. When the Prince drew near the hedge of thorns, it was changed into a a hedge of beautiful large flowers, which parted and bent aside to let him pass, and then closed behind behind him in a thick hedge. When he reached the castleyard, he saw the horses and brindled hunting-dogs lying asleep, and and on the roof the pigeons were sitting with their heads under their wings. And when he came indoors, the flies on the wall were asleep, the cook in the kitchen had his hand uplifted to strike the scullion, and the kitchenmaid had the black fowl fowl on her lap ready to pluck. Then he mounted higher, and saw in in the hall the whole court lying asleep, and above them, on their thrones, slept the King and the Queen. And still he went farther, and all was so quiet that he could hear his his own breathing, and at last he came to the tower, and went up the winding stair, and opened the the door of the little room room where Rosamond lay. And when he saw her looking so lovely in her sleep, he could not turn away his eyes; and presently he stooped and kissed her, and she awaked, and opened her eyes, and looked very kindly on him. And she rose, and they went forth together, the King and the Queen and whole court waked up, and gazed on each other with great eyes of wonderment. And the horses in the yard got up and shook themselves, the hounds sprang up and wagged their tails, the pigeons on the roof drew their heads from under their their wings, looked round, and flew into the field, the flies on the wall crept on a little farther, the kitchen fire leapt up and and blazed, and cooked the meat, the joint on the spit began to roast, the cook gave the scullion such a box on the ear that he roared out, and the the maid went on plucking the fowl. Then the wedding of the Prince and Rosamond was held held with all splendor, and they lived very very happily together until their lives' end. THE END END

8. Testing: find number of duplicated words... --> PASSED.

duplicated word found: THE

duplicated word found: their

duplicated word found: the

duplicated word found: bring

duplicated word found: wise

duplicated word found: There

duplicated word found: of

duplicated word found: golden

duplicated word found: them

duplicated word found: stood

duplicated word found: revenge

duplicated word found: desirous

duplicated word found: grew

duplicated word found: her

duplicated word found: she

duplicated word found: in

duplicated word found: till

duplicated word found: she

duplicated word found: old

duplicated word found: that

duplicated word found: sooner

duplicated word found: that

duplicated word found: was

duplicated word found: at

duplicated word found: nothing

duplicated word found: tried

duplicated word found: long

duplicated word found: and

duplicated word found: named

duplicated word found: sons

duplicated word found: at

duplicated word found: a

duplicated word found: behind

duplicated word found: and

duplicated word found: fowl

duplicated word found: in

duplicated word found: his

duplicated word found: the

duplicated word found: room

duplicated word found: their

duplicated word found: and

duplicated word found: the

duplicated word found: held

duplicated word found: very

duplicated word found: END

9. Testing: removal of duplicated words... --> PASSED.

Print the file after removal of duplicated words:

THE SLEEPING BEAUTY IN TIMES PAST there lived a King and Queen, who said to each other every day of their lives, "Would that we had a child!" and yet they had none. But it happened once that when the Queen was bathing, there came a frog out of the water, and he squatted on the ground, and said to her, "Thy wish shall be fulfilled; before a year has gone by, thou shalt bring a daughter into the world." And as the frog foretold, so it happened; and the Queen bore a daughter so beautiful that the King could not contain himself for joy, and he ordained a great feast. Not only did he bid to it his relations, friends, and acquaintances, but also the wise women, that they might be kind and favorable to the child. There were thirteen of them in his kingdom, but as he had only provided twelve golden plates for them to eat from, one of them had to be left out. However, the feast was celebrated with all splendor; and as it drew to an end, the wise women stood forward to present to the child their wonderful gifts: one bestowed virtue, one beauty, a third riches, and so on, whatever there is in the world to wish for. And when eleven of them had said their say, in came the uninvited thirteenth, burning to revenge herself, and without greeting or respect, she cried with a loud voice, "In the fifteenth year of her age the Princess shall prick herself with a spindle and shall fall down dead." And without speaking one more word she turned away and left the hall. Every one was terrified at her saying, when the twelfth came forward, for she had not yet bestowed her gift, and though she could not do away with the evil prophecy, yet she could soften it, so she said, "The Princess shall not die, but fall into a deep sleep for a hundred years." Now the King, being desirous of saving his child even from this misfortune, gave commandment that all the spindles in his kingdom should be burnt up. The maiden grew up, adorned with all the gifts of the wise women; and she was so lovely, modest, sweet, and kind and clever, that no one who saw her could help loving her. It happened one day, she being already fifteen years old, that the King and Queen rode abroad; and the maiden was left behind alone in the castle. She wandered about into all the nooks and corners, and into all the chambers and parlors, as the fancy took her, till at last she came to an old tower. She climbed the narrow winding stair which led to a little door, with a rusty key sticking out of the lock; she turned the key, and the door opened, and there in the little room sat an old woman with a spindle, diligently spinning her flax. "Good day, mother," said the Princess, "what are you doing?" "I am spinning," answered the old woman, nodding her head. "What thing is that twists round so briskly?" asked the maiden, and taking the spindle into her hand she began to spin; but no sooner had she touched it than the evil prophecy was fulfilled, and she pricked her finger with it. In that very moment she fell back upon the bed that stood there, and lay in a deep sleep, and this sleep fell upon the whole castle. The King and Queen, who had returned and were in the great hall fell fast asleep, and with them the whole court. The horses in their stalls, the dogs in the yard, the pigeons on the roof, the flies on the wall, the very fire that flickered on the hearth, became still, and slept like the rest; and the meat on the spit ceased roasting, and the cook, who was going to pull the scullions hair for some mistake he had made, let him go, and went to sleep. And the wind ceased, and not a leaf fell from the trees about the castle. Then round about that place there grew a hedge of thorns thicker every year, until at last the whole castle was hidden from view, and nothing of it could be seen but the vane on the roof. And a rumor went abroad in all that country of the beautiful sleeping Rosamond, for so was the Princess called; and from time to time many Kings sons came and tried to force their way through the hedge; but it was impossible for them to do so, for the thorns held fast together like strong hands, and the young men were caught by them, and not being able to get free, there died a lamentable death. Many a long year afterwards there came a Kings son into that country, and heard an old man tell how there should be a castle standing behind the hedge of thorns, and that there a beautiful enchanted Princess named Rosamond had slept for a hundred years, and with her the King and Queen, and the whole court. The old man had been told by his grandfather that many Kings sons had sought to pass the thorn-hedge, but had been caught and pierced by the thorns, and had died a miserable death. Then said the young man, "Nevertheless, I do not fear to try; I shall win through and see the lovely Rosamond." The good old man tried to dissuade him, but he would not listen to his words. For now the hundred years were at an end, and the day had come when Rosamond should be awakened. When the Prince drew near the hedge of thorns, it was changed into a hedge of beautiful large flowers, which parted and bent aside to let him pass, and then closed behind him in a thick hedge. When he reached the castleyard, he saw the horses and brindled hunting-dogs lying asleep, and on the roof the pigeons were sitting with their heads under their wings. And when he came indoors, the flies on the wall were asleep, the cook in the kitchen had his hand uplifted to strike the scullion, and the kitchenmaid had the black fowl on her lap ready to pluck. Then he mounted higher, and saw in the hall the whole court lying asleep, and above them, on their thrones, slept the King and the Queen. And still he went farther, and all was so quiet that he could hear his own breathing, and at last he came to the tower, and went up the winding stair, and opened the door of the little room where Rosamond lay. And when he saw her looking so lovely in her sleep, he could not turn away his eyes; and presently he stooped and kissed her, and she awaked, and opened her eyes, and looked very kindly on him. And she rose, and they went forth together, the King and the Queen and whole court waked up, and gazed on each other with great eyes of wonderment. And the horses in the yard got up and shook themselves, the hounds sprang up and wagged their tails, the pigeons on the roof drew their heads from under their wings, looked round, and flew into the field, the flies on the wall crept on a little farther, the kitchen fire leapt up and blazed, and cooked the meat, the joint on the spit began to roast, the cook gave the scullion such a box on the ear that he roared out, and the maid went on plucking the fowl. Then the wedding of the Prince and Rosamond was held with all splendor, and they lived very happily together until their lives' end. THE END

FINISHED TESTING USING LARGE TEXT FILE.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications Dasfaa 2023 International Workshops Bdms 2023 Bdqm 2023 Gdma 2023 Bundlers 2023 Tianjin China April 17 20 2023 Proceedings Lncs 13922

Authors: Amr El Abbadi ,Gillian Dobbie ,Zhiyong Feng ,Lu Chen ,Xiaohui Tao ,Yingxia Shao ,Hongzhi Yin

1st Edition

3031354141, 978-3031354144

More Books

Students also viewed these Databases questions