?? 函數.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 + -