Advertisement
informaticage

CPP Permuation of school lessons

Jun 1st, 2021
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. struct lesson {
  6.   std::string name;
  7.   unsigned int length;
  8. };
  9.  
  10. int compare_lessons(const lesson &left, const lesson &right) {
  11.   return right.name.compare(left.name) < 0;
  12. }
  13.  
  14. void print_vector(std::vector<lesson> lessons, int week = 1) {
  15.   std::cout << "Week " << week << ": " << std::endl;
  16.  
  17.   std::string days[] = {
  18.     "Monday",
  19.     "Tuesday",
  20.     "Wednesday",
  21.     "Thursday",
  22.     "Friday",
  23.     "Saturday",
  24.     "Sunday"
  25.   };
  26.  
  27.   int day = 0;
  28.  
  29.   for (size_t i = 0; i < lessons.size(); i++) {
  30.     std::cout << days[(day) % 7] << std::endl;
  31.     std::cout << lessons[i].name << std::endl;
  32.     i++;
  33.     std::cout << lessons[i].name << std::endl;
  34.     day++;
  35.   }
  36.   std::cout << std::endl;
  37. }
  38.  
  39. int main() {
  40.   std::vector<lesson> lessons;
  41.  
  42.   lesson temp;
  43.   do {
  44.     std::cout << "Subject name: ";
  45.     std::cin >> temp.name;
  46.  
  47.     if (temp.name != "exit") {
  48.       lessons.push_back(temp);
  49.     }
  50.   } while (temp.name != "exit");
  51.  
  52.   std::sort(lessons.begin(), lessons.end(), compare_lessons);
  53.  
  54.   int week = 1;
  55.   do {
  56.     print_vector(lessons, week++);
  57.   } while (
  58.       std::next_permutation(lessons.begin(), lessons.end(), compare_lessons));
  59.  
  60.   return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement