📄 函数.txt
字号:
#include<iostream.h>
float t1=1.5;
float t2=3;
float t3=4.5;
struct Pro
{
char name[10];//进程名
float reach;//进程到达时间
float time;//进程运行需要的时间
int priority;// 数字越小优先级越高
Pro *next;
};
typedef Pro * tab;
struct PCB
{char name[10];//进程名
float reach;//进程到达时间
float time;//进程运行需要的时间
int priority;// 数字越小优先级越高
float rest;//进程执行剩余时间
float start;//进程执行开始时间
float end;//进程执行结束时间
float pause;//进程中途停止时间
int n;//进程执行次数
PCB *next;
};
typedef PCB* pcb;
void inist_table(tab &l)
{int n;
Pro *p;
Pro *q;
l=new Pro;
q=l;
cout<<"请输入进程个数"<<endl;
cin>>n;
for(int i=1;i<=n;i++)
{p=new Pro;
cout<<"请输入进程名"<<endl;
cin>>p->name;
cout<<"请输入进程到达时间"<<endl;
cin>>p->reach;
cout<<"请输入进程执行所需时间"<<endl;
cin>>p->time;
cout<<"请输入进程优先级"<<endl;
cin>>p->priority;
q->next=p;
q=p;
}
q->next=NULL;
}
void insert_PCB(Pro *q,pcb &l11,pcb &l22,pcb &l33)//插入新节点
{pcb p;
if(q->priority==1)
{p=l11;
l11=new PCB;
char *x;
char *y;
x=l11->name;
y=q->name;
while(*y!='\0')
{(*x)=(*y);
x++;
y++;}
(*x)=(*y);
l11->reach=q->reach;
l11->time=q->time;
l11->rest=q->time;
l11->priority=q->priority;
l11->n=0;
p->next=l11;
l11->next=NULL;
}
if(q->priority==2)
{p=l22;
l22=new PCB;
char *x;
char *y;
x=l22->name;
y=q->name;
while(*y!='\0')
{(*x)=(*y);
x++;
y++;}
(*x)=(*y);
l22->reach=q->reach;
l22->time=q->time;
l22->rest=q->time;
l22->priority=q->priority;
l22->n=0;
p->next=l22;
l22->next=NULL;
}
if(q->priority==3)
{p=l33;
l33=new PCB;
char *x;
char *y;
x=l33->name;
y=q->name;
while(*y!='\0')
{(*x)=(*y);
x++;
y++;}
(*x)=(*y);
l33->reach=q->reach;
l33->time=q->time;
l33->rest=q->time;
l33->priority=q->priority;
l33->n=0;
p->next=l33;
l33->next=NULL;
}
}
void add(pcb L, pcb &l1,pcb &l11,pcb &l2,pcb &l22,pcb &l3,pcb &l33)
{ switch(L->priority)
{case 1:
if(L->next)
{l1->next=L->next;
l22->next=L;
l22=L;
l22->priority=2;
l22->next=NULL;}
else{ l1->next=NULL;l11=l1;
l22->next=L;
l22=L;
l22->priority=2;
l22->next=NULL;}
break;
case 2:
if(L->next)
{l2->next=L->next;
l33->next=L;
l33=L;
l33->priority=3;
l33->next=NULL;}
else{ l2->next=NULL;l22=l1;
l33->next=L;
l33=L;
l33->priority=3;
l33->next=NULL;}
break;
case 3:
if(L->next)
{l3->next=L->next;
l33->next=L;
l33=L;
l33->priority=3;
l33->next=NULL;}
break;
}
}
void add1(PCB *L,pcb &l1,pcb &l11,pcb &l2,pcb &l22,pcb &l3,pcb &l33,pcb &l44)
{if(L->priority==1){l1->next=L->next;if(!L->next)l11=l1;}
if(L->priority==2){l2->next=L->next;if(!L->next)l22=l2;}
if(L->priority==3){l3->next=L->next;if(!L->next)l33=l3;}
l44->next=L;
l44=L;
l44->next=NULL;
}
void did(Pro *l)
{
float now=0;//当前时间
pcb L1,L11,L2,L22,L3,L33;//分别指向每个队列的第一个节点和最后一个节点
pcb L4,L44;//指向执行完的的队列的第一个节点和最后个节点
pcb p;//指向当前操作的节点
Pro *q;
q=l->next;
L1=new PCB;
L1->next=NULL;
L11=L1;
L2=new PCB;
L2->next=NULL;
L22=L2;
L3=new PCB;
L3->next=NULL;
L33=L3;
L4=new PCB;
L4->next=NULL;
L44=L4;
insert_PCB(q,L11,L22,L33);
q=q->next;
char *kk;
loop1:
p=L1->next;
if(p!=NULL)
{
p->rest=p->rest-t1;
p->n++;
kk=p->name;
while(*kk!='\0')
{cout<<(*kk);
kk++;}
cout<<endl;
if(p->n==1)p->start=now;
if(p->rest==0){now=now+t1;p->end=now;}
if(p->rest<0){now=now+p->rest+t1;p->end=now;}
if(p->rest>0){now=now+t1;p->pause=now;}
while(q)
{if(q->reach<=now)
{
insert_PCB(q,L11,L22,L33);
q=q->next;}
else break;
}
if(p->rest>0)add(p,L1,L11,L2,L22,L3,L33);
else add1(p,L1,L11,L2,L22,L3,L33,L44);
goto loop1;
}
p=L2->next;
if(p!=NULL)
{
p->rest=p->rest-t2;
p->n++;
kk=p->name;
while(*kk!='\0')
{cout<<(*kk);
kk++;}
cout<<endl;
if(p->n==1)p->start=now;
if(p->rest==0){now=now+t2;p->end=now;}
if(p->rest<0){now=now+p->rest+t2;p->end=now;}
if(p->rest>0){now=now+t2;p->pause=now;}
while(q)
{if(q->reach<=now)
{
insert_PCB(q,L11,L22,L33);
q=q->next;}
else break;
}
if(p->rest>0)add(p,L1,L11,L2,L22,L3,L33);
else add1(p,L1,L11,L2,L22,L3,L33,L44);
goto loop1;
}
p=L3->next;
if(p!=NULL)
{
p->rest=p->rest-t3;
p->n++;
kk=p->name;
while(*kk!='\0')
{cout<<(*kk);
kk++;}
cout<<endl;
if(p->n==1)p->start=now;
if(p->rest==0){now=now+t3;p->end=now;}
if(p->rest<0){now=now+p->rest+t3;p->end=now;}
if(p->rest>0){now=now+t3;p->pause=now;}
while(q)
{if(q->reach<=now)
{
insert_PCB(q,L11,L22,L33);
q=q->next;}
else break;
}
if(p->rest>0)add(p,L1,L11,L2,L22,L3,L33);
else add1(p,L1,L11,L2,L22,L3,L33,L44);
goto loop1;
}
float k=0;
int c=0;
p=L4->next;
while(p)
{float m;
m=p->end-p->start;
k=k+m;
p=p->next;c++;}
k=k/c;
cout<<k<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -