SHARE
TWEET

hw

a guest Feb 21st, 2020 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //HW3
  2. //Due: 11:59PM, February 21 (Friday)
  3.  
  4. #include <iostream>
  5. #include <list>
  6. #include <map>
  7. #include <string>
  8. #include <tuple>
  9.  
  10. using namespace std;
  11.  
  12. class course {
  13. public:
  14.     string name;
  15.     int section;
  16.     int credits;
  17.     string grade;
  18.     course() {}
  19.     course(string n, int s, int c, string g) { name = n; section = s; credits = c; grade = g; }
  20.     bool operator==(const course& cx) {
  21.         return cx.name == name;
  22.     }
  23.     bool operator<(const course& cx) {
  24.         return cx.grade < grade;
  25.     }
  26.  
  27.     int operator+(int x) {
  28.         return credits + x;
  29.     }
  30.  
  31.     //You might need to implement some overloaded operators here.
  32.  
  33. };
  34. ostream& operator<< (ostream& str, const course c) { //each ostream (output stream) is a device you can send data to, reference->so not making copy, gets the buffer and prints it
  35.     str << c.name << " " << c.section << " " << c.credits << " " << c.grade;
  36.     return str;
  37. }
  38. //Implement the following functions.
  39. //When adding a student, if the student is already in DB, then ignore the operation.
  40. //When adding a course, if the course is already in DB, then ignore the operation.
  41. //When dropping a course, if the course does not exist, then ignore the operation.
  42. //When removing a student, if the student does not exist, then ignore the operation.
  43. //All courses in a semester need to be sorted.
  44. //When dropping or adding a course, overall GPA, semester GPA, overall credits and semester credits all need to be updated.
  45.  
  46. //Semeser numbers:  Spring 2019: 20191; Fall 2019: 20192, etc.
  47. ostream& operator<<(ostream& str, const map<int, tuple<int, float, list<course*>*> >& semester) {
  48.     for (auto i : semester) {
  49.  
  50.     }
  51. }
  52.  
  53. ostream& operator<<(ostream& str, const list<course*>* classes) {
  54.  
  55.     /*for (auto i : classes) {
  56.         str << i
  57.     }*/
  58. }
  59.  
  60. void add_student(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id);
  61. void remove_student(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id);
  62. 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
  63.                                                                 //All courses in the list should be sorted according to name (increasing order)
  64. void drop_course(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id, course c);
  65. void print_student_semester_courses(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id);
  66. void print_student_all_courses(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id);
  67.  
  68. //Implement additional functions such that you can do
  69. //cout << DB << endl;
  70.  
  71. //You might need to implement some overloaded operators in the course class.
  72.  
  73.  
  74.  
  75. int main() {
  76.     map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>  DB;
  77.  
  78.     add_student(DB, 11111);
  79.     course C1("CIS554", 1, 3, "A-"), C2("CSE674", 1, 3, "B+"), C3("MAT296", 8, 4, "A"), C4("WRT205", 5, 3, "A");
  80.  
  81.     add_course(DB, 20171, 11111, C1);
  82.     add_course(DB, 20171, 11111, C4);
  83.     add_course(DB, 20171, 11111, C3);
  84.     add_course(DB, 20171, 11111, C2);
  85.     print_student_semester_courses(DB, 20171, 11111);
  86.  
  87.     drop_course(DB, 20171, 11111, C1);
  88.     print_student_semester_courses(DB, 20171, 11111); //sorted according to course name
  89.  
  90.     course C5("CIS351", 2, 3, "A-"), C6("PSY205", 5, 3, "B+"), C7("MAT331", 2, 3, "A"), C8("ECN203", 4, 3, "A");
  91.     add_course(DB, 20172, 11111, C5);
  92.     add_course(DB, 20172, 11111, C6);
  93.     add_course(DB, 20172, 11111, C7);
  94.     add_course(DB, 20172, 11111, C8);
  95.     add_course(DB, 20172, 11111, C3);
  96.     print_student_all_courses(DB, 11111);//ID GPA
  97.  
  98.     add_student(DB, 11112);
  99.     add_course(DB, 20171, 11112, C2);
  100.     add_course(DB, 20171, 11112, C5);
  101.     add_course(DB, 20171, 11112, C7);
  102.     add_course(DB, 20171, 11112, C4);
  103.     print_student_semester_courses(DB, 20171, 11112);
  104.  
  105.     add_course(DB, 20172, 11112, C8);
  106.     add_course(DB, 20172, 11112, C3);
  107.     add_course(DB, 20172, 11112, C5);
  108.     add_course(DB, 20172, 11112, C1);
  109.     print_student_semester_courses(DB, 20172, 11112);
  110.  
  111.     print_student_all_courses(DB, 11112);
  112.  
  113.     //cout << DB << endl;
  114.     remove_student(DB, 11111);
  115.     //cout << DB << endl;
  116.     getchar();
  117.     getchar();
  118.     return 0;
  119. }
  120. //map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>
  121. //
  122. void add_student(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id) {
  123.     if (DB.find(id) != DB.end())return;
  124.     tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>* overall = new tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>;
  125.     map<int, tuple<int, float, list<course*>*>>* semesterCollection = new map<int, tuple<int, float, list<course*>*>>;
  126.     get<2>(*overall) = semesterCollection;
  127.     DB[id] = overall;
  128. }
  129. void remove_student(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id) {
  130.     if (DB.find(id) != DB.end()) {
  131.         auto overall_Tuple = DB[id]; //first tuple
  132.         auto mapSemester_Map = *get<2>(*overall_Tuple); //second map
  133.  
  134.         if (mapSemester_Map.size() != 0) { //checks if semester isnt empty
  135.             for (auto i = mapSemester_Map.begin(); i != mapSemester_Map.end(); ++i) {
  136.                 auto mapSemester_Tuple = i->second;//inner mapsemester_tuple
  137.                 auto courseList = *get<2>(mapSemester_Tuple);
  138.  
  139.                 if (courseList.size() != 0) {//searching the courselist, if found
  140.                     for (auto i : courseList)delete i;
  141.                 }
  142.                 delete& courseList;
  143.             }
  144.             for (auto i = mapSemester_Map.begin(); i != mapSemester_Map.end(); ++i)delete& i;
  145.         }
  146.         delete& mapSemester_Map;
  147.         delete overall_Tuple;
  148.         DB.erase(id);
  149.  
  150.     }return;
  151. }
  152.  
  153. void add_course(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id, course c) {
  154.     if (DB.find(id) != DB.end()) {
  155.         auto overall_Tuple = DB[id]; //first tuple
  156.         auto mapSemester_Map = *get<2>(*overall_Tuple); //pointer to second map
  157.  
  158.         if (mapSemester_Map.find(semester) == mapSemester_Map.end()) { //checks if the semester is already there in the tuple
  159.  
  160.             tuple<int, float, list<course*>*> semesterTuple; //semestertuple of 2nd map
  161.             mapSemester_Map[semester] = semesterTuple;//setting 20191 value to semestertuple
  162.             auto classes = get<2>(semesterTuple); //list of classes of the semester tuple
  163.             list<course*>* courseList = new list<course*>;
  164.             courseList->push_back(new course(c));
  165.             auto credits = 0;
  166.             auto grade = 0.00;
  167.             map<string, float> gradingScale{ {"A", 4.00}, {"A-", 3.75}, {"B+", 3.33}, {"B", 3.00}, {"B-", 2.67}, {"C+", 2.33},
  168.             {"C", 2.00}, {"C-", 1.67}, {"D+", 1.33}, {"D", 1.00} };
  169.             for (auto i = courseList->begin(); i != courseList->end(); ++i) {
  170.                 credits = **i + credits;
  171.                 grade += ((gradingScale[(*i)->grade]) * (*i)->credits);
  172.             }
  173.             //can only assign address to pointers
  174.             //auto &p1 = get<2>((*get<2>(*DB[id]))[semester]);
  175.             get<0>(semesterTuple) = credits;
  176.             get<1>(semesterTuple) = grade / credits;
  177.         }
  178.         else {
  179.  
  180.             auto innerTuple = mapSemester_Map[semester]; //returns a tuple
  181.             auto& courseList = *get<2>(innerTuple);//returns a tuple of sem creds and gpa
  182.             for (auto i = courseList.begin(); i != courseList.end(); ++i) {
  183.                 if (**i == c)return;
  184.             }
  185.             courseList.push_back(new course(c));
  186.             auto credits = 0;
  187.             auto grade =  0.00;
  188.             map<string, float> gradingScale{ {"A", 4.00}, {"A-", 3.75}, {"B+", 3.33}, {"B", 3.00}, {"B-", 2.67}, {"C+", 2.33},
  189.             {"C", 2.00}, {"C-", 1.67}, {"D+", 1.33}, {"D", 1.00} };
  190.             for (auto i = courseList.begin(); i != courseList.end(); ++i) {
  191.                 credits = **i + credits;
  192.                 grade += ((gradingScale[(**i).grade]) * (**i).credits);
  193.             }
  194.             //can only assign address to pointers
  195.             //auto &p1 = get<2>((*get<2>(*DB[id]))[semester]);
  196.             get<0>(innerTuple) = credits;
  197.             get<1>(innerTuple) = grade / credits;
  198.         }
  199.         //FOR GPA and Creds get<0>(*DB[id]) = 0; get<1>(*DB[id]) = 0.0; creds, gpa respectively
  200.     }return;
  201.     //comparator sort overload the course
  202. }
  203.  
  204. void drop_course(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id, course c) {
  205.     if (DB.find(id) != DB.end()) {
  206.         auto overall_Tuple = DB[id]; //first tuple
  207.         auto mapSemester_Map = *get<2>(*overall_Tuple); //second map
  208.  
  209.         if (mapSemester_Map.find(semester) != mapSemester_Map.end()) { //checks if the semester is found
  210.  
  211.             auto mapSemester_Tuple = mapSemester_Map[semester];
  212.             auto courseList = *get<2>(mapSemester_Tuple);
  213.             auto credits = 0;
  214.             auto grade = 0.00;
  215.             for (auto i = courseList.begin(); i != courseList.end(); ++i) {
  216.                 if (**i == c)courseList.remove(*i);
  217.                 credits = **i + credits;
  218.             }
  219.             auto tupleCreds = mapSemester_Map[semester];
  220.             get<0>(tupleCreds) = credits;
  221.             get<1>(tupleCreds) = grade / credits;
  222.         }
  223.     }return;
  224. }
  225.  
  226. void print_student_semester_courses(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int semester, int id) {
  227.     if (DB.find(id) != DB.end()) {
  228.         auto overall_Tuple = DB[id];
  229.         auto mapSemester_Map = *get<2>(*overall_Tuple);
  230.  
  231.         if (mapSemester_Map.find(semester) != mapSemester_Map.end()) {
  232.             auto currentSemester = mapSemester_Map[semester];
  233.             auto courseList = get<2>(currentSemester);
  234.             auto gpa = get<0>(currentSemester);
  235.             auto credits = get<1>(currentSemester);
  236.             cout << "ID: " << id << endl;
  237.             cout << "Semester: " << semester << endl;
  238.             cout << "GPA: " << gpa << endl;
  239.             //cout << "Credits: " << credits << endl;
  240.             auto i = 0;
  241.             for (auto i = courseList.begin(); i != courseList.end(); ++i)cout << courseList[i] << endl;
  242.         }
  243.  
  244.     }
  245. }
  246. void print_student_all_courses(map<int, tuple<int, float, map<int, tuple<int, float, list<course*>*> >*>*>& DB, int id) {
  247.     if (DB.find(id) != DB.end()) {
  248.         auto overall_Tuple = DB[id];
  249.         auto mapSemester_Map = *get<2>(*overall_Tuple);
  250.         //print overall gpa
  251.         //print overall credits
  252.         cout << "DB: " << endl;
  253.         cout << "Overall Credits: " << endl;
  254.         int credits = 0;
  255.         auto i = 0;
  256.         while (i != mapSemester_Map.size()) {
  257.             auto eachCredits = get<0>(mapSemester_Map[i]);
  258.             credits = credits + eachCredits;
  259.             ++i;
  260.         }
  261.         cout << "Overall GPA: " << endl;
  262.  
  263.  
  264.         for (auto i : mapSemester_Map) {
  265.             print_student_semester_courses(DB, i.first, id);
  266.         }
  267.         //print each semester
  268.         //for (auto i = mapSemester_Map.begin(); i != mapSemester_Map->end(); ++i) {
  269.             //mapSemester_Map.
  270.  
  271.         //}
  272.     }
  273. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top