Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Client program Your program will ask the user for the file name, open the text file , read each category / line record and store

Client program
Your program will ask the user for the file name, open the text file , read each category/line record and store into two strings. Make sure the file opened correctly before you start processing it. You do not know how many records are in the file so your program should read until it gets to the end of the file.
Your program will invoke the add method on the linked list object passing the category and line of text for the current record.
It will then display the contents of the list (the records that were read from the file). Display the category once with all of the lines in that category indented.
You will use a class method toVector that puts all of the category/line pairs into a vector and returns it to the program that will display it.
Add Method Details
Your add method will create the node, set the values and put the node in the list at the correct location. The lines of text should be grouped by category and in ascending order within each category. Put the categories into the linked list in the same order they were first encountered in the file. The program will continue to do this until the end of the file has been reached.
* Add a node this method will take as input two string values the category and the line. It will create a node object and set the attributes. Then it will put the node in the linked list in alphabetical order grouped by category. The categories will not be in sorted order they will be in the order they were encountered in the file, but the lines within each category will be in alphabetical order. You will not add the items then sort them later you MUST put them in the correct spot one time. You will not use a vector for any of this process - you are to only process your linked list.
* toVector returns vector with the contents of the list which will be a string category followed by a string line of text from the file. You will use only the push_back method to get the strings into the vector.
* Destructor
#include
#include
#include
#include
using namespace std;
// Node class for the linked list
class Node {
public:
std::string category;
std::string line;
Node* next;
Node(const string& data) : line(data), next(nullptr){}
};
// Linked list class
class LinkedList {
private:
Node* headPtr;
int numItems;
public:
LinkedList() : headPtr(nullptr), numItems(0){}
~LinkedList();
void addNode(const string& line);
void displayList();
};
LinkedList::~LinkedList(){
while (headPtr != nullptr){
Node* temp = headPtr;
headPtr = headPtr->next;
delete temp;
}
}
void LinkedList::addNode(const string& line){
Node* newNode = new Node(line);
if (headPtr == nullptr){
headPtr = newNode;
} else {
Node* curPtr = headPtr;
while (curPtr->next != nullptr){
curPtr = curPtr->next;
}
curPtr->next = newNode;
}
}
void LinkedList::displayList(){
Node* curPtr = headPtr;
while (curPtr != nullptr){
cout curPtr->line endl;
curPtr = curPtr->next;
}
}
int main(){
LinkedList categories[5]; // Assuming 5 categories: Boring, Interesting, Name, Nonsense, Others
string filename ="/Users/input.txt"; // Change this to your input file name
ifstream inputFile(filename);
if (!inputFile){
cerr "Error opening file " filename endl;
return 1;
}
string line;
string category;
while (getline(inputFile, line)){
if (line.empty()) continue;
// Check the category and add line to the respective linked list
if (category == "Boring") categories[0].addNode(line);
else if (category == "Interesting") categories[1].addNode(line);
else if (category == "Name") categories[2].addNode(line);
else if (category == "Nonsense") categories[3].addNode(line);
else categories[4].addNode(line); // Others
}
inputFile.close();
// Display the lines in the specified order
for (int i =0; i 5; i++){
categories[i].displayList();
}
return 0;
}Boring
The sky is blue.
Name
Clovis Bagwell
Interesting
The quick brown fox jumped over the lazy dog.
Interesting
Shake rattle and roll.
Nonsense
One red bean stuck in the bottom of a tin bowl.
Interesting
Rats live on no evil star.
Name
Mark Knopfler
Nonsense
Ceramic fists artificial deceased 'n cists rancid buds burst
Interesting
It is impossible for most people to lick their own elbow.
Interesting
An ostrich's eye is bigger than its brain.
Name
Archibald Beechcroft
Boring
Workers of the world unite.
Nonsense
A squid eating dough in a polyethylene bag is fast and bulbous.
Name
Oliver Crangle
Interesting
Rubber bands last longer when refrigerated.
Boring
Data structures is really hard.
Boring
It's rain
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Chemical Engineering questions

Question

Describe effectiveness of reading at night?

Answered: 1 week ago

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago