#include #include // Provides toupper #include // Provides EXIT_SUCCESS and size_t #include class List { public: enum { CAPACITY = 3 }; // or, static const size_t CAPACITY = 30; typedef double Item; List( ) { used = 0; } void start( ) { current_index = 0; } void advance( ) { current_index++; } void insert(const Item& entry); void attach(const Item& entry); void remove_current( ); size_t size( ) const { return used; } bool is_item( ) const { return (current_index < used)? true:false; } size_t index ( ) const { return current_index; } Item current( ) const { return data[current_index]; } private: Item data[CAPACITY]; size_t used; size_t current_index; }; void List::remove_current( ) { assert(is_item()); for(int i = current_index + 1; i < used; i++) data[i-1] = data[i]; used--; } void List::insert(const Item& entry) { size_t i; assert(size() < CAPACITY); if(!is_item()) current_index = 0; for(i = used; i > current_index; i--) data[i] = data[i-1]; data[current_index] = entry; used++; } void List::attach(const Item& entry) { size_t i; assert(size() < CAPACITY); if(!is_item()) current_index = used; else { for(i = used; i > current_index + 1; i--) data[i] = data[i-1]; current_index += 1; } data[current_index] = entry; used++; } void print_menu( ) { cout << "\nThe following choices are available: " << endl; cout << " ! Activate the start( ) function" << endl; cout << " + Activate the advance( ) function" << endl; cout << " ? Print the result from the is_item( ) function" << endl; cout << " C Print the result from the current( ) function" << endl; cout << " P Print a copy of the entire List (also index and used)" << endl; cout << " I Insert a new number with the insert(...) function" << endl; cout << " A Attach a new number with the attach(...) function" << endl; cout << " R Activate the remove_current( ) function" << endl; cout << " Q Quit this test program" << endl; } char get_user_command( ) { char command; cout << "Enter choice: "; cin >> command; // Input of characters skips blanks and newline character return command; } void show_list(List display) // call by value! { cout << "Current Index = " << display.index() << " " << "Used Size = " << display.size() << endl; cout << "( "; for (display.start( ); display.is_item( ); display.advance( )) cout << display.current( ) << " "; cout << ")"; } double get_number( ) { double result; cout << "Please enter a real number for the List: "; cin >> result; cout << result << " has been read." << endl; return result; } void main( ) { List test; // A List that we'll perform tests on char choice; // A command character entered by the user cout << "I have initialized an empty List of real numbers." << endl; do { print_menu( ); choice = (char)toupper(get_user_command( )); switch (choice) { case '!': test.start(); break; case '+': test.advance( ); break; case '?': if (test.is_item( )) cout << "There is an item." << endl; else cout << "There is no current item." << endl; break; case 'C': if (test.is_item( )) cout << "Current item is: " << test.current( ) << endl; else cout << "There is no current item." << endl; break; case 'P': show_list(test); break; case 'I': test.insert(get_number( )); break; case 'A': test.attach(get_number( )); break; case 'R': test.remove_current( ); cout << "The current item has been removed" << endl; break; case 'Q': cout << "Ridicule is the best test of truth." << endl; break; default: cout << choice << " is invalid. Sorry." << endl; } } while ((choice != 'Q')); }