Advertisement
Guest User

hw

a guest
Feb 19th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. //HW3
  2. //Due: 11:59PM, February 21 (Friday)
  3.  
  4. #include <iostream>
  5. #include <list>
  6. #include <map>
  7. #include <string>
  8. using namespace std;
  9.  
  10. class course {
  11. public:
  12. string name;
  13. int section;
  14. int credits;
  15. string grade;
  16. course() {}
  17. course(string n, int s, int c, string g) { name = n; section = s; credits = c; grade = g; }
  18.  
  19. //You might need to implement some overloaded operators here.
  20.  
  21. };
  22. //Implement the following functions.
  23. //When adding a student, if the student is already in DB, then ignore the operation.
  24. //When adding a course, if the course is already in DB, then ignore the operation.
  25. //When dropping a course, if the course does not exist, then ignore the operation.
  26. //When removing a student, if the student does not exist, then ignore the operation.
  27. //All courses in a semester need to be sorted.
  28. //When dropping or adding a course, overall GPA, semester GPA, overall credits and semester credits all need to be updated.
  29.  
  30. //Semeser numbers: Spring 2019: 20191; Fall 2019: 20192, etc.
  31.  
  32. void add_student(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id);
  33. void remove_student(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id);
  34. void add_course(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id, course c); //20171 Spring semester of 2017; 20172: Fall semester of 2017
  35. //All courses in the list should be sorted according to name (increasing order)
  36. void drop_course(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id, course c);
  37. void print_student_semester_courses(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id);
  38. void print_student_all_courses(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id);
  39.  
  40. //Implement additional functions such that you can do
  41. //cout << DB << endl;
  42.  
  43. //You might need to implement some overloaded operators in the course class.
  44.  
  45. int main() {
  46. map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*> DB;
  47.  
  48. add_student(DB, 11111);
  49. course C1("CIS554", 1, 3, "A-"), C2("CSE674", 1, 3, "B+"), C3("MAT296", 8, 4, "A"), C4("WRT205", 5, 3, "A");
  50.  
  51. add_course(DB, 20171, 11111, C1);
  52. add_course(DB, 20171, 11111, C4);
  53. add_course(DB, 20171, 11111, C3);
  54. add_course(DB, 20171, 11111, C2);
  55. print_student_semester_courses(DB, 20171, 11111);
  56.  
  57. drop_course(DB, 20171, 11111, C1);
  58. print_student_semester_courses(DB, 20171, 11111); //sorted according to course name
  59.  
  60. course C5("CIS351", 2, 3, "A-"), C6("PSY205", 5, 3, "B+"), C7("MAT331", 2, 3, "A"), C8("ECN203", 4, 3, "A");
  61. add_course(DB, 20172, 11111, C5);
  62. add_course(DB, 20172, 11111, C6);
  63. add_course(DB, 20172, 11111, C7);
  64. add_course(DB, 20172, 11111, C8);
  65. add_course(DB, 20172, 11111, C3);
  66. print_student_all_courses(DB, 11111);//ID GPA
  67.  
  68. add_student(DB, 11112);
  69. add_course(DB, 20171, 11112, C2);
  70. add_course(DB, 20171, 11112, C5);
  71. add_course(DB, 20171, 11112, C7);
  72. add_course(DB, 20171, 11112, C4);
  73. print_student_semester_courses(DB, 20171, 11112);
  74.  
  75. add_course(DB, 20172, 11112, C8);
  76. add_course(DB, 20172, 11112, C3);
  77. add_course(DB, 20172, 11112, C5);
  78. add_course(DB, 20172, 11112, C1);
  79. print_student_semester_courses(DB, 20172, 11112);
  80.  
  81. print_student_all_courses(DB, 11112);
  82.  
  83. cout << DB << endl;
  84. remove_student(DB, 11111);
  85. cout << DB << endl;
  86. getchar();
  87. getchar();
  88. return 0;
  89. }
  90. //map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>
  91. //
  92. void add_student(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id) {
  93. if (DB.find(id) != DB.end())return;
  94. tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>* overall = new tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>;
  95. map<int, tuple<int, float, list<course*>*>>* semesterCollection = new map<int, tuple<int, float, list<course*>*>>;
  96. get<2>(*overall) = semesterCollection;
  97. DB[id] = overall;
  98. }
  99. void remove_student(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id) {
  100. if (DB.find(id) != DB.end()) {
  101. auto overall_Tuple = DB[id]; //first tuple
  102. auto mapSemester_Map = get<2>(*overall_Tuple); //second map
  103.  
  104. if (mapSemester_Map.size() != 0) { //checks if semester isnt empty
  105.  
  106. auto mapSemester_Tuple = mapSemester_Map[semester];
  107. auto courseList = get<2>(*mapSemester_Tuple);
  108.  
  109. if (courseList.size() != 0) {//searching the courselist, if found
  110. courseList.clear();
  111. }
  112. else {
  113. return;
  114. }
  115.  
  116. }
  117. else { return; }
  118.  
  119.  
  120. }return;
  121. }
  122.  
  123. void add_course(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id, course c) {
  124. if (DB.find(id) != DB.end()) {
  125. auto overall_Tuple = DB[id]; //first tuple
  126. auto mapSemester_Map = get<2>(*overall_Tuple); //second map
  127.  
  128. if (mapSemester_Map.find(semester) == mapSemester_Map.end()) { //checks if the semester is already there in the tuple
  129.  
  130. tuple<int, float, list<course*>*>* semesterTuple = new tuple<int, float, list<course*>*>; //semestertuple of 2nd map
  131. mapSemester_Map->insert(semester, semesterTuple);//setting 20191 value to semestertuple
  132. auto classes = get<2>(*semesterTuple); //list of classes of the semester tuple
  133.  
  134. if (classes->find(course) == classes->end()) {
  135.  
  136. list<course*>* courseList = new list<course*>*;
  137. courseList->insert(course);
  138. }
  139. else {
  140. return;
  141. }
  142. }
  143. else {
  144. auto existingSem = mapSemester.find(semester);
  145. if (existingSem->find(course) == existingSem->end()) {
  146. courseList->insert(course);
  147.  
  148. }
  149. else {
  150. return;
  151. }
  152. }
  153.  
  154. }return;
  155. //comparator sort overload the course
  156. }
  157.  
  158. void drop_course(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id, course c) {
  159. if (DB.find(id) != DB.end()) {
  160. auto overall_Tuple = DB[id]; //first tuple
  161. auto mapSemester_Map = get<2>(*overall_Tuple); //second map
  162.  
  163. if (mapSemester_Map.find(semester) != mapSemester_Map.end()) { //checks if the semester is found
  164.  
  165. auto mapSemester_Tuple = mapSemester_Map[semester];
  166. auto courseList = get<2>(*mapSemester_Tuple);
  167.  
  168. if (courseList.find(course) != courseList.end()) {//searching the courselist, if found
  169. courseList.remove(course);
  170. }
  171. else {
  172. return;
  173. }
  174. }
  175. else { return; }
  176.  
  177.  
  178. }return;
  179. }
  180.  
  181. void print_student_semester_courses(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id) {
  182.  
  183. }
  184. void print_student_all_courses(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id) {
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement