Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, Please i need clarification and possibly adjustment to this C++ answer code i was giving. it works for level 1 and 2... but fails

Hi, Please i need clarification and possibly adjustment to this C++ answer code i was giving. it works for level 1 and 2... but fails for level 3 and 4.

#include #include #include #ifdef _WIN32 #include "getopt.h" #else #include #endif using namespace std; #ifndef ROLODEX_H_ #define ROLODEX_H_ template class Rolodex // Rolodex Class. { public: struct RolodexItem { // Defines a RolodexItem, contains a word and 2 pointers for next and prev items in the list. T value_; T LowerValue; RolodexItem* next_; RolodexItem* prev_; int sequence; }; Rolodex() // Default constructor. { inserted = 0; deleted = 0; forward = 0; backward = 0; sentinel = new RolodexItem; sentinel->next_ = sentinel; sentinel->prev_ = sentinel; current = sentinel; } bool isBeforeFirst() // Returns true if the Rolodex is positioned before the first card. { if (current == sentinel) { return(true); } else { return(false); } } bool isAfterLast() // Returns true if the Rolodex is positioned after the last card. { if (current == sentinel) { return (true); } else { return(false); } } void rotateForward() // Rotates the Rolodex one step forwards. { current = current->next_; forward++; return; } void rotateBackward() // Rotates the Rolodex one step backwards. { current = current->prev_; backward++; return; } T& currentValue(bool alpha = false) // Returns the value of the current card or the lowercase card depending on alpha's state. { if (alpha) { return (current->LowerValue); } return(current->value_); } void insertAfterCurrent(const T& value) // Inserts a new card after the current position and positions the Rolodex at the newly inserted card. { current->next_->prev_ = new RolodexItem; current->next_->prev_->prev_ = current; current->next_->prev_->next_ = current->next_; current->next_ = current->next_->prev_; current = current->next_; current->value_ = value; current->LowerValue = value; string temp = current->LowerValue; for (int i = 0; i < temp.size(); i++) { temp[i] = tolower(temp[i]); } current->LowerValue = temp; inserted++; current->sequence = inserted + deleted; return; } void insertBeforeCurrent(const T& value) // Inserts a new card before the current position and positions the Rolodex at the newly inserted card. { current->prev_->next_ = new RolodexItem; current->prev_->next_->next_ = current; current->prev_->next_->prev_ = current->prev_; current->prev_ = current->prev_->next_; current = current->prev_; current->value_ = value; current->LowerValue = value; string temp = current->LowerValue; for (int i = 0; i < temp.size(); i++) { temp[i] = tolower(temp[i]); } current->LowerValue = temp; inserted++; current->sequence = inserted + deleted; return; } void Delete() // Deletes current card. Sets current to next unless next is sentinel. If next is sentinel current is set to previous. { RolodexItem* temp; current->prev_->next_ = current->next_; current->next_->prev_ = current->prev_; if (current->next_ != sentinel) { temp = current->next_; } else { temp = current->prev_; } delete current; current = temp; deleted++; return; } void report() // reports nember of times each operation was executed. { cout << inserted << " " << deleted << " " << forward << " " << backward << endl; } int Sequence() { return (current->sequence); } void printState() // Testing purposes. { RolodexItem *item = sentinel->next_; while (item != sentinel) { item = item->next_; cout << item->value_ << endl; } } private: int deleted; int inserted; int forward; int backward; RolodexItem* sentinel; RolodexItem* current; }; #endif /* ROLODEX_H_ */ istream& input(bool FILEFLAG); ifstream in; int main(int argc, char** argv) { int c; bool verbose = false; // Report actions flag. bool NoDuplicates = false; // NoDuplicates flag. bool report = false; // Reports operations flag. bool alphabetic = false; // Alphabetical order flag. bool sequence = false; // Output sequence flag. bool plus = false; // Plus flag decleration. bool minus = false; // Minus flag decleration. bool FILEFLAG; // Flag to determine wether to use standard input or external file input. while ((c = getopt(argc, argv, "vdras")) != -1) { switch (c) { case 'v': verbose = true; break; // Report actions if true. case 'd': NoDuplicates = true; break; // Will not insert duplicates if true. case 'r': report = true; break; // Reports operations if true. case 'a': alphabetic = true; break; // Outputs in alphabetical order if true. case 's': sequence = true; break; // Outputs words input order if true. } } argc -= optind; argv += optind; if (argc == 0) { FILEFLAG = false; } for (int i = 0; i < argc; i++) { in.open(argv[i]); if (in.fail()) { cerr << "Can't read " << argv[i] << endl; exit(0); } FILEFLAG = true; } string word; Rolodex rolodex; while (input(FILEFLAG) >> word) { if (word[0] == '+') { plus = true; // Sets plus flag if a '+' is read. word.erase(word.begin()); } if (word[0] == '-') { minus = true; // Sets minus flag if a '-' is read. word.erase(word.begin()); } string compareword = word; if (alphabetic) { for (int i = 0; i <= word.length(); i++) { // Converts the word to lowercase for comparison with current->LowerValue; compareword[i] = tolower(word[i]); } } if (rolodex.isBeforeFirst() || rolodex.currentValue(alphabetic) <= compareword) { // If rolodex is before first or the current value(word) is <= word. while (!rolodex.isAfterLast() && rolodex.currentValue(alphabetic) < compareword) { // While rolodex is not after last and the current value(word) is < word. rolodex.rotateForward(); if (verbose) { cerr << "forward" << endl; } } if (!minus) { // If '-' wasnt used... if (!NoDuplicates) { // If -d is not used, always insert the word. rolodex.insertBeforeCurrent(word); if (verbose) { cerr << "insertBefore" << endl; } } else if (rolodex.currentValue(alphabetic) != compareword || plus == true) { // If -d is used, insert word if not already present or it has a '+' prefix. rolodex.insertBeforeCurrent(word); plus = false; // Reset flag. } } else if (rolodex.currentValue(alphabetic) == compareword) { // If '-' was used and the current value is not == word. rolodex.Delete(); // Delete the current word. minus = false; // Reset flag. } } else if (rolodex.isAfterLast() || rolodex.currentValue(alphabetic) >= compareword) { while (!rolodex.isBeforeFirst() && rolodex.currentValue(alphabetic) > compareword) { rolodex.rotateBackward(); if (verbose) { cerr << "backward" << endl; } } if (!minus) { if (!NoDuplicates) { rolodex.insertAfterCurrent(word); if (verbose) { cerr << "insertAfter" << endl; } } else if (rolodex.currentValue(alphabetic) != compareword || plus == true) { rolodex.insertAfterCurrent(word); plus = false; } } else if (rolodex.currentValue(alphabetic) == compareword) { rolodex.Delete(); minus = false; } } } if (sequence) { // Outputs the sequence number in which the word was entered, also outputs in alphabetical order. while (!rolodex.isBeforeFirst()) { rolodex.rotateBackward(); } rolodex.rotateForward(); // Go to first card while (!rolodex.isAfterLast()) { cout << rolodex.Sequence() << " " << rolodex.currentValue() << endl; // Outputs sequence number " " current card. rolodex.rotateForward(); } } else if (!report) { // Outputs in either normal alphabetical or strict alphabetical depending on if -a was used. while (!rolodex.isBeforeFirst()) { rolodex.rotateBackward(); } rolodex.rotateForward(); // Go to first card while (!rolodex.isAfterLast()) { cout << rolodex.currentValue() << endl; rolodex.rotateForward(); } } else { rolodex.report(); // Reports operations. } #ifdef _WIN32 system("pause"); #endif return(0); } istream& input(bool FILEFLAG) { if (FILEFLAG == true) { return in; } else { return cin; } }

This is the failed report i get.

Level 3: Failed test 4: differences in output ..... arguments .................... -c ..... input data ................... a b c -c ..... expected stdout .............. b ..... observed stdout .............. a b ..... expected stderr .............. ..... observed stderr .............. ./test: invalid option -- 'c'

Level 4: Failed test 2: differences in output ..... arguments .................... -c ..... input data ................... a b c -c ..... expected stdout .............. b ..... observed stdout .............. a b ..... expected stderr .............. ..... observed stderr .............. ./test: invalid option -- 'c'

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 Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

2. Describe why we form relationships

Answered: 1 week ago

Question

5. Outline the predictable stages of most relationships

Answered: 1 week ago