Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C by shirma ( 16 years ago )
#include <iostream>
#include <cstring>
using namespace std;
struct task_type{
int duration;
int priority;
int begin_time;
char task_name[10];
task_type* next;
};
struct queue_type{
task_type* head;
task_type* tail;
};
struct stack_type{
task_type* head;
};
void add_task_in_queue(queue_type* queue,task_type task){
task_type* p;
p=new task_type;
*p=task;
p->next=NULL;
if(queue->head==NULL){
queue->head=p;
queue->tail=p;
}else{task_type* tail;
queue->tail->next=p;
queue->tail=p;
}
}
task_type* pick_task_from_queue(queue_type* queue){
return queue->head;
}
void pop_task_from_queue(queue_type* queue){
if(queue->head==NULL)
return;
task_type* p;
p=queue->head;
queue->head->next=queue->head;
delete p;
}
void add_task_in_stack(stack_type* stack,task_type task){
task_type* p;
p=new task_type;
p->next=stack->head;
stack->head=p;
}
task_type* pick_task_from_stack(stack_type* stack){
return stack->head;
}
void pop_task_from_stack(stack_type* stack){
if(stack->head==NULL)
return;
task_type* p;
p=stack->head;
stack->head->next=stack->head;
delete p;
}
void add_task(queue_type* queue){
task_type task;
cout<<"Input duration"<<endl;
cin>>task.duration;
cout<<"Input priority of "<<endl;
cin>>task.priority;
cout<<"Input begin_time"<<endl;
cin>>task.begin_time;
cout<<"Input task name"<<endl;
cin>>task.task_name;
add_task_in_queue(queue,task);
}
bool is_empty_stack(stack_type* stack){
return stack->head==NULL;
}
bool is_empty_queue(queue_type* queue){
return queue->head==NULL;
}
void processing(queue_type* queue){
stack_type* stack;
task_type p1;
task_type p2;
task_type p3;
task_type tmp;
int tact=0;
while(!is_empty_queue(queue)||!is_empty_stack(stack)||p1.duration!=0||p2.duration!=0||p3.duration!=0){
if(!is_empty_stack(stack)){
cout<<tact<<endl;
tmp=*pick_task_from_stack(stack);
switch(tmp.priority){
case 0:
if(p1.duration==0){
cout<<"Процессор №1"<<endl;
p1=tmp;
pop_task_from_stack(stack);
}
break;
case 1:
if(p2.duration==0){
cout<<"Процессор №2"<<endl;
p2=tmp;
pop_task_from_stack(stack);
}
case 2:
if(p3.duration==0){
cout<<"Процессор №3"<<endl;
p3=tmp;
pop_task_from_stack(stack);
}
}
}
if(!is_empty_queue(queue)){
tmp=*pick_task_from_queue(queue);
if(tmp.begin_time==tact){
switch(tmp.priority){
case 0:
if(p1.duration==0){
p1=tmp;
pop_task_from_stack(stack);
}
break;
case 1:
if(p2.duration==0){
p2=tmp;
pop_task_from_stack(stack);
}
case 2:
if(p3.duration==0){
p3=tmp;
pop_task_from_stack(stack);
}
}
}
if(p1.duration>=0){
p1.duration--;
}
if(p2.duration>=0){
p2.duration--;
}
if(p3.duration>=0){
p3.duration--;
}
tact++;
}
}
}
int main()
{
int size;
stack_type* stack=new stack_type;
queue_type* queue=new queue_type;
queue->head=NULL;
queue->tail=NULL;
cout<<"How much tasks do u want to add?"<<endl;
cin>>size;
for(int i=0;i<size;i++){
add_task(queue);
}
processing(queue);
}
Revise this Paste