StefiIOE

Rasporeduvac na zadaci polimorfizam

May 17th, 2020
1,159
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. class Task{
  6.     protected:
  7.     char id[5];
  8.    
  9.     public:
  10.  
  11.    
  12.     Task(char *id="")
  13.     {
  14.         strcpy(this->id,id);
  15.     }
  16.     Task (const Task &t)
  17.     {
  18.     strcpy(this->id,t.id);
  19.     }
  20.     char *getID(){return id;}
  21.   virtual int getOrder()=0;
  22.   virtual void print()=0;
  23. };
  24.  
  25. class TimedTask : public Task{
  26.     private:
  27.     int time;
  28.     public:
  29.     TimedTask(char *id,int time): Task(id)
  30.     {
  31.     this->time=time;
  32.     }
  33.     TimedTask(const TimedTask &t):Task(t){this->time=t.time;}
  34.     int getOrder(){return time;}
  35.     void print()
  36.     {
  37.     cout<<"TT->"<<id<<":"<<getOrder()<<endl;
  38.     }
  39. };
  40.  
  41. class PriorityTask : public Task {
  42.      private:
  43.    int priority;
  44.    
  45.     public:
  46.         PriorityTask(char *id,int priority): Task(id)
  47.     {
  48.     this->priority=priority;
  49.     }
  50.     PriorityTask(const PriorityTask &t):Task(t){this->priority=t.priority;}
  51.     int getOrder(){return priority;}
  52.     void print()
  53.     {
  54.     cout<<"PT->"<<id<<":"<<getOrder()<<endl;
  55.     }
  56.  
  57.    
  58. };
  59.  
  60. bool operator ==(Task * t1, Task & t2) {
  61.     return strcmp(t1->getID(),t2.getID())==0&&t1->getOrder()==t2.getOrder();
  62. }
  63.  
  64. void scheduleTimedTasks(Task ** tasks, int n,int t) {
  65.     for (int i=0;i<n-1;i++){
  66.         for (int j=i+1;j<n;j++){
  67.             if (tasks[i]->getOrder()>tasks[j]->getOrder()){
  68.                 Task * tmp = tasks[i];
  69.                 tasks[i]=tasks[j];
  70.                 tasks[j]=tmp;
  71.             }
  72.         }
  73.     }
  74.    
  75.     for (int i=0;i<n;i++){
  76.         TimedTask * ti = dynamic_cast<TimedTask *>(tasks[i]);
  77.         if (ti!=0&&tasks[i]->getOrder()<t){
  78.             tasks[i]->print();
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84. void schedulePriorityTasks(Task ** tasks, int n, int p){
  85.     for (int i=0;i<n-1;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.    
  95.     for (int i=0;i<n;i++){
  96.         PriorityTask * t = dynamic_cast<PriorityTask *>(tasks[i]);
  97.         if (t!=0&&tasks[i]->getOrder()<p){
  98.             tasks[i]->print();
  99.         }
  100.     }
  101. }
  102.  
  103. //void schedulePriorityTasks(Task ** tasks, int n, int p) {}
  104.  
  105. int main () {
  106.     int testCase;
  107.     int n;
  108.     cin>>testCase;
  109.    
  110.     if (testCase==0){
  111.         cin>>n;
  112.         Task ** tasks;
  113.         tasks = new Task * [n];
  114.         for (int i=0;i<n;i++){
  115.         char id [5];
  116.         int timeOrPriority;
  117.         int type; //0 za timed, 1 za priority
  118.         cin >> type >>id >> timeOrPriority;
  119.         if (type==0)
  120.             tasks[i] = new TimedTask(id,timeOrPriority);
  121.         else
  122.             tasks[i] = new PriorityTask(id,timeOrPriority);
  123.         //tasks[i]->print();
  124.         }
  125.        
  126.         cout<<"SCHEDULING PRIORITY TASKS WITH PRIORITY DEGREE LESS THAN 10"<<endl;
  127.         schedulePriorityTasks(tasks,n,10);
  128.        
  129.     }
  130.     else if (testCase==1) {
  131.         cin>>n;
  132.         Task ** tasks;
  133.         tasks = new Task * [n];
  134.         for (int i=0;i<n;i++){
  135.         char id [5];
  136.         int timeOrPriority;
  137.         int type; //0 za timed, 1 za priority
  138.         cin >> type >>id >> timeOrPriority;
  139.         if (type==0)
  140.             tasks[i] = new TimedTask(id,timeOrPriority);
  141.         else
  142.             tasks[i] = new PriorityTask(id,timeOrPriority);
  143.         //tasks[i]->print();
  144.         }
  145.        
  146.        
  147.         cout<<"SCHEDULING TIMED TASKS WITH EXECUTION TIME LESS THAN 50"<<endl;
  148.         scheduleTimedTasks(tasks,n,50);
  149.     }
  150.     else {
  151.         TimedTask * tt1 = new TimedTask("11",10);
  152.         TimedTask * tt2 = new TimedTask("11",11);
  153.         TimedTask * tt3 = new TimedTask("11",11);
  154.         PriorityTask * pp1 = new PriorityTask("aa",10);
  155.         PriorityTask * pp2 = new PriorityTask("11",10);
  156.        
  157.         cout << (tt1==(*tt2))<<endl;
  158.         cout << (tt2==(*tt3))<<endl;
  159.         cout << (pp1==(*pp2))<<endl;
  160.         cout << (pp2==(*tt1))<<endl;
  161.     }
  162.    
  163.     return 0;
  164. }
RAW Paste Data