Guest User

Untitled

a guest
Jan 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Entry {
  5. private:
  6. string _name; // name
  7. string _room; // room
  8. public:
  9. Entry() = default; // for allocation
  10. Entry(string n, string r) : _name{n}, _room{r} {};
  11. string name() { return _name; }
  12. string room() { return _room; }
  13. void set_room(string r) { _room = r; }
  14. };
  15.  
  16. class AddressBook {
  17. private:
  18. int size = 0; // size
  19. Entry* entries; // pointer to beginning of memory block for entries
  20. void sort_entries();
  21. public:
  22. class EntryDoesNotExist{};
  23. AddressBook() : entries{new Entry[0]} {};
  24. ~AddressBook() { delete[] entries; }
  25. void add(string name, string room);
  26. void move(string name, string room);
  27. void remove(string name);
  28. void clear();
  29. string find(string name);
  30. void print_sorted();
  31. };
  32.  
  33. void AddressBook::add(string name, string room)
  34. {
  35. size++; // increment size
  36. Entry* extended = new Entry[size]; // allocate size+1 Entries on heap
  37. if (!extended) throw runtime_error("Could not allocate memory");
  38. for (int i = 0; i < size - 1; i++) {
  39. extended[i] = entries[i]; // copy old Entries over to a new block
  40. }
  41. Entry e{name, room};
  42. extended[size - 1] = e;
  43. delete[] entries; // deallocate old block
  44. entries = extended; // point entries to a new block
  45. }
  46.  
  47. void AddressBook::remove(string name)
  48. {
  49. string _ = find(name); // only for error handling, return value is not needed
  50.  
  51. size--; // decrement size
  52. Entry* reduced = new Entry[size]; // allocate size-1 Entries on heap
  53. if (!reduced) throw runtime_error("Could not allocate memory");
  54. for (int i = 0; i < size; i++) {
  55. if (entries[i].name() == name) {
  56. reduced[i] = entries[i+1]; // skip over the record to remove
  57. } else {
  58. reduced[i] = entries[i];
  59. }
  60. }
  61. delete[] entries; // deallocate original entries
  62. entries = reduced; // point entries to a reduced block
  63. }
  64.  
  65.  
  66. string AddressBook::find(string name)
  67. {
  68. for (int i = 0; i < size; i++)
  69. if (entries[i].name() == name) return entries[i].room();
  70. throw EntryDoesNotExist();
  71. }
  72.  
  73. void AddressBook::move(string name, string room)
  74. {
  75. for (int i = 0; i < size; i++)
  76. if (entries[i].name() == name) entries[i].set_room(room);
  77. }
  78.  
  79. void AddressBook::print_sorted()
  80. {
  81. sort_entries();
  82. for (int i = 0; i < size; i++) {
  83. cout << entries[i].name() << " is in room " << entries[i].room() << endl;
  84. }
  85. }
  86.  
  87. void AddressBook::clear()
  88. {
  89. size = 0;
  90. Entry* empty = new Entry[size];
  91. delete[] entries; // deallocate original entries
  92. entries = empty; // point to new empty block
  93. }
  94.  
  95. // Bubble sort from sratch. We sort in place by swapping through pointers.
  96. // I know it's not ideal, but in our case we don't care
  97. // about the specific ordering of elements inside the structure,
  98. // so might as well re-sort them before each print.
  99. // (it seems natural to me to make the print function an instance method,
  100. // so it maps better to the interface)
  101. void AddressBook::sort_entries()
  102. {
  103. bool swap_needed = true; // initial pass
  104. for (int i = 1; i <= size && swap_needed; i++) {
  105. swap_needed = false; // break early if no more swaps needed
  106. for (int j = 0; j < size-1; j++) {
  107. // Two passes to sort by name and by room
  108. if (entries[j].name() > entries[j+1].name()) {
  109. swap(entries[j+1], entries[j]);
  110. swap_needed = true;
  111. }
  112. if (entries[j].room() > entries[j+1].room()) {
  113. swap(entries[j+1], entries[j]);
  114. swap_needed = true;
  115. }
  116. }
  117. }
  118. }
  119.  
  120. void operate_on_input(AddressBook& ab)
  121. {
  122. while(cin) {
  123. cout << "> ";
  124. string operation;
  125. cin >> operation;
  126. if (operation == "add") {
  127. string name, room;
  128. cin >> name >> room;
  129. ab.add(name, room);
  130. } else if (operation == "list") {
  131. ab.print_sorted();
  132. } else if (operation == "remove") {
  133. string name;
  134. cin >> name;
  135. ab.remove(name);
  136. } else if (operation == "move") {
  137. string name, room;
  138. cin >> name >> room;
  139. ab.move(name, room);
  140. } else if (operation == "find") {
  141. string name;
  142. cin >> name;
  143. string room = ab.find(name);
  144. cout << name << " is in room " << room << endl;
  145. } else if (operation == "clear") {
  146. ab.clear();
  147. } else if (operation == "exit") {
  148. break;
  149. }
  150. }
  151. }
  152.  
  153. int main()
  154. {
  155. AddressBook ab;
  156. try {
  157. operate_on_input(ab);
  158. } catch (AddressBook::EntryDoesNotExist) {
  159. cerr << "error: entry does not exist" << endl;
  160. }
  161.  
  162. return 0;
  163. }
Add Comment
Please, Sign In to add comment