Advertisement
Zuhairy_Harry

AA Project

Jan 10th, 2024
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. struct Student {
  12.     int id, age, roomFloor;
  13.     string name, state, department, fees;
  14.  
  15. };
  16.  
  17. // Function prototypes
  18. void customSwap(Student& a, Student& b, int& swapCount);
  19. int partition(vector<Student>& students, int low, int high, int& swapCount);
  20. void quickSort(vector<Student>& students, int low, int high, int& swapCount);
  21. void loadStudents(const string& filename, vector<Student>& students);
  22. int binarySearch(const vector<Student>& students, int l, int r, int x);
  23. int linearSearch(const vector<Student>& students, int x);
  24. void displayStudents(const vector<Student>& students, int displayLimit);
  25. void merge(vector<Student>& students, int low, int mid, int high, int& swapCount);
  26. void mergeSort(vector<Student>& students, int low, int high, int& swapCount);
  27. double calculateAverageAge(const vector<Student>& students);
  28. int calculateTotalDept(const vector<Student>& students);
  29.  
  30. string trim(const string& str);
  31.  
  32. // Function implementations
  33. string trim(const string& str) {
  34.     size_t first = str.find_first_not_of(" \t\"");
  35.     if (first == string::npos) return "";
  36.     size_t last = str.find_last_not_of(" \t\"");
  37.     return str.substr(first, (last - first + 1));
  38. }
  39.  
  40.  
  41. int partition(vector<Student>& students, int low, int high, int& swapCount) {
  42.     int pivot = students[high].id;
  43.     int i = low - 1;
  44.  
  45.     for (int j = low; j < high; j++) {
  46.         if (students[j].id < pivot) {
  47.             i++;
  48.             customSwap(students[i], students[j], swapCount);
  49.         }
  50.     }
  51.     customSwap(students[i + 1], students[high], swapCount);
  52.     return i + 1;
  53. }
  54.  
  55.  
  56.  
  57.  
  58. /*
  59. -----------------------------------------------------------------------------------
  60.     QUICK SORT FUNCTION
  61. -----------------------------------------------------------------------------------
  62. */
  63. void quickSort(vector<Student>& students, int low, int high, int& swapCount) {
  64.     if (low < high) {
  65.         int pi = partition(students, low, high, swapCount);
  66.         quickSort(students, low, pi - 1, swapCount);
  67.         quickSort(students, pi + 1, high, swapCount);
  68.     }
  69. }
  70.  
  71.  
  72.  
  73.  
  74. /*
  75. -----------------------------------------------------------------------------------
  76.     LOAD FILE FUNCTION
  77. -----------------------------------------------------------------------------------
  78. */
  79. void loadStudents(const string& filename, vector<Student>& students) {
  80.     ifstream ip(filename);
  81.     if (!ip.is_open()) {
  82.         cerr << "Error: File could not be opened" << '\n';
  83.         return;
  84.     }
  85.  
  86.     string line;
  87.     getline(ip, line); // Skip the header if present
  88.  
  89.     while (getline(ip, line)) {
  90.         stringstream ss(line);
  91.         Student tempStudent;
  92.         string tempId, tempAge, tempRoomFloor;
  93.  
  94.         getline(ss, tempId, ',');
  95.         getline(ss, tempStudent.name, ',');
  96.         getline(ss, tempAge, ',');
  97.         getline(ss, tempStudent.state, ',');
  98.         getline(ss, tempStudent.department, ',');
  99.         getline(ss, tempRoomFloor, ',');
  100.         getline(ss, tempStudent.fees, '\n');
  101.  
  102.         tempStudent.id = stoi(trim(tempId));
  103.         tempStudent.age = stoi(trim(tempAge));
  104.         tempStudent.roomFloor = stoi(trim(tempRoomFloor));
  105.         tempStudent.name = trim(tempStudent.name);
  106.         tempStudent.state = trim(tempStudent.state);
  107.         tempStudent.department = trim(tempStudent.department);
  108.         tempStudent.fees = trim(tempStudent.fees);
  109.  
  110.         students.push_back(tempStudent); // Add the new student to the vector
  111.     }
  112.  
  113.     ip.close();
  114. }
  115.  
  116.  
  117.  
  118.  
  119. /*
  120. -----------------------------------------------------------------------------------
  121.     MERGE FUNCTION
  122. -----------------------------------------------------------------------------------
  123. */
  124. void merge(vector<Student>& students, int low, int mid, int high, int& swapCount) {
  125.     int n1 = mid - low + 1;
  126.     int n2 = high - mid;
  127.  
  128.     vector<Student> left(n1), right(n2);
  129.  
  130.     for (int i = 0; i < n1; i++)
  131.         left[i] = students[low + i];
  132.     for (int j = 0; j < n2; j++)
  133.         right[j] = students[mid + 1 + j];
  134.  
  135.     int i = 0, j = 0, k = low;
  136.  
  137.     while (i < n1 && j < n2) {
  138.         if (left[i].id <= right[j].id) {
  139.             students[k++] = left[i++];
  140.         }
  141.         else {
  142.             students[k++] = right[j++];
  143.             swapCount += n1 - i; // Counting swaps when elements are moved from left to right
  144.         }
  145.     }
  146.  
  147.     while (i < n1) {
  148.         students[k++] = left[i++];
  149.     }
  150.     while (j < n2) {
  151.         students[k++] = right[j++];
  152.     }
  153.  
  154. }
  155.  
  156.  
  157.  
  158.  
  159. /*
  160. -----------------------------------------------------------------------------------
  161.     MERGE SORT FUNCTION
  162. -----------------------------------------------------------------------------------
  163. */
  164. void mergeSort(vector<Student>& students, int low, int high, int& swapCount) {
  165.     if (low < high) {
  166.         int mid = low + (high - low) / 2;
  167.  
  168.         mergeSort(students, low, mid, swapCount);
  169.         mergeSort(students, mid + 1, high, swapCount);
  170.  
  171.         merge(students, low, mid, high, swapCount);
  172.     }
  173.  
  174.     //cout << "Total Swaps = " << swapCount << endl;
  175. }
  176.  
  177.  
  178.  
  179.  
  180. /*
  181. -----------------------------------------------------------------------------------
  182.     CALCULATE TOTAL STUDENT FROM DEPT FUNCTION
  183. -----------------------------------------------------------------------------------
  184. */
  185. int calculateTotalDept(const vector<Student>& students) {
  186.     if (students.empty()) {
  187.         return 0.0; // Return 0 if there are no students
  188.     }
  189.  
  190.     int ftmkDept = 0;
  191.     int ftkekDept = 0;
  192.     int fpttDept = 0;
  193.     for (const auto& student : students) {
  194.         if (student.department == "FTMK") {
  195.             ftmkDept++;
  196.         }
  197.         else if (student.department == "FTKEK") {
  198.             ftkekDept++;
  199.         }
  200.         else if (student.department == "FPTT") {
  201.             fpttDept++;
  202.         }
  203.     }
  204.  
  205.     cout << "\nTotal student for FTMK department = " << ftmkDept << endl;
  206.     cout << "Total student for FTKEK department = " << ftkekDept << endl;
  207.     cout << "Total student for FPTT department = " << fpttDept << "\n" << endl;
  208.  
  209.     return ftmkDept, ftkekDept, fpttDept;
  210. }
  211.  
  212.  
  213.  
  214.  
  215. /*
  216. -----------------------------------------------------------------------------------
  217.     CALCULATE AVERAGE AGE FUNCTION
  218. -----------------------------------------------------------------------------------
  219. */
  220. double calculateAverageAge(const vector<Student>& students) {
  221.     if (students.empty()) {
  222.         return 0.0; // Return 0 if there are no students
  223.     }
  224.  
  225.     int totalAge = 0;
  226.     for (const auto& student : students) {
  227.         totalAge += student.age;
  228.     }
  229.  
  230.     return static_cast<double>(totalAge) / students.size();
  231. }
  232.  
  233.  
  234.  
  235.  
  236. /*
  237. -----------------------------------------------------------------------------------
  238.     BINARY SEARCH FUNCTION
  239. -----------------------------------------------------------------------------------
  240. */
  241. int binarySearch(const Student students[], int l, int r, int x) {
  242.     if (r >= l) {
  243.         int mid = l + (r - l) / 2;
  244.  
  245.         if (students[mid].id == x)
  246.             return mid;
  247.  
  248.         if (students[mid].id > x)
  249.             return binarySearch(students, l, mid - 1, x);
  250.  
  251.         return binarySearch(students, mid + 1, r, x);
  252.     }
  253.  
  254.     return -1;
  255. }
  256.  
  257.  
  258.  
  259.  
  260. /*
  261. -----------------------------------------------------------------------------------
  262.     LINEAR SEARCH FUNCTION
  263. -----------------------------------------------------------------------------------
  264. */
  265. int linearSearch(const Student students[], int n, int x) {
  266.     for (int i = 0; i < n; i++) {
  267.         if (students[i].id == x) {
  268.             return i;
  269.         }
  270.     }
  271.     return -1;
  272. }
  273.  
  274.  
  275.  
  276.  
  277. /*
  278. -----------------------------------------------------------------------------------
  279.     SWAP FUNCTION
  280. -----------------------------------------------------------------------------------
  281. */
  282. void customSwap(Student& a, Student& b, int& swapCount) {
  283.     swap(a, b);  // Swaps the entire Student objects
  284.     swapCount++;
  285. }
  286.  
  287.  
  288.  
  289.  
  290. /*
  291. -----------------------------------------------------------------------------------
  292.     MERGE SWAP FUNCTION
  293. -----------------------------------------------------------------------------------
  294. */
  295. int binarySearch(const vector<Student>& students, int l, int r, int x) {
  296.     while (l <= r) {
  297.         int mid = l + (r - l) / 2;
  298.         if (students[mid].id == x) {
  299.             return mid;
  300.         }
  301.         else if (students[mid].id < x) {
  302.             l = mid + 1;
  303.         }
  304.         else {
  305.             r = mid - 1;
  306.         }
  307.     }
  308.     return -1;
  309. }
  310.  
  311.  
  312.  
  313.  
  314. /*
  315. -----------------------------------------------------------------------------------
  316.     LINEAR SEARCH FUNCTION
  317. -----------------------------------------------------------------------------------
  318. */
  319. int linearSearch(const vector<Student>& students, int x) {
  320.     for (int i = 0; i < students.size(); ++i) {
  321.         if (students[i].id == x) {
  322.             return i;
  323.         }
  324.     }
  325.     return -1;
  326. }
  327.  
  328.  
  329.  
  330.  
  331. /*
  332. -----------------------------------------------------------------------------------
  333.     DISPLAY DATA FUNCTION
  334. -----------------------------------------------------------------------------------
  335. */
  336. void displayStudents(const vector<Student>& students, int displayLimit) {
  337.  
  338.     const int fieldWidth = 20;
  339.     cout << "---------------------------------------------------------------------------------------------------------" << endl;
  340.     cout << "ID\t|Name\t\t\t|Age\t|State\t\t|Department\t|Room Floor\t|Fees\t\t|" << endl;
  341.     /*cout << "ID" << string(fieldWidth - 2, ' ') << "|Name" << string(fieldWidth - 4, ' ')
  342.         << "|Age" << string(fieldWidth - 2, ' ') << "|State" << string(fieldWidth - 6, ' ')
  343.         << "|Department" << string(fieldWidth - 10, ' ') << "|Room Floor" << string(fieldWidth - 10, ' ')
  344.         << "|Fees" << string(fieldWidth - 1, ' ') << "|" << endl;*/
  345.     cout << "---------------------------------------------------------------------------------------------------------" << endl;
  346.     int count = 0;
  347.     for (const auto& student : students) {
  348.  
  349.         if (count >= displayLimit) break;
  350.  
  351.         if (student.name.length() >= 15) {
  352.             if (student.state.length() > 5) {
  353.                 if (student.fees.length() > 5) {
  354.                     cout << student.id << "\t|" << student.name << "\t|" << student.age << "\t|" << student.state << "\t|" << student.department << "\t\t|";
  355.                     cout << student.roomFloor << "\t\t|" << student.fees << "\t|" << endl;
  356.                 }
  357.                 else {
  358.                     cout << student.id << "\t|" << student.name << "\t|" << student.age << "\t|" << student.state << "\t|" << student.department << "\t\t|";
  359.                     cout << student.roomFloor << "\t\t|" << student.fees << "\t\t|" << endl;
  360.                 }
  361.  
  362.             }
  363.             else {
  364.  
  365.                 if (student.fees.length() > 5) {
  366.                     cout << student.id << "\t|" << student.name << "\t|" << student.age << "\t|" << student.state << "\t\t|" << student.department << "\t\t|";
  367.                     cout << student.roomFloor << "\t\t|" << student.fees << "\t|" << endl;
  368.                 }
  369.                 else {
  370.                     cout << student.id << "\t|" << student.name << "\t|" << student.age << "\t|" << student.state << "\t\t|" << student.department << "\t\t|";
  371.                     cout << student.roomFloor << "\t\t|" << student.fees << "\t\t|" << endl;
  372.                 }
  373.             }
  374.         }
  375.         else {
  376.             if (student.state.length() > 5) {
  377.  
  378.                 if (student.fees.length() > 5) {
  379.                     cout << student.id << "\t|" << student.name << "\t\t|" << student.age << "\t|" << student.state << "\t|" << student.department << "\t\t|";
  380.                     cout << student.roomFloor << "\t\t|" << student.fees << "\t|" << endl;
  381.                 }
  382.                 else {
  383.                     cout << student.id << "\t|" << student.name << "\t\t|" << student.age << "\t|" << student.state << "\t|" << student.department << "\t\t|";
  384.                     cout << student.roomFloor << "\t\t|" << student.fees << "\t\t|" << endl;
  385.                 }
  386.             }
  387.             else {
  388.                 if (student.fees.length() > 5) {
  389.                     cout << student.id << "\t|" << student.name << "\t\t|" << student.age << "\t|" << student.state << "\t\t|" << student.department << "\t\t|";
  390.                     cout << student.roomFloor << "\t\t|" << student.fees << "\t|" << endl;
  391.                 }
  392.                 else {
  393.                     cout << student.id << "\t|" << student.name << "\t\t|" << student.age << "\t|" << student.state << "\t\t|" << student.department << "\t\t|";
  394.                     cout << student.roomFloor << "\t\t|" << student.fees << "\t\t|" << endl;
  395.                 }
  396.             }
  397.         }
  398.  
  399.  
  400.         /* cout << student.id << string(fieldWidth - to_string(student.id).length(), ' ')
  401.              << "|" << student.name << string(fieldWidth - student.name.length(), ' ')
  402.              << "|" << student.age << string(fieldWidth - to_string(student.age).length(), ' ')
  403.              << "|" << student.state << string(fieldWidth - student.state.length(), ' ')
  404.              << "|" << student.department << string(fieldWidth - student.department.length(), ' ')
  405.              << "|" << student.roomFloor << string(fieldWidth - to_string(student.roomFloor).length(), ' ')
  406.              << "|" << student.fees << string(fieldWidth - student.fees.length(), ' ') << "|" << endl;*/
  407.  
  408.         count++;
  409.     }
  410.     cout << "---------------------------------------------------------------------------------------------------------" << endl;
  411. }
  412.  
  413.  
  414.  
  415.  
  416. /*
  417. -----------------------------------------------------------------------------------
  418.     MAIN FUNCTION
  419. -----------------------------------------------------------------------------------
  420. */
  421.  
  422. int main() {
  423.     vector<Student> students;
  424.     loadStudents("output2.csv", students);
  425.     int swapCount = 0;
  426.     int numStudents = 0;
  427.  
  428.     int choice;
  429.     do {
  430.         cout << "----------------------------------------------------------------------------------" << endl;
  431.         cout << "SATRIA MANAGEMENT SYSTEM" << endl;
  432.         cout << "1) Unsort Data" << endl;
  433.         cout << "2) Sorting" << endl;
  434.         cout << "3) Search" << endl;
  435.         cout << "4) Additional Features" << endl;
  436.         cout << "5) Exit" << endl;
  437.         cout << "----------------------------------------------------------------------------------" << endl;
  438.         cout << "\nEnter choice: ";
  439.         cin >> choice;
  440.  
  441.         switch (choice) {
  442.         case 1: {
  443.             cout << "Unsorted Data" << endl;
  444.             displayStudents(students, min(100, static_cast<int>(students.size())));
  445.             main();
  446.             break;
  447.         }
  448.         case 2: {
  449.             int sortChoice = 0;
  450.             string sortChoiceWord = "";
  451.             do {
  452.                 cout << "----------------------------------------------------------------------------------" << endl;
  453.                 cout << "Sorting Menu" << endl;
  454.                 cout << "1) Quick Sort" << endl;
  455.                 cout << "2) Merge Sort" << endl;
  456.                 cout << "3) Back" << endl;
  457.                 cout << "----------------------------------------------------------------------------------" << endl;
  458.                 cout << "\nEnter choice: ";
  459.                 cin >> sortChoice;
  460.  
  461.                 swapCount = 0; // Reset swap count before sorting
  462.                 switch (sortChoice) {
  463.                 case 1:
  464.                     cout << "\nStudents sorted using Quick Sort!" << endl;
  465.                     quickSort(students, 0, students.size() - 1, swapCount); // Pass swapCount here
  466.                     displayStudents(students, min(100, static_cast<int>(students.size())));
  467.                     cout << "Total swaps: " << swapCount << "\n" << endl;
  468.                     main();
  469.                     break;
  470.                 case 2:
  471.                     cout << "\nStudents sorted using Merge Sort!" << endl;
  472.                     mergeSort(students, 0, students.size() - 1, swapCount);
  473.                     displayStudents(students, min(100, static_cast<int>(students.size())));
  474.                     cout << "Total swaps: " << swapCount << "\n" << endl;
  475.                     main();
  476.                     break;
  477.                 case 3:
  478.                     main();
  479.                 default:
  480.                     if (!cin.fail()) {
  481.                         cout << "Please input a valid number!" << "\n" << endl;
  482.                         sortChoiceWord = to_string(sortChoice);
  483.                     }
  484.                     else {
  485.                         cin.clear();
  486.                         cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  487.                         cout << "Invalid input. Please enter an integer!" << "\n" << endl;
  488.                         sortChoiceWord = to_string(sortChoice);
  489.                     }
  490.                 }
  491.  
  492.             } while ((sortChoiceWord != "1") && (sortChoiceWord != "2") && (sortChoiceWord != "3"));
  493.  
  494.  
  495.         }
  496.         case 3: {
  497.             int searchChoice = 0;
  498.             string searchChoiceWord = "";
  499.             do {
  500.                 cout << "----------------------------------------------------------------------------------" << endl;
  501.                 cout << "Search Menu" << endl;
  502.                 cout << "1) Binary Search" << endl;
  503.                 cout << "2) Linear Search" << endl;
  504.                 cout << "3) Back" << endl;
  505.                 cout << "----------------------------------------------------------------------------------" << endl;
  506.                 cout << "Enter choice: ";
  507.                 cin >> searchChoice;
  508.  
  509.  
  510.                 int index = -1;
  511.                 if (searchChoice == 1) {
  512.                     cout << "Enter ID to search: ";
  513.                     int idToSearch;
  514.                     cin >> idToSearch;
  515.  
  516.                     // Make sure students are sorted before binary search
  517.                     quickSort(students, 0, students.size() - 1, swapCount);
  518.                     index = binarySearch(students, 0, students.size() - 1, idToSearch);
  519.  
  520.                     if (index != -1) {
  521.                         cout << "\nStudent found: " << endl;
  522.                         cout << "ID: " << students[index].id << '\n';
  523.                         cout << "Name: " << students[index].name << '\n';
  524.                         cout << "Age: " << students[index].age << '\n';
  525.                         cout << "Department: " << students[index].department << "\n" << endl;
  526.                         // ... Print other details ...
  527.                     }
  528.                     else {
  529.                         cout << "Student not found." << endl;
  530.                     }
  531.                     main();
  532.                 }
  533.                 else if (searchChoice == 2) {
  534.                     cout << "Enter ID to search: ";
  535.                     int idToSearch;
  536.                     cin >> idToSearch;
  537.  
  538.                     index = linearSearch(students, idToSearch);
  539.  
  540.                     if (index != -1) {
  541.                         cout << "\nStudent found: " << endl;
  542.                         cout << "ID: " << students[index].id << '\n';
  543.                         cout << "Name: " << students[index].name << '\n';
  544.                         cout << "Age: " << students[index].age << '\n';
  545.                         cout << "Department: " << students[index].department << "\n" << endl;
  546.                         // ... Print other details ...
  547.                     }
  548.                     else {
  549.                         cout << "Student not found." << endl;
  550.                     }
  551.                     main();
  552.                 }
  553.                 else if (searchChoice == 3) {
  554.                     main();
  555.                 }
  556.                 else {
  557.                     if (!cin.fail()) {
  558.                         cout << "Please input a valid number!" << "\n" << endl;
  559.                         searchChoiceWord = to_string(searchChoice);
  560.                     }
  561.                     else {
  562.                         cin.clear();
  563.                         cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  564.                         cout << "Invalid input. Please enter an integer!" << "\n" << endl;
  565.                         searchChoiceWord = to_string(searchChoice);
  566.                     }
  567.                 }
  568.             } while ((searchChoiceWord != "1") && (searchChoiceWord != "2") && (searchChoiceWord != "3"));
  569.  
  570.  
  571.         }
  572.         case 4: {
  573.             int searchChoice = 0;
  574.             string searchChoiceWord = "";
  575.  
  576.             do {
  577.                 cout << "\n----------------------------------------------------------------------------------" << endl;
  578.                 cout << "Additional Features Menu" << endl;
  579.                 cout << "1) Calculate Age" << endl;
  580.                 cout << "2) Total Student from each department" << endl;
  581.                 cout << "3) Back" << endl;
  582.                 cout << "----------------------------------------------------------------------------------" << endl;
  583.                 cout << "Enter choice: ";
  584.                 cin >> searchChoice;
  585.  
  586.                 switch (searchChoice)
  587.                 {
  588.                 case 1: {
  589.                     // Inside your main function, possibly at the end or wherever you find appropriate
  590.                     double averageAge = calculateAverageAge(students);
  591.                     cout << "\nAverage age of students: " << averageAge << "\n" << endl;
  592.                     main();
  593.                     break;
  594.                 }
  595.                 case 2: {
  596.                     calculateTotalDept(students);
  597.                     main();
  598.                     break;
  599.                 }
  600.                 case 3: {
  601.                     main();
  602.                 }
  603.                 default:
  604.                     if (!cin.fail()) {
  605.                         cout << "Please input a valid number!" << "\n" << endl;
  606.                         searchChoiceWord = to_string(searchChoice);
  607.                     }
  608.                     else {
  609.                         cin.clear();
  610.                         cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  611.                         cout << "Invalid input. Please enter an integer!" << "\n" << endl;
  612.                         searchChoiceWord = to_string(searchChoice);
  613.                     }
  614.                 }
  615.             } while ((searchChoiceWord != "1") && (searchChoiceWord != "2") && (searchChoiceWord != "3"));
  616.  
  617.  
  618.         }
  619.         case 5: {
  620.             break;
  621.         }
  622.         default:
  623.             if (!cin.fail()) {
  624.                 cout << "Please input a valid number!" << "\n" << endl;
  625.                 main();
  626.             }
  627.             else {
  628.                 cin.clear();
  629.                 cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  630.                 cout << "Invalid input. Please enter an integer!" << "\n" << endl;
  631.                 main();
  632.             }
  633.  
  634.         }
  635.     } while (choice == 1 || choice == 2);
  636.  
  637. }
  638.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement