Advertisement
metalni

OOP Labs 7 Rasporeduvac na zadaci

Jun 2nd, 2020
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Task{
  7.     protected:
  8.         char id[5];
  9.     public:
  10.     Task();
  11.     Task (const char * id);
  12.     ~Task();
  13.     virtual int getOrder() = 0;
  14.     virtual void print() = 0;
  15.     const char * getID();
  16. };
  17.  
  18. Task::Task(){
  19.     strcpy(this->id, "None");
  20. }
  21. Task::Task(const char * id){
  22.     strcpy(this-> id, id);
  23. }
  24. Task::~Task(){}
  25. const char * Task::getID(){
  26.     return this->id;
  27. }
  28. //Preoptovaruvanje na operatorot za sporedba == (prima 2 argumenti bidejki se definira nadvor od klasata Task)
  29. bool operator==(Task *left, Task &right){
  30.     if(!strcmp(left->getID(), right.getID())&&(left->getOrder() == right.getOrder()))
  31.         return true;
  32.     else
  33.         return false;
  34. }
  35. class TimedTask : public Task{
  36.     private:
  37.        int time;
  38.     public:
  39.         TimedTask();
  40.         TimedTask(const char * id, const int time) : Task(id){
  41.             this->time = time;
  42.         }
  43.         ~TimedTask();
  44.         int getOrder();
  45.         void print();
  46. };
  47.  
  48. TimedTask::TimedTask(){
  49.     this->time = 0;
  50. }
  51. TimedTask::~TimedTask(){}
  52. int TimedTask::getOrder(){
  53.     return this->time;
  54. }
  55. void TimedTask::print(){
  56.     cout << "TT->" << this->id << ":" << this->time << endl;
  57. }
  58.  
  59. class PriorityTask : public Task {
  60.     private:
  61.         int priority;
  62.     public:
  63.         PriorityTask();
  64.         PriorityTask(const char * id, const int priority) : Task (id){
  65.             this->priority = priority;
  66.         }
  67.         ~PriorityTask();
  68.         int getOrder();
  69.         void print();
  70. };
  71.  
  72. PriorityTask::PriorityTask(){
  73.     this->priority = 0;
  74. }
  75. PriorityTask::~PriorityTask(){}
  76. int PriorityTask::getOrder(){
  77.     return this->priority;
  78. }
  79. void PriorityTask::print(){
  80.     cout << "PT->" << this->id << ":" << this->priority << endl;
  81. }
  82.  
  83. //void scheduleTimedTasks(Task ** tasks, int n, int t) {}
  84. void scheduleTimedTasks(Task ** tasks, int n, int t){
  85.     for (int i=0; i<n; i++){
  86.         for(int j=i+1; j<n; j++){
  87.             if(tasks[i]->getOrder() > tasks[j]->getOrder()){
  88.                 Task *tmp = tasks[i];
  89.                 tasks[i] = tasks[j];
  90.                 tasks[j] = tmp;
  91.             }
  92.         }
  93.     }
  94.     for(int i = 0; i < n; i++){
  95.         TimedTask *pt = dynamic_cast<TimedTask *>(tasks[i]);
  96.         if(pt){
  97.             if(tasks[i]->getOrder() < t)
  98.                 tasks[i]->print();
  99.         }
  100.     }
  101. }
  102. //void schedulePriorityTasks(Task ** tasks, int n, int p) {}
  103. void schedulePriorityTasks(Task ** tasks, int n, int p){
  104.     for (int i=0; i<n; i++){
  105.         for(int j=i+1; j<n; j++){
  106.             if(tasks[i]->getOrder() > tasks[j]->getOrder()){
  107.                 Task *tmp = tasks[i];
  108.                 tasks[i] = tasks[j];
  109.                 tasks[j] = tmp;
  110.             }
  111.         }
  112.     }
  113.  
  114.     for(int i = 0; i < n; i++){
  115.         PriorityTask *pt = dynamic_cast<PriorityTask *>(tasks[i]);
  116.         if(pt){
  117.             if(tasks[i]->getOrder() < p)
  118.                 tasks[i]->print();
  119.         }
  120.     }
  121. }
  122.  
  123. //main
  124. int main () {
  125.     int testCase;
  126.     int n;
  127.     cin>>testCase;
  128.    
  129.     if (testCase==0){
  130.         cin>>n;
  131.         Task ** tasks;
  132.         tasks = new Task * [n];
  133.         for (int i=0;i<n;i++){
  134.         char id [5];
  135.         int timeOrPriority;
  136.         int type; //0 za timed, 1 za priority
  137.         cin >> type >>id >> timeOrPriority;
  138.         if (type==0)
  139.             tasks[i] = new TimedTask(id,timeOrPriority);
  140.         else
  141.             tasks[i] = new PriorityTask(id,timeOrPriority);
  142.         //tasks[i]->print();
  143.         }
  144.        
  145.         cout<<"SCHEDULING PRIORITY TASKS WITH PRIORITY DEGREE LESS THAN 10"<<endl;
  146.         schedulePriorityTasks(tasks,n,10);
  147.        
  148.     }
  149.     else if (testCase==1) {
  150.         cin>>n;
  151.         Task ** tasks;
  152.         tasks = new Task * [n];
  153.         for (int i=0;i<n;i++){
  154.         char id [5];
  155.         int timeOrPriority;
  156.         int type; //0 za timed, 1 za priority
  157.         cin >> type >>id >> timeOrPriority;
  158.         if (type==0)
  159.             tasks[i] = new TimedTask(id,timeOrPriority);
  160.         else
  161.             tasks[i] = new PriorityTask(id,timeOrPriority);
  162.         //tasks[i]->print();
  163.         }
  164.        
  165.        
  166.         cout<<"SCHEDULING TIMED TASKS WITH EXECUTION TIME LESS THAN 50"<<endl;
  167.         scheduleTimedTasks(tasks,n,50);
  168.     }
  169.     else {
  170.         TimedTask * tt1 = new TimedTask("11",10);
  171.         TimedTask * tt2 = new TimedTask("11",11);
  172.         TimedTask * tt3 = new TimedTask("11",11);
  173.         PriorityTask * pp1 = new PriorityTask("aa",10);
  174.         PriorityTask * pp2 = new PriorityTask("11",10);
  175.        
  176.         cout << (tt1==(*tt2))<<endl;
  177.         cout << (tt2==(*tt3))<<endl;
  178.         cout << (pp1==(*pp2))<<endl;
  179.         cout << (pp2==(*tt1))<<endl;
  180.     }
  181.    
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement