Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. // READ BEFORE YOU START:
  2. // You are given a partially completed program that creates a list of pets with their list of absents.
  3. // Each student has the corresponding information: name, standard, and a linked list of absents.
  4. // To begin, you should trace through the given code and understand how it works.
  5. // Please read the instructions above each required function and follow the directions carefully.
  6. // If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases.
  7. //
  8. // You are to assume that all input is valid:
  9. // Valid name: String containing alphabetical letters beginning with a capital letter
  10. // Valid standard: String containing alphabetical letters beginning with a number
  11. // Valid date: String in the following format: "MM/DD/YYYY" ex: "01/01/2010"
  12. // All input will be a valid length and no more than the allowed amount of memory will be used
  13. //
  14. // Q1 : CLASS METHODS Part 1 : Constructor and Accessor Methods for Student class in Student.cpp file ( 5 points)
  15. // Q2 : CLASS METHODS Part 2 : Class methods for Student class in Student.cpp file (10 points)
  16. // Q3 : Add Function in hw09.cpp file ( 5 points)
  17. // Q4 : Search Function in hw09.cpp file (10 points)
  18. // Q5 : Remove One Function in hw09.cpp file (15 points)
  19. // Q6 : Implement cin / cout for the lines in main without modifying the functionality ( 5 points)
  20.  
  21. #include <iostream>
  22. #include <fstream>
  23. #include <string>
  24.  
  25. #include "Container.h"
  26. #include "Student.h"
  27. #include "Absent.h"
  28.  
  29. using namespace std;
  30.  
  31. // forward declarations
  32. void flush();
  33. void branching(char);
  34. void registration(char);
  35. void add_student(string, string);
  36. Student* search_student(string, string);
  37. void remove_student(string, string);
  38.  
  39. void print_all();
  40. void remove_all();
  41.  
  42. Container* list = NULL; // global list
  43.  
  44. int main()
  45. {
  46. // _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); // Use to check for memory leaks in VS
  47.  
  48. char ch = 'i';
  49.  
  50. do {
  51.  
  52. // Q6: Implement cin / cout for the lines below without modifying the functionality (5 points)
  53. // (change all printf statements to cout and read the next char using cin)
  54. cout << "Please enter your selection\n";
  55. cout << "\ta: add a new student to the list\n";
  56. cout << "\tc: add a new absent for a student\n";
  57. cout << "\tr: remove a student from the list\n";
  58. cout << "\tp: print all students on the list\n";
  59. cout << "\tq: quit\n";
  60. cin >> ch;
  61. // End Q6
  62.  
  63. flush();
  64. branching(ch);
  65. } while (ch != 'q');
  66.  
  67. remove_all();
  68. list = NULL;
  69. return 0;
  70. }
  71.  
  72. void flush()
  73. {
  74. int c;
  75. do c = getchar(); while (c != '\n' && c != EOF);
  76. }
  77.  
  78. void branching(char c)
  79. {
  80. switch (c) {
  81. case 'a':
  82. case 'c':
  83. case 'r':
  84. case 'p':
  85. registration(c);
  86. break;
  87. case 'q':
  88. break;
  89. default:
  90. cout << endl << "Invalid input!" << endl << endl;
  91. }
  92. }
  93.  
  94. // The registration function is used to determine how much data is needed and which function to send that data to.
  95. // It uses pointers and values that are returned from some functions to produce the correct ouput.
  96. // There is no implementation needed here, but you should study this function and know how it works.
  97. // It is always helpful to understand how the code works before implementing new features.
  98. // Do not change anything in this function or you risk failing the automated test cases.
  99. void registration(char c)
  100. {
  101. string name, standard;
  102.  
  103. if (c == 'p')
  104. print_all();
  105. else
  106. {
  107. cout << endl << "Please enter the student's name: " << endl;
  108. cin >> name;
  109. cout << "Please enter the student's standard: " << endl;
  110. cin >> standard; flush();
  111.  
  112. Student* student_result = search_student(name, standard);
  113.  
  114. if (c == 'a') // add student
  115. {
  116. if (student_result == NULL)
  117. {
  118. add_student(name, standard);
  119. cout << endl << "Student added." << endl << endl;
  120. }
  121. else
  122. cout << endl << "Student already on list." << endl << endl;
  123. }
  124. else if (c == 'c') // add absent
  125. {
  126. if (student_result == NULL)
  127. {
  128. cout << endl << "Student not found." << endl << endl;
  129. return;
  130. }
  131.  
  132. string date;
  133. cout << "Please enter the date of the absent: " << endl;
  134. cin >> date; flush();
  135.  
  136. student_result->addAbsent(date);
  137. cout << endl << "Absent added." << endl << endl;
  138. }
  139. else if (c == 'r') // remove student
  140. {
  141. if (student_result == NULL)
  142. {
  143. cout << endl << "Student not found." << endl << endl;
  144. return;
  145. }
  146.  
  147. remove_student(name, standard);
  148. cout << endl << "Student removed from the list." << endl << endl;
  149. }
  150. }
  151. }
  152.  
  153. // Q3: Add Student (5 points)
  154. // This function will be used to add a new student to the head of you linked list of containers, no need for sorting.
  155. // The search function is called before this function, therefore you can assume the student is not already on the list.
  156. // If the student is added to the list, return 1. If the student already exists on the list (not added), return 0.
  157. void add_student(string name, string standard)
  158. {
  159. //creating new Student pointer
  160. Student *newStud = new Student(name, standard);
  161. //new Container object
  162. Container *newCont = new Container();
  163. newCont->student = newStud;
  164. //new list
  165. newCont->next = list;
  166. list = newCont;
  167. }
  168.  
  169. // Q4: Search (10 points)
  170. // This function will be used to search for a student on the list.
  171. // Students on the list may have the same name OR the same standard, but should not have the same name AND standard.
  172. // Therefore, you must traverse the list and return a pointer to a 'Student' with the desired name AND standard.
  173. // If the student does not exist on the list, return NULL. (See registration function for use of this function).
  174. Student* search_student(string name, string standard)
  175. {
  176. //setting new container to list
  177. Container* tempCont = list;
  178. while (tempCont != NULL)
  179. {
  180. //checking if names match up
  181. if (name == tempCont->student->getName())
  182. {
  183. return tempCont->student;
  184. }
  185. tempCont = tempCont->next;
  186. }
  187. //return NULL
  188. return NULL;
  189. }
  190.  
  191. void DeleteAbsentList(Absent* remove_start)
  192. {
  193. //checking if remove start is NULL (beginning of list)
  194. if (remove_start != NULL)
  195. {
  196. //setting the second pointer equal to start
  197. Absent* follow_remove = remove_start;
  198. while (follow_remove != NULL)
  199. {
  200. //iterating through and saying next
  201. remove_start = remove_start->next;
  202. delete follow_remove;
  203. //setting the pointers equal again
  204. follow_remove = remove_start;
  205. }
  206. //delete the function
  207. delete follow_remove;
  208. }
  209. }
  210. // Q5: Remove Pet (15 points)
  211. // This function will be used to remove a student from the list.
  212. // Traverse the list and use the parameters to remove the student.
  213. // Use proper memory management to ensure no memory leaks.
  214. void remove_student(string name, string standard)
  215. {
  216. //temp container set to list
  217. Container* temp = list;
  218. Container* follow = list;
  219. if (name == list->student->getName())
  220. {
  221. //creating absent free pointer and showing which one to free
  222. Absent* absentDelete = temp->student->absents;
  223. list = list->next;
  224. DeleteAbsentList(absentDelete);
  225. delete temp->student;
  226. delete temp;
  227. }
  228. else
  229. {
  230. temp = temp->next;
  231. while (temp->next != NULL)
  232. {
  233. if (name == temp->next->student->getName())
  234. {
  235. break;
  236. }
  237. follow = temp;
  238. temp = temp->next;
  239. }
  240. follow->next = temp->next;
  241. //deleting the new one
  242. Absent* absentDelete = temp->student->absents;
  243. DeleteAbsentList(absentDelete);
  244. //delete the student
  245. delete temp->student;
  246. //setting next to NULL
  247. if (temp->next != NULL)
  248. {
  249. //delete temp->next
  250. delete temp->next;
  251. }
  252. }
  253. }
  254.  
  255. // This function is already implemented for you. It traverses the list and removes all pets to ensure no memory leaks.
  256. void remove_all()
  257. {
  258. while (list != NULL)
  259. {
  260. Container* container_to_be_removed = list;
  261. list = list->next;
  262.  
  263. while (container_to_be_removed->student->absents != NULL)
  264. {
  265. Absent *absent_to_be_removed = container_to_be_removed->student->absents;
  266. container_to_be_removed->student->absents = container_to_be_removed->student->absents->next;
  267. delete absent_to_be_removed;
  268. }
  269.  
  270. delete container_to_be_removed->student;
  271. delete container_to_be_removed;
  272. }
  273. }
  274.  
  275. // This function is already implemented for you. It prints all of the pets in the list in an organized format.
  276. void print_all()
  277. {
  278. if (list == NULL) cout << endl << "List is empty!" << endl << endl;
  279.  
  280. Container *container_traverser = list;
  281.  
  282. while (container_traverser != NULL)
  283. {
  284. cout << endl <<
  285. "Name: " << container_traverser->student->getName() << endl <<
  286. "Standard: " << container_traverser->student->getStandard() << endl;
  287.  
  288. string last_absent = container_traverser->student->lastAbsent();
  289.  
  290. if (last_absent.empty())
  291. cout << "Last Absent: " << "N/A" << endl << endl;
  292. else
  293. cout << "Last Absent: " << last_absent << endl << endl;
  294.  
  295.  
  296. container_traverser = container_traverser->next;
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement