//
设计一个按优先数调度算法实现处理器调度的程序。
#include < iostream.h >
typedef struct p
{
char name[];
int time;
int num;
struct p *next;
} plist;
void creat();
void sort1(plist * L);
void sort2(plist * L);
void display(plist * L);
void done(plist * L);
plist * L;
int main()
{
creat();
display(L);
done(L);
getchar();
getchar();
}
void creat()
{
plist *s,*r;
L=new plist;
L->next=NULL;
r=L;
cout<<" 优先数进程调度模拟"<<endl<<endl;
cout<<"输入进程的名字,要求运行时间和优先数:"<<endl;
for(int i=1;i<=5;i++)
{
s=new plist;
while(1)
{
cout<<"第"<<i<<"个进程:";
cin>>s->name>>s->time>>s->num;
if(s->time>0)
break;
else cout<<"输入错误,请重新输入."<<endl;
}
r->next=s;
r=s;
}
r->next=NULL;
sort1(L);
}
void done(plist * L)
{
while(1)
{
plist *p=L->next;
if(p==NULL)
{
cout<<"所有进程执行完毕."<<endl;
return;
}
else
{
cout<<p->name<<"进程运行一个了单位时间."<<endl;
p->num-=1;
p->time-=1;
if(p->time==0)
{
L->next=p->next;
delete p;
display(L);
}
else
{
sort2(L);
display(L);
}
}
}
}
void display(plist * L)
{
plist *p=L->next;
while(p!=NULL)
{
cout<<p->name<<" "<<p->time<<" "<<p->num<<endl;
p=p->next;
}
cout<<endl;
}
void sort1(plist * L) // 链表排序
{
plist *p=L->next, *q, *r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
q=L;
while(q->next!=NULL&&q->next->num>p->num)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}
void sort2(plist * L)
{
plist *p=L->next,*q,*r;
L->next=p->next;
q=L->next;
r=L;
while(q!=NULL&&q->num>=p->num)
{
r=q;
q=r->next;
}
r->next=p;
p->next=q;
}
#include < iostream.h >
typedef struct p
{
char name[];
int time;
int num;
struct p *next;
} plist;
void creat();
void sort1(plist * L);
void sort2(plist * L);
void display(plist * L);
void done(plist * L);
plist * L;
int main()
{
creat();
display(L);
done(L);
getchar();
getchar();
}
void creat()
{
plist *s,*r;
L=new plist;
L->next=NULL;
r=L;
cout<<" 优先数进程调度模拟"<<endl<<endl;
cout<<"输入进程的名字,要求运行时间和优先数:"<<endl;
for(int i=1;i<=5;i++)
{
s=new plist;
while(1)
{
cout<<"第"<<i<<"个进程:";
cin>>s->name>>s->time>>s->num;
if(s->time>0)
break;
else cout<<"输入错误,请重新输入."<<endl;
}
r->next=s;
r=s;
}
r->next=NULL;
sort1(L);
}
void done(plist * L)
{
while(1)
{
plist *p=L->next;
if(p==NULL)
{
cout<<"所有进程执行完毕."<<endl;
return;
}
else
{
cout<<p->name<<"进程运行一个了单位时间."<<endl;
p->num-=1;
p->time-=1;
if(p->time==0)
{
L->next=p->next;
delete p;
display(L);
}
else
{
sort2(L);
display(L);
}
}
}
}
void display(plist * L)
{
plist *p=L->next;
while(p!=NULL)
{
cout<<p->name<<" "<<p->time<<" "<<p->num<<endl;
p=p->next;
}
cout<<endl;
}
void sort1(plist * L) // 链表排序
{
plist *p=L->next, *q, *r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
q=L;
while(q->next!=NULL&&q->next->num>p->num)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}
void sort2(plist * L)
{
plist *p=L->next,*q,*r;
L->next=p->next;
q=L->next;
r=L;
while(q!=NULL&&q->num>=p->num)
{
r=q;
q=r->next;
}
r->next=p;
p->next=q;
}