?? zydd.txt
字號:
// zuoye.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
#define WorkMAX 20
//---------------------------------------------------------
typedef struct zuoye /*作業控制快*/
{char name; /*作業名*/
float handintime; /*提交時間*/
float runtime; /*運行時間*/
int workspace; /*工作空間*/
float starttime; /*開始時間*/
float finishtime; /*完成時間*/
float T; /*周轉時間*/
float W; /*帶權周轉時間*/
char condition; /*狀態*/
}JCB;
//---------------------------------------------------------
void turn_func(JCB work[WorkMAX],int i,int j) /*交換位置*/
{JCB bridge;
bridge=work[i];
work[i]=work[j];
work[j]=bridge;
}
//--------------------------------------------------------
int input_func(JCB work[WorkMAX]) /*輸入函數*/
{int n,i,j;
cout<<"please input the work number:"<<endl; /*作業個數*/
cin>>n;
cout<<"please input the works message"<<endl; /*作業信息*/
cout<<"Workname Handintime Runtime:"<<endl;
for(i=0;i<n;i++)
{cin>>work[i].name;
cin>>work[i].handintime;
cin>>work[i].runtime;
work[i].condition='W';
}
for(i=0;i<n-1;i++) /*按時間先后排序*/
for(j=i+1;j<n;j++)
if(work[i].handintime>work[j].handintime)
turn_func(work,i,j);
return n;
}
//--------------------------------------------------------
int Fselect_func(JCB work[WorkMAX],int n,int s0)/*先來先服務*/
{int i,s1,nostop;
for(nostop=1,i=0;i<n&&nostop;i++)
if(work[i].condition=='W')
{s1=i;nostop=0;}
return s1;
}
//-----------------------------------------------------------
int Sselect_func(JCB work[WorkMAX],int n,int s0)/*短作業優先*/
{int i,j,s1,count,nostop,a[WorkMAX];
for(nostop=1,i=0;i<n&&nostop;i++)
if(work[i].condition=='W')
{s1=i;nostop=0;}
if(s0!=-1)
{
for(j=0,i=0;i<n;i++)
if(work[i].condition=='W'&&work[i].handintime<=work[s0].finishtime)
{
a[j]=i;
j++;
}
count=j;
if(count>1)
{
s1=a[0];
for(j=1;j<count;j++)
if(work[a[j]].runtime<work[s1].runtime)
s1=a[j];
}
}
return s1;
}
//-------------------------------------------------------------------------
int Hselect_func(JCB work[WorkMAX],int n,int s0) /*最高響應比*/
{int i,j,s1,count,nostop,b[WorkMAX];
for(nostop=1,i=0;i<n&&nostop;i++)
if(work[i].condition=='W')
{s1=i;nostop=0;}
if(s0!=-1)
{
for(j=0,i=0;i<n;i++)
if(work[i].condition=='W'&&work[i].handintime<=work[s0].finishtime)
{
b[j]=i;
j++;
}
count=j;
if(count>1)
{
s1=b[0];
for(j=1;j<count;j++)
if((work[s0].finishtime-work[b[j]].handintime)/work[b[j]].runtime>
(work[s0].finishtime-work[s1].handintime)/work[s1].runtime)
s1=b[j];
}
}
return s1;
}
//------------------------------------------------------------------------
void run_func(JCB work[WorkMAX],int s0,int s1) /*作業運行函數*/
{ work[s1].condition='R';
if(s0==-1)
work[s1].starttime=work[s1].handintime;
else if(work[s1].handintime>work[s0].finishtime)
work[s1].starttime=work[s1].handintime;
else work[s1].starttime=work[s0].finishtime;
work[s1].finishtime=work[s1].starttime+work[s1].runtime;
work[s1].condition='F';
work[s1].T=work[s1].finishtime-work[s1].handintime;
work[s1].W=work[s1].T/work[s1].runtime;
cout<<work[s1].name;
}
//----------------------------------------------------------------
void print_func(JCB work[WorkMAX],int n) /*輸出*/
{int i;
float sumT=0,sumW=0;
for(i=0;i<n;i++)
{sumT+=work[i].T;sumW+=work[i].W;}
cout<<endl;
cout<<"The average of T is:"<<sumT/n<<endl;
cout<<"The average of W is:"<<sumW/n<<endl;
cout<<endl;
}
//--------------------------------------------------------------
int main(int argc, char* argv[]) /*模擬作業調度*/
{int k,n,s0,s1;
JCB work[WorkMAX];
n=input_func(work); /*輸入*/
//---------------------------------------------------------------
cout<<"FCFS:"<<endl; /*先來先服務*/
for(k=0;k<n;k++)
work[k].condition='W';
s0=-1;
for(k=0;k<n;k++)
{s1=Fselect_func(work,n,s0);
run_func(work,s0,s1);
s0=s1;
}
print_func(work,n);
//-----------------------------------------------------------------
cout<<"Short work:"<<endl; /*短作業優先*/
for(k=0;k<n;k++)
work[k].condition='W';
s0=-1;
for(k=0;k<n;k++)
{s1=Sselect_func(work,n,s0);
run_func(work,s0,s1);
s0=s1;
}
print_func(work,n);
//------------------------------------------------------------------
cout<<"High respond:"<<endl; /*最高響應比*/
for(k=0;k<n;k++)
work[k].condition='W';
s0=-1;
for(k=0;k<n;k++)
{s1=Hselect_func(work,n,s0);
run_func(work,s0,s1);
s0=s1;
}
print_func(work,n);
//------------------------------------------------------------------
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -