// Deletes a specified collection. An error occurs if a collection with the // specified name does not exist. void delete_Collection(Ordered_list<Collection> &catalog) { String name; cin >> name; Ordered_list<Collection>::Iterator it = catalog.begin(); while (it != catalog.end()) { if (it -> get_name() == name) { it -> clear(); catalog.erase(it); num_Collections--; cout << "Collection " << name << " deleted" << endl; return; } ++it; } throw Error("No collection with that name!"); return; }
// Add a record to a collection. An error occurs if the collection does not // exist, an integer cannot be read, an invalid record number is specified, // or the specified record is already a member of the specified collection. void add_Record_to_Collection(const Ordered_list<Record *> &library, Ordered_list<Collection> &catalog) { String name; cin >> name; Collection tmp_Collection(name); Ordered_list<Collection>::Iterator collection_it = catalog.find(tmp_Collection); if (collection_it == catalog.end()) { throw Error("No collection with that name!"); } int ID = get_int(cin); Record tmp_Record(ID); Ordered_list<Record *>::Iterator record_it = library.find(&tmp_Record); if (record_it == library.end()) { throw Error("No record with that ID!"); } collection_it -> add_member(*record_it); cout << "Member " << ID << " " << (*record_it) -> get_title() << " added" << endl; return; }
// Remove a specified record from a specified collection. Errors occur if // an integer cannot be read, there is not a record with the specified ID, // or a collection does not exist with the specified name. void remove_Record_from_Collection(const Ordered_list<Record *> &library, Ordered_list<Collection> &catalog) { String name; cin >> name; Ordered_list<Collection>::Iterator it = catalog.begin(); while (it != catalog.end()) { if (it -> get_name() == name) { int ID = get_int(cin); Record tmp_Record(ID); Ordered_list<Record *>::Iterator record_it = library.find(&tmp_Record); if (record_it == library.end()) { throw Error("No record with that ID!"); } it -> remove_member(*record_it); cout << "Member " << ID << " " << (*record_it) -> get_title() << " deleted" << endl; return; } ++it; } throw Error("No collection with that name!"); return; }
void print(const char* label, const Ordered_list<T, OF>& in_list) { cout << label << " has " << in_list.size() << " items:"; for(typename Ordered_list<T, OF>::Iterator it = in_list.begin(); it != in_list.end(); it++) { cout << ' ' << *it; } cout << endl; }
void print_ptr(Ordered_list<T>& in_list) { for(typename Ordered_list<T>::Iterator it = in_list.begin(); it != in_list.end(); it++) { if(it != in_list.begin()) // output a leading space after the first one cout << ' '; cout << *(*it); } cout << endl; }
Error_code binary_2(const Ordered_list &the_list, const Key &target, int &position) { Record data; int bottom = 0, top = the_list.size()-1; while(bottom<=top){ position = (bottom+top)/2; the_list.retrieve(position,data); if(data==target) return success; if(data<target) bottom=position+1; else top = position-1; } return not_present; }
// Restore the state of the program from a previous save. An error occurs // if the file could not be opened, or the file contains invalid data (which // would be the case if the specified file has not been generated by the // program in a previous state. void restore_All(Ordered_list<Record *> &library_by_title, Ordered_list<Record *> &library_by_ID, Ordered_list<Collection> &catalog) { String filename; cin >> filename; ifstream is(filename.c_str()); if (!is) { throw Error("Could not open file!"); } try { clear_data(library_by_title, library_by_ID, catalog); Record::reset_ID_counter(); num_Records = get_int(is); for (int i = 0; i < num_Records; i++) { Record *record = new Record(is); library_by_title.insert(record); library_by_ID.insert(record); } num_Collections = get_int(is); for (int i = 0; i < num_Collections; i++) { Collection collection(is, library_by_title); catalog.insert(collection); } } catch (Error &error) { clear_data(library_by_title, library_by_ID, catalog); Record::reset_ID_counter(); throw Error("Invalid data found in file!"); } is.close(); cout << "Data loaded" << endl; return; }
// Find a record with a speified ID, and print the information about it. // An error occurs if an integer cannot be read, or a record with the // specified ID does not exist. void find_Record_using_ID(const Ordered_list<Record *> &library) { int ID = get_int(cin); Record tmp_Record(ID); Ordered_list<Record *>::Iterator it = library.find(&tmp_Record); if (it == library.end()) { throw Error("No record with that ID!"); } cout << **it; return; }
// Find a record with a specified title and print the information about it. // An error occurs if a record with that name does not exist. void find_Record_using_title(const Ordered_list<Record *> &library) { String title = get_title(); Record tmp_Record(title); Ordered_list<Record *>::Iterator it = library.find(&tmp_Record); if (it != library.end()) { cout << **it; } else { throw Error("No record with that title!"); } return; }
// This clears the contents of the catalog. void clear_Catalog(Ordered_list<Collection> &catalog) { num_Collections = 0; catalog.clear(); cout << "All collections deleted" << endl; return; }
// Prints the contents of the currently library. void print_Library(const Ordered_list<Record *> &library) { int num_records = library.size(); if (!num_records) { cout << "Library is empty" << endl; return; } cout << "Library contains " << num_records << " records:" << endl; Ordered_list<Record *>::Iterator it = library.begin(); while (it != library.end()) { cout << **it; ++it; } return; }
// Change the rating of a record. Errors occur if an integer cannot be read, // a record with the specified ID does not exist, or the value of the new // rating is not in the valid rating range. void modify_Record_rating(Ordered_list<Record *> &library) { int ID = get_int(cin); Record tmp_Record(ID); Ordered_list<Record *>::Iterator it = library.find(&tmp_Record); if (it == library.end()) { throw Error("No record with that ID!"); } int rating = get_int(cin); (*it) -> set_rating(rating); cout << "Rating for record " << ID << " changed to " << rating << endl; return; }
// Prints the contents of the current catalog. void print_Catalog(const Ordered_list<Collection> &catalog) { int num_collections = catalog.size(); if (!num_collections) { cout << "Catalog is empty" << endl; return; } cout << "Catalog contains " << num_collections << " collections:" << endl; Ordered_list<Collection>::Iterator it = catalog.begin(); while (it != catalog.end()) { cout << *it; ++it; } return; }
//Recursive implementation without equality checking Error_code recursive_binary_1(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if(bottom<top){ int mid = (bottom+top)/2; the_list.retrieve(mid, data); if(data<target) return recursive_binary_1(the_list,target,mid+1,top,position); else return recursive_binary_1(the_list,target,bottom,mid,position); } else if(top<bottom) return not_present; else{ position = bottom; the_list.retrieve(bottom,data); if(data==target) return success; else return not_present; } }
//Iterative version without equality checking Error_code binary_1(const Ordered_list &the_list,const Key &target, int &position) { Record data; int bottom =0, top = the_list.size()-1; while(bottom<top){ int mid = (bottom+top)/2; the_list.retrieve(mid,data); if(data<target) bottom = mid+1; else top = mid; } if(top<bottom) return not_present; else{ position = bottom; the_list.retrieve(bottom,data); if(data==target) return success; else return not_present; } }
// Prints the contents of a collection with a specified name. An error // occurs if a collection of that name does not exist. void print_Collection_using_name(const Ordered_list<Collection> &catalog) { String name; cin >> name; Ordered_list<Collection>::Iterator it = catalog.begin(); while (it != catalog.end()) { if (it -> get_name() == name) { cout << *it; return; } ++it; } throw Error("No collection with that name!"); return; }
// Save the current state of the program to a specified output file. An // error occurs if the specified file cannot be opened for writing. void save_All(const Ordered_list<Record *> &library, const Ordered_list<Collection> &catalog) { String filename; cin >> filename; ofstream os(filename.c_str()); if (!os) { throw Error("Could not open file!"); } os << library.size() << endl; Ordered_list<Record *>::Iterator record_it = library.begin(); while (record_it != library.end()) { (*record_it) -> save(os); ++record_it; } os << catalog.size() << endl; Ordered_list<Collection>::Iterator collection_it = catalog.begin(); while (collection_it != catalog.end()) { collection_it -> save(os); ++collection_it; } os.close(); cout << "Data saved" << endl; return; }
// This clears the contents of the current library. An error occurs if // there is a collection with one or more members. void clear_Library(Ordered_list<Record *> &library_by_title, Ordered_list<Record *> &library_by_ID, const Ordered_list<Collection> &catalog) { Ordered_list<Collection>::Iterator it = catalog.begin(); while (it != catalog.end()) { if (!it -> empty()) { throw Error("Cannot clear all records unless all collections are empty!"); } ++it; } empty_record_list(library_by_title); // We've already called the destructors for the pointers to records // so now we just need to clear the other list library_by_ID.clear(); num_Records = 0; cout << "All records deleted" << endl; return; }
// Add collection with a specified name. An error occurs if the catalog // already has a collection with the specified name. void add_Collection(Ordered_list<Collection> &catalog) { String name; cin >> name; Ordered_list<Collection>::Iterator it = catalog.begin(); while (it != catalog.end()) { if (it -> get_name() == name) { throw Error("Catalog already has a collection with this name!"); } ++it; } catalog.insert(Collection(name)); num_Collections++; cout << "Collection " << name << " added" << endl; return; }
// Add a record to the library. An error occurs if the library already has // a record with the specified name. void add_Record(Ordered_list<Record *> &library_by_title, Ordered_list<Record *> &library_by_ID) { String medium; cin >> medium; String title = get_title(); Record tmp_Record(title); if (library_by_title.find(&tmp_Record) != library_by_title.end()) { throw Error("Library already has a record with this title!"); } Record * record = new Record(medium, title); library_by_title.insert(record); library_by_ID.insert(record); num_Records++; cout << "Record " << record -> get_ID() << " added" << endl; return; }
//Recursive version with equality checking Error_code recursive_binary_2(const Ordered_list &the_list, const Key &target, int bottom, int top, int &position) { Record data; if(bottom<=top){ int mid=(top+bottom)/2; the_list.retrieve(position,data); if(data==target){ position = mid; return success; } else if(data<target) recursive_binary_2(the_list,target,mid+1,top,position); else recursive_binary_2(the_list,target,bottom,mid,position); } else return not_present; }
// Remove a record from the library. An error occurs if a record with the // specified title does not exist, or if the specified record is a member of // one or more collections. void delete_Record(Ordered_list<Record *> &library_by_title, Ordered_list<Record *> &library_by_ID, const Ordered_list<Collection> &catalog) { String title = get_title(); Record tmp_Record(title); Ordered_list<Record *>::Iterator record_it = library_by_title.find(&tmp_Record); if (record_it == library_by_title.end()) { throw Error("No record with that title!"); } Record * record = *record_it; Ordered_list<Collection>::Iterator collection_it = catalog.begin(); while (collection_it != catalog.end()) { if (collection_it -> is_member_present(record)) { throw Error("Cannot delete a record that is a member of a collection!"); } ++collection_it; } library_by_title.erase(record_it); record_it = library_by_ID.find(record); library_by_ID.erase(record_it); num_Records--; cout << "Record " << record -> get_ID() << " " << record -> get_title() << " deleted" << endl; delete record; return; }
Ordered_list<int> subfunction(Ordered_list<int> in_list) { in_list.insert(4); in_list.insert(0); return in_list; }