Advertisement
RobertDeMilo

BB5.3 Какими должны быть функции? Часть 1

Jun 21st, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. // #include <iostream>
  2.  
  3. // void Process()
  4. // {
  5. //     string query_type;
  6. //     cin>>query_type;
  7. //     int start,finish;
  8. //     cin>>start>>finish;
  9.    
  10. //     if(query_type=="ADD")
  11. //     {
  12. //         routes.AddRoute (start,finish);
  13.        
  14. //     }
  15. //     else if(query_type=="GO")
  16. //     {
  17. //         cout<<routes.FindNearestFinish(start,finish)<<"\n";
  18. //     }
  19. // }
  20.  
  21. // int main()
  22. // {
  23.  
  24. // int query_count;
  25.  
  26. // cin>>query_count;
  27.  
  28. // for(int query_id =0;query_id<query_count;++query_id)
  29. // {
  30. //     Process();
  31. // }
  32. //     return 0;
  33. // }
  34.  
  35. #include <algorithm>
  36. #include <map>
  37. #include <string>
  38. #include <tuple>
  39. #include <vector>
  40.  
  41. using namespace std;
  42.  
  43. // Перечислимый тип для статуса задачи
  44. enum class TaskStatus {
  45.     NEW,          // новая
  46.     IN_PROGRESS,  // в разработке
  47.     TESTING,      // на тестировании
  48.     DONE          // завершена
  49. };
  50.  
  51.  
  52. using TasksInfo = map<TaskStatus, int>;
  53.  
  54.  
  55. TaskStatus Next(TaskStatus task_status)
  56. {
  57.       return static_cast<TaskStatus>(static_cast<int>(task_status) + 1);
  58. }
  59.  
  60.  
  61. using TasksInfo = map<TaskStatus, int>;
  62.  
  63. class TeamTasks {
  64. public:
  65.  
  66.       const TasksInfo& GetPersonTasksInfo(const string& person) const;
  67.    
  68.  
  69.       void AddNewTask(const string& person);
  70.    
  71.  
  72.       tuple<TasksInfo, TasksInfo> PerformPersonTasks(const string& person, int task_count);
  73.  
  74. private:
  75.       map<string, TasksInfo> person_tasks_;
  76. };
  77.  
  78. const TasksInfo& TeamTasks::GetPersonTasksInfo(const string& person) const
  79. {
  80.       return person_tasks_.at(person);
  81. }
  82.  
  83. void TeamTasks::AddNewTask(const string& person) {
  84.       ++person_tasks_[person][TaskStatus::NEW];
  85. }
  86.  
  87.  
  88. void RemoveZeros(TasksInfo& tasks_info) {
  89.  
  90.       vector<TaskStatus> statuses_to_remove;
  91.       for (const auto& task_item : tasks_info) {
  92.             if (task_item.second == 0) {
  93.                   statuses_to_remove.push_back(task_item.first);
  94.             }
  95.       }
  96.       for (const TaskStatus status : statuses_to_remove) {
  97.             tasks_info.erase(status);
  98.       }
  99. }
  100.  
  101.  
  102. tuple<TasksInfo, TasksInfo> TeamTasks::PerformPersonTasks(const string& person, int task_count) {
  103.    
  104.       TasksInfo updated_tasks, untouched_tasks;
  105.      
  106.       const bool has_person = person_tasks_.count(person);////????????????????????????
  107.      
  108.    
  109.       TasksInfo& tasks = person_tasks_[person];
  110.    
  111.    
  112.       for (TaskStatus status = TaskStatus::NEW;status != TaskStatus::DONE;status = Next(status))
  113.       {
  114.             // Считаем обновлённые
  115.             updated_tasks[Next(status)] = min(task_count, tasks[status]);
  116.             // Считаем, сколько осталось обновить
  117.             task_count -= updated_tasks[Next(status)];
  118.       }
  119.    
  120.    
  121.       for (TaskStatus status = TaskStatus::NEW;
  122.            status != TaskStatus::DONE;
  123.            status = Next(status)) {
  124.             untouched_tasks[status] = tasks[status] - updated_tasks[Next(status)];
  125.             tasks[status] += updated_tasks[status] - updated_tasks[Next(status)];
  126.       }
  127.      
  128.       tasks[TaskStatus::DONE] += updated_tasks[TaskStatus::DONE];
  129.    
  130.      
  131.       RemoveZeros(updated_tasks);
  132.       RemoveZeros(untouched_tasks);
  133.       RemoveZeros(person_tasks_.at(person));
  134.    
  135.       return {updated_tasks, untouched_tasks};
  136. }
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement