短作业优先算法-SJF

//任何转载源代码的请保留出处作者等相关信息

//作者:chlaws

//运行环境:visual studio 2008

//描述:短作业优先算法-SJF

//说明:才用c风格写的,这里不给出原创的思路,在阅读过程中有不懂得可以询问

#include <iostream> #include <cstdlib> #include <numeric> #include <algorithm> using namespace std; #define MAX 10 int MAXNUM=10000; char process[MAX]=""; //进程标识 int arrivetime[MAX];//达到时间 int servicetime[MAX];//服务时间 int finishtime[MAX]; //完成时间 int turnovertime[MAX];//周转时间 double avgturnovertime; //平均周转时间 double powertime[MAX]; //带权周转时间 double avgpowertime; //平均带权周转时间 int init(); void SJF(); void output(); void showsingle(int* arr,int len); //初始化,并返回进程数 int init() { cout << "输入进程队列标识(用单个字母表示一个进程,字母间用tab间隔)" << endl; int i=0; while(i<MAX) { cin.get(process[i]); if(process[i]==' ' || process[i]=='/t') { continue; } if(process[i]=='q' || process[i]=='/n') { process[i]='/0'; break; } i++; } int len=strlen(process); cout << "依次输入进程到达时间(时间之间用tab间隔)" << endl; for(int ix=0; ix<len; ix++) { cin >> arrivetime[ix]; } cout << "依次输入服务时间(时间之间用tab间隔)" <<endl; for(ix=0; ix<len; ix++) { cin >> servicetime[ix]; } return len; } void SJF(int len) { //=======开始进入关键计算区===================================== //完成时间的计算 int totalsrvtime=servicetime[0]; finishtime[0]=totalsrvtime; int servicetimebak[MAX];//servicetimebak用来作为servicetime的副本,避免对servicetime造成修改 copy(servicetime,servicetime+MAX,servicetimebak); int* phead=servicetimebak; int* pos=NULL;//用来指向服务时间序列中的最小值 int curpos;//用来在下面的for循环中标识每次在服务时间序列中所找到的最小值的下标 for(int ix=1; ix<len; ix++) { pos=min_element(servicetimebak+1,servicetimebak+len); curpos=distance(phead,pos); totalsrvtime=totalsrvtime+servicetime[curpos]; finishtime[curpos]=totalsrvtime; *pos=MAXNUM; } //=======结束离开关键计算区===================================== //周转时间计算 for(ix=0; ix<len; ix++) { turnovertime[ix]=finishtime[ix]-arrivetime[ix]; } avgturnovertime=accumulate(turnovertime,turnovertime+len,0)*1.0/len; //带权周转时间计算 for(ix=0; ix<len; ix++) { powertime[ix]=turnovertime[ix]*1.0/servicetime[ix]; } //平均带权周转时间 double tmptotal=0.0; for(ix=0; ix<len; ix++) { tmptotal+=powertime[ix]; } avgpowertime=tmptotal/len; } void output() { cout <<endl<<endl; cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl; int len=strlen(process); //显示进程序列 for(int ix=0; ix<len; ix++) { cout <<process[ix] << "/t"; } cout << endl; //显示到达时间序列 showsingle(arrivetime,len); //显示服务时间序列 showsingle(servicetime,len); cout <<endl<<endl; //显示完成时间序列 showsingle(finishtime,len); //显示周转时间序列 showsingle(turnovertime,len); cout << "平均周转时间 :" << avgturnovertime << endl; //显示带权周转时间序列 for(ix=0; ix<len; ix++) { cout << powertime[ix] << "/t"; } cout <<endl; cout << "平均带权周转时间:" << avgpowertime << endl; cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl; } //对int类型的数组进行格式化输出 void showsingle(int* arr,int len) { for(int ix=0; ix<len; ix++) { cout << arr[ix] << "/t"; } cout <<endl; } int main() { cout << "/t/t||本程序是短作业优先算法||" << endl; int len = init(); SJF(len); output(); system("PAUSE"); return 0; } /*running result ||本程序是短作业优先算法|| 输入进程队列标识(用单个字母表示一个进程,字母间用tab间隔) a b c d e 依次输入进程到达时间(时间之间用tab间隔) 0 1 2 3 4 依次输入服务时间(时间之间用tab间隔) 4 3 5 2 4 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ a b c d e 0 1 2 3 4 4 3 5 2 4 4 9 18 6 13 4 8 16 3 9 平均周转时间 :8 1 2.66667 3.2 1.5 2.25 平均带权周转时间:2.12333 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 请按任意键继续. . . */ 

你可能感兴趣的:(c,算法,System,作业,output,distance)