#include #include #include #include #include #include "Set.H" #include "Map.H" Set* GetSet (void); Map* GetMap (void); void ProcMemError (void); void main() { set_new_handler (ProcMemError); int numSets, numMaps; cout << "How many sets do you wish to create?" << endl; cin >> numSets; assert (numSets >= 0); cout << "How many maps do you wish to create?" << endl; cin >> numMaps; assert (numMaps >= 0); int total = numSets + numMaps; assert (total > 0); Set** basePointers = new Set* [total]; int i; for (i = 0; i < numSets; ++i) { basePointers[i] = GetSet(); } for (i = numSets; i < total; ++i) { basePointers[i] = GetMap(); } cout << "\nDisplaying " << numSets << " sets and " << numMaps << " maps:\n" << endl; for (i = 0; i < total; ++i) { if (i < numSets) { cout << "Set: "; } else { cout << "Map: "; } cout << (*(basePointers[i])) << endl; } cout << "\nNow testing GetValue()\n"; cout << "How many keys do you want to enter?" << endl; int key, numKeys; char* value; cin >> numKeys; for (i = 1; i <= numKeys; ++i) { cout << "Enter key value "; cin >> key; value = ((Map *) basePointers[1])->GetValue (key); cout << key << " "; if (value == NULL) { cout << "Not found" << endl; } else { cout << value << endl; } } for (i = 0; i < total; ++i) { delete basePointers[i]; } cout << "\nEnd of Program" << endl; } Set* GetSet() { int count; cout << "How many elements in the set?" << endl; cin >> count; assert (count >= 0); if (count == 0) { return (new Set); } int* temp = new int [count]; for (int i = 0; i < count; ++i) { cout << "Enter element #" << (i + 1) << endl; cin >> temp[i]; } Set* sptr = new Set (temp, count); delete[] temp; return sptr; } Map* GetMap() { int count; cout << "How many key/value pairs in the map?" << endl; cin >> count; assert (count >= 0); if (count == 0) { return (new Map); } int* itemp = new int [count]; char** stemp = new char* [count]; int i; char temp[81]; for (i = 0; i < count; ++i) { cout << "Enter key #" << (i + 1) << endl; cin >> itemp[i]; cout << "Enter value #" << (i + 1) << endl; cin.ignore(); cin.getline (temp, 81); stemp[i] = new char [strlen (temp) + 1]; strcpy (stemp[i], temp); } Map* mptr = new Map (itemp, stemp, count); delete[] itemp; for (i = 0; i < count; ++i) { delete [] stemp[i]; } delete[] stemp; return mptr; } void ProcMemError() { cerr << "Memory allocation error" << endl; exit (-1); }