Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Account{
  6. public:
  7. string employee;
  8. string class_room;
  9. string combined(){
  10. return class_room+employee;
  11. }
  12. };
  13.  
  14. class myvector{
  15. private:
  16.  
  17. public:
  18. Account *employees;
  19. int size;
  20. myvector(){
  21. size=0;
  22. employees=new Account[size];
  23. }
  24.  
  25. ~myvector(){
  26. delete[] employees;
  27. }
  28.  
  29. void push(string name, string room){
  30. size++;
  31. Account *array=new Account[size];
  32.  
  33. for(int i=0; i<size-1; i++){
  34. array[i] = employees[i];
  35. }
  36.  
  37. Account *newaccount=new Account;
  38. newaccount->employee=name;
  39. newaccount->class_room=room;
  40. array[size-1]= *newaccount;
  41. delete[] employees;
  42. employees = array;
  43.  
  44. }
  45. Account at(int index){
  46. return employees[index];
  47. }
  48. int lenght(){
  49. return size;
  50. }
  51. int search(string name){
  52. for(int i=0; i<size; i++){
  53. if(employees[i].employee==name){
  54. return i;
  55. }
  56. }
  57. return -1;
  58. }
  59. void erase(string name){
  60. employees[search(name)]=employees[size-1];
  61. Account *array=new Account[size-1];
  62. for(int i=0; i<size-1; i++){
  63. if(employees[i].employee!=name){
  64. array[i] = employees[i];
  65. }
  66. }
  67. delete[] employees;
  68. employees = array;
  69. size--;
  70. }
  71. void clear(){
  72. delete[] employees;
  73. size=0;
  74. Account *employees=new Account[size];
  75. }
  76. void sort(){
  77. bool isUnsorted=true;
  78. int sequence_count=1;
  79. while(isUnsorted){
  80. isUnsorted=false;
  81. for(int i=0; i<size-sequence_count; i++){
  82. if((employees[i].combined().compare(employees[i+1].combined()))==1){
  83. isUnsorted=true;
  84. Account token=employees[i];
  85. employees[i]=employees[i+1];
  86. employees[i+1]=token;
  87. }
  88. }
  89. sequence_count++;
  90. }
  91. }
  92. };
  93.  
  94. class AddressBook{
  95. private:
  96. myvector accounts;
  97. public:
  98. void add(string name, string room){
  99. if(accounts.search(name)!=-1){
  100. throw runtime_error("Account already exists");
  101. }
  102. accounts.push(name, room);
  103. }
  104. void move(string name, string room){
  105. if(accounts.search(name)==-1){
  106. throw runtime_error("Account does not exists");
  107. }
  108. accounts.erase(name);
  109. accounts.push(name, room);
  110. }
  111. void removal(string name){
  112. if(accounts.search(name)==-1){
  113. throw runtime_error("Account does not exists");
  114. }
  115. accounts.erase(name);
  116. }
  117. void find(string name){
  118. if(accounts.search(name)==-1){
  119. throw runtime_error("Account does not exists");
  120. }
  121. cout << accounts.at(accounts.search(name)).employee << " is in room " << accounts.at(accounts.search(name)).class_room << endl;
  122. }
  123. void list(){
  124. if(accounts.lenght()<1){
  125. throw runtime_error("List is empty");
  126. }
  127. accounts.sort();
  128. for(int i=0; i<accounts.lenght(); i++){
  129. cout << accounts.at(i).employee << " is in room " << accounts.at(i).class_room << endl;
  130. }
  131. }
  132. void clear(){
  133. accounts.clear();
  134. }
  135. };
  136. void room_check(string room){
  137. if(room.size()!=4){
  138. throw runtime_error("Invalid room code");
  139. }
  140. if(!isalpha(room[0])){
  141. throw runtime_error("Invalid room code");
  142. }
  143. for(int i=1; i<4; i++){
  144. if(!isdigit(room[i])){
  145. throw runtime_error("Invalid room code");
  146. }
  147. }
  148. }
  149.  
  150. int main(){
  151. string command;
  152. string name;
  153. string room;
  154. AddressBook book;
  155. while (true){
  156. try{
  157. cin >> command;
  158. if (command=="add"||command=="move"||command=="remove"||command=="find"){
  159. cin >> name;
  160. if(command=="add"||command=="move"){
  161. cin >> room;
  162. }
  163. }
  164.  
  165. if(command=="add"){
  166. room_check(room);
  167. book.add(name, room);
  168. }
  169. if(command=="move"){
  170. room_check(room);
  171. book.move(name, room);
  172. }
  173. if(command=="remove"){
  174. book.removal(name);
  175. }
  176. if(command=="find"){
  177. book.find(name);
  178. }
  179. if(command=="list"){
  180. book.list();
  181. }
  182. if(command=="clear"){
  183. book.clear();
  184. }
  185. if(command=="exit"){
  186. return 0;
  187. }
  188. }
  189. catch (runtime_error &e){
  190. cerr << "Error: " <<e.what() << endl;
  191. }
  192. }
  193. return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement