标题: 机器调度算法 [打印本页]
作者: 123456请问 时间: 2017-5-28 10:11
标题: 机器调度算法
#include<iostream.h>
int flag;
typedef struct
{
int ID;
double time;
}zuoye;
double min(double x[],int n)
{
if(n>1){
double temp=x[1];
flag=1;
for(inti=2;i<=n;i++)
if(x<temp){
temp=x;
flag=i;
}
return temp;
}
else
{
flag=1;
return x[1];
}
}
double max(double x[],int n)
{
if(n>1){
double temp=x[1];
for(inti=2;i<=n;i++)
if(x>temp){
temp=x;
}
return temp;
}
else
{
return x[1];
}
}
void exchange(zuoye &A,zuoye &B)
{
zuoye temp=A;
A=B;
B=temp;
}
void Bubble(zuoye q[],int n)
{
for(int i=1;i<n;i++)
for(intj=i+1;j<=n;j++)
if(q.time<q[j].time )
exchange(q,q[j]);
}
void main()
{
int i;
int M,N;
double Time=0;
int POINT;
cout<<"请输入机器数和作业数,要求作业数大于机器数:"<<endl;
cout<<"机器数:";
cin>>M;
cout<<"作业数:";
cin>>N;
zuoye *p=new zuoye[N+1];
if (M>=N)
cout<<"Error!"<<endl;
else
{
for(i=1;i<=N;i++)
{ p.ID=i;
}
double* jiqi=newdouble[M+1];
for(i=1;i<=N;i++)
{cout<<"作业"<<i<<"所需时间是:";
cin>>p.time;
}
Bubble(p,N);
POINT=1;
cout<<endl<<endl;
for( i=1;i<=M;i++)
{
jiqi=p[POINT].time;
cout<<"机器"<<i<<"当前正在处理作业"<<p[POINT].ID<<
endl<<"该作业所需时间为:"<<p[POINT].time<<endl;
POINT++;
cout<<endl;
}
while(POINT<=N){
doubleMIN=min(jiqi,M);
Time+=MIN;
cout<<MIN<<"分钟过后"<<endl<<"机器"<<flag<<"完成上次作业。开始处理作业:"<<p[POINT].ID<<endl<<
"该作业所需时间为:"<<p[POINT].time<<endl;
jiqi[flag]=p[POINT].time;
POINT++;
cout<<endl;
for(i=1;i<=M;i++)
{
if(i!=flag){
jiqi-=MIN;
cout<<"机器"<<i<<"剩余处理时间"<<jiqi<<endl;
cout<<endl;
}
}
}
Time+=max(jiqi,M);
cout<<endl;
cout<<"全部作业以处理完毕。消耗总时间为:"<<Time<<endl;
}
}
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |