Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with following program to pass all levels in C++ Task A Rolodex is a rotating file that is usually used to record

I need help with following program to pass all levels in C++

Task

A Rolodex is a rotating file that is usually used to record business contact information. Cards are inserted in alphabetic order by name. You operate the Rolodex by rolling it forwards or backwards until you find the information you need (or find the place where a new card should be inserted).

Your task is to create an class that simulates a Rolodex and use it to implement a word sorting program. The program starts with an empty Rolodex, then reads words from standard input and adds a card for each word into the appropriate position to maintain overall sorted order. To find the correct position, the Rolodex is rolled either backwards or forwards from the current card. When all input has been processed, the program moves the Rolodex to the beginning and then outputs each word in order, one per line

A basic version of the program that includes a skeleton Rolodex class is provided. The class has methods for rotating the Rolodex forwards and backwards, for returning the value of the card at the current position, and for inserting a new card either before or after the current card. The initial version of the program implements the basic sorting algorithm, which is a modified version of insertion sort (it is consequently not very efficient). In addition, it supports a command line processing mechanism that allows you to run the program with options. The initial version recognises two options: -v (verbose) and -c (current). The verbose option causes the actions of the Rolodex to be reported as they occur, which might be useful when debugging, whilst the current option causes the program to print only the current card's value rather than all card values from the current card.

Since you do not know how many entries the Rolodex will hold, you will need to use dynamically allocated memory to store the information. An easy way to implement the Rolodex file is using a doubly linked list. Each item in the list will store the information for a single card and will need an instance variable to store the card value as well as pointers to the next and previous items. For example, you could represent the items like this:

struct RolodexItem

{ std::string value_;

RolodexItem* next_;

RolodexItem* prev_;

};

You will also need some way of knowing when you have reached the beginning or end of the Rolodex. One strategy is to use a circular list with a sentinel value that marks both the beginning and the end. This approach will considerably simplify the management of the links when items are inserted or removed.

Level 1: Basic operation Complete the implementation of Rolodex so that all the methods work correctly. The insert methods should leave the Rolodex positioned so that the current item is the one just inserted.

Level 2: No duplicates Modify the program so that the command line option -d (no duplicates) causes a new item to be inserted only if the item is not already present in the Rolodex. For example, if the input consisted of the text

far far far away

then the output would contain only two lines

away

far

Level 3: Deletion Modify the program such that an input string that begins with a - causes that word (without the -) to be deleted from the Rolodex instead of inserted. For example, if the input is

the quick black fox jumps -black brown

over the lazy dog then the output would contain the word brown but not black.

Deleting a word should be implemented by moving the Rolodex to the position where the word would normally be inserted, but then deleting the current item if it matches the word (if it does not match, no action is performed). You will need to add a method to the Rolodex class that deletes the current item and leaves the Rolodex positioned so that the current item is the one following the deleted item if there is a following item, or the item preceding the deleted item otherwise.

Hint: as part of your solution, consider the case where the input word is a - (i.e., a dash).

Level 4: Report Modify the program so that if it is run with the command line option -r (report), it outputs a report of the Rolodex operations rather than the words themselves. The report should consist of the following integer values separated by spaces: the number of items inserted, the number of duplicate items omitted (expected to be 0 unless the -d option is in effect), the number of items deleted, the number of times the rotateForward operation was performed, and the number of times the rotateBackward operation was performed. For example, if the input consisted of the text

the quick brown fox jumps over the lazy dog

then the output should read

9 0 0 5 8

The report indicates that there were 9 words inserted, 0 duplicate words omitted, 0 words deleted, and that a total of 5 forward rotations and 8 backward rotations of the Rolodex were performed.

However, if the -d option is in effect for the same input, the output should instead read

8 1 0 5 8

In this case, the report indicates that there were 8 words inserted, 1 duplicate word omitted (the second occurrence of the), 0 words deleted, 5 forward rotations, and 8 backward rotations.

Main cpp:

#include

#include

#include

#include "Rolodex.h"

using namespace std;

auto verbose = false; // Log Rolodex actions

auto printCurrent = false; // Output only the current Rolodex card

void rolodex()

{

string word;

Rolodex rolodex;

while (cin >> word) {

if (rolodex.isBeforeFirst() || rolodex.currentValue() <= word) {

while (!rolodex.isAfterLast() && rolodex.currentValue() < word) {

rolodex.rotateForward();

if (verbose)

cerr << "rotateForward ";

}

rolodex.insertBeforeCurrent(word);

if (verbose)

cerr << "insertBeforeCurrent ";

}

else if (rolodex.isAfterLast() || rolodex.currentValue() >= word) {

while (!rolodex.isBeforeFirst() && rolodex.currentValue() > word) {

rolodex.rotateBackward();

if (verbose)

cerr << "rotateBackward ";

}

rolodex.insertAfterCurrent(word);

if (verbose)

cerr << "insertAfterCurrent ";

}

}

if (printCurrent) {

cout << rolodex.currentValue() << ' ';

}

else { // Output all Rolodex card values (default)

while (!rolodex.isBeforeFirst()) {

rolodex.rotateBackward();

}

rolodex.rotateForward(); // Go to the first card

while (!rolodex.isAfterLast()) {

cout << rolodex.currentValue() << ' ';

rolodex.rotateForward();

}

}

}

int main(int argc, char** argv)

{

int c;

while ((c = getopt(argc, argv, "vc")) != EOF) {

switch (c) {

case 'v':

verbose = true;

break;

case 'c':

printCurrent = true;

break;

}

}

argc -= optind;

argv += optind;

rolodex();

return 0;

}

Roledex cpp

#include "Rolodex.h"

Rolodex::Rolodex()

{

// Add code here

}

/**

* Returns true if the Rolodex is positioned before the first card.

*/

bool Rolodex::isBeforeFirst() const

{

// Replace with real code

return false;

}

/**

* Returns true if the Rolodex is positioned after the last card.

*/

bool Rolodex::isAfterLast() const

{

// Replace with real code

return false;

}

/**

* Rotates the Rolodex one card forwards.

*/

void Rolodex::rotateForward()

{

// Add code here

}

/**

* Rotates the Rolodex one card backwards.

*/

void Rolodex::rotateBackward()

{

// Add code here

}

/**

* Returns the value of the current card.

*/

const std::string& Rolodex::currentValue() const

{

// Temporary hack to keep the compiler happy

// Replace both lines with real code

static std::string dummy;

return dummy;

}

/**

* Inserts a new card after the current card and positions the Rolodex

* at the newly inserted card.

*

* @param value The value to insert into a new card.

*/

void Rolodex::insertAfterCurrent(const std::string& value)

{

// Add code here

}

/**

* Inserts a new card before the current card and positions the Rolodex

* at the newly inserted card.

*

* @param value The value to insert into a new card.

*/

void Rolodex::insertBeforeCurrent(const std::string& value)

{

// Add code here

}

Rolodex.h

#ifndef ROLODEX_H

#define ROLODEX_H

#include

class Rolodex {

public:

/**

* Creates a new empty Rolodex.

*/

explicit Rolodex();

/**

* Returns true if the Rolodex is positioned before the first card.

*/

bool isBeforeFirst() const;

/**

* Returns true if the Rolodex is positioned after the last card.

*/

bool isAfterLast() const;

/**

* Rotates the Rolodex one card forwards.

*/

void rotateForward();

/**

* Rotates the Rolodex one card backwards.

*/

void rotateBackward();

/**

Please help me

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

7 Explain how a communication process may be conceptualised.

Answered: 1 week ago