#include <stdio.h> #include <string.h> void main() { int i=0,j=0,finishtime=0,rotatetime=0,n=0,k=0,l,m; float a,b; int starttime,temp,max=1; int reachtime[24],runtime[24]; FILE *fp; char str[1000]; fp=fopen("d:/diaodu.txt","rt"); if(fp==NULL) { printf("cannot open file "); return; } printf("到达时间 所需运行时间 "); while (fgets(str,1000,fp)) { sscanf(str,"%d,%d ",reachtime+i,runtime+i); printf("%d %d ",reachtime[i],runtime[i]); i++; } fclose(fp); for(j=0;j<i;j++) { for (k=0;k<i-j;k++) { if (reachtime[k]>reachtime[k+1]) { temp=runtime[k]; runtime[k]=runtime[k+1]; runtime[k+1]=temp; temp=reachtime[k]; reachtime[k]=reachtime[k+1]; reachtime[k+1]=temp; } } } printf("*********************** "); printf("1.fcfs 2.SJF 3.HRRN "); printf("*********************** "); scanf("%d",&n); starttime=reachtime[1]; if(n==1) { printf("You want to run fcfs: "); printf("作业编号 开始时间 运行时间 完成时间 周转时间 带权周转时间 "); for(j=1;j<=i;j++) { finishtime=starttime+runtime[j]; rotatetime=starttime+runtime[j]-reachtime[j]; printf("%d %d %d %d %d %f ",j,starttime,runtime[j],finishtime,rotatetime,(float)(starttime+runtime[j]-reachtime[j])/runtime[j]); a+=rotatetime; b+=(starttime+runtime[j]-reachtime[j])/runtime[j]; starttime=starttime+runtime[j]; if(starttime<reachtime[j+1]) starttime=reachtime[j+1]; } printf("平均周转时间为:%f ",a/i); printf("平均带权周转时间为:%f ",b/i); } if(n==2) { printf("You want to run sjf "); printf("作业编号 开始时间 运行时间 完成时间 周转时间 带权周转时间 "); starttime=reachtime[1]; for(j=1;j<=i;j++) { finishtime=starttime+runtime[j]; rotatetime=starttime+runtime[j]-reachtime[j]; printf("%d %d %d %d %d %f ",j,starttime,runtime[j],finishtime,rotatetime,(float)(starttime+runtime[j]-reachtime[j])/runtime[j]); a+=rotatetime; b+=(starttime+runtime[j]-reachtime[j])/runtime[j]; for(k=j;k<=i;k++) { if(starttime+runtime[j]>reachtime[k+1]) max=k; } for(l=j;l<max;l++) { for (m=0;m<max-l;m++) { if (runtime[m]>runtime[m+1]) { temp=runtime[m]; runtime[m]=runtime[m+1]; runtime[m+1]=temp; temp=reachtime[m]; reachtime[m]=reachtime[m+1]; reachtime[m+1]=temp; } } } starttime=starttime+runtime[j]; if(starttime<reachtime[j+1]) starttime=reachtime[j+1]; } printf("平均周转时间为:%f",a/i); printf("平均带权周转时间为:%f",b/i); } }