#include<stdio.h> #include<stdlib.h> int Number=0; //作业个数 float AverageTurnaroundTime=0; //平均周转时间 float AverageTurnaroundTimeWithWeight=0; //平均带权周转时间 struct work { char Name[10]; //工作名称 int ArrivalTime; //到达时间 int NeetTime; //所需时间 int StartTime; //开始时间 int FinishTime; //完成时间 int TurnaroundTime; //周转时间 float TurnaroundTimeWithWeight; //带权周转时间 float ResponseRation; //响应比 int Buff; //工作状态(-2:不可进入 -1:可进入 0:进入 1:完成) }; typedef work WORK; WORK CY[32]; int IntegerCheck(int Min,int Max); //整数校验模块 void TheMainMenu(); //主菜单界面 void ManualInput(); void FCFS_Algorithm( //输出模块 ); //先来先服务算法 void SJF_Algorithm(); //手动输入模块 void AlgorithmSelectMenu(); //算法选择菜单 void Output(); //最短作业优先算法 void HRRF_Algorithm(); //最高响应比优先算法 main() { while(1) { TheMainMenu(); } } int IntegerCheck(int Min,int Max) { int n,b; do { while(scanf("%d",&n)!=1) { fflush(stdin); printf(" 格式错误!请重新输入:"); } if(n<Min||n>Max) { printf(" 范围错误!请重新输入<%d--%d>:",Min,Max); continue; } b=0; }while(b); return n; } void TheMainMenu() { int n; system("cls"); printf(" ============================ 主选菜单 ============================"); printf(" 1.手动输入作业信息"); printf(" 0.退出"); printf(" =================================================================="); printf(" 请输入选项<0--1>:"); n=IntegerCheck(0,1); switch(n) { case 1:ManualInput();break; case 2:break; case 3:break; case 4:break; case 0:exit(0);break; } } void ManualInput() { int i; system("cls"); printf(" ======================== 手动输入作业信息 ========================"); printf(" =================================================================="); printf(" 请输入个数<2-32>"); Number=IntegerCheck(2,32); for(i=0;i<Number;i++) { printf(" 请输入第%d个数",i+1); printf(" ================"); printf(" 作业名称:"); scanf("%s",&CY[i].Name); printf(" 到达时间:"); CY[i].ArrivalTime=IntegerCheck(0,1024); printf(" 所需时间:"); CY[i].NeetTime=IntegerCheck(1,1024); CY[i].StartTime=-1; CY[i].FinishTime=-1; CY[i].TurnaroundTime=-1; CY[i].TurnaroundTimeWithWeight=-1; CY[i].ResponseRation=-1; CY[i].Buff=-2; } AlgorithmSelectMenu(); } void ArrivalTimeSort() { int i,j; WORK Temp; printf(" 名称 到达 需要"); for(i=0;i<Number;i++) { for(j=i;j<Number;j++) { if(CY[i].ArrivalTime>CY[j].ArrivalTime) { Temp=CY[j]; CY[j]=CY[i]; CY[i]=Temp; } } printf(" %s %d %d",CY[i].Name,CY[i].ArrivalTime,CY[i].NeetTime); } } void AlgorithmSelectMenu() { int n; system("cls"); printf(" ========================== 算法选择菜单 =========================="); ArrivalTimeSort(); printf(" =================================================================="); printf(" 1.FCFS (先来先服务 First Come First Served)"); printf(" 2.SJF (最短作业优先 Shortest Job First)"); printf(" 3.HRRF (最高响应比优先 Highest Response Ratio First)"); printf(" =================================================================="); printf(" 请输入选项<1--3>:"); n=IntegerCheck(1,3); switch(n) { case 1:FCFS_Algorithm();break; case 2:SJF_Algorithm();break; case 3:HRRF_Algorithm();break; } } void Output() { int i,j,a,b,x,suma,sumb; x=0; printf(" =================================================================="); printf(" 名称 到达 需要 开始 完成 周转 带权"); for(j=0;j<Number;j++) { if(CY[j].Buff == 0) { printf(" %s %d %d %d" ,CY[j].Name,CY[j].ArrivalTime,CY[j].NeetTime,CY[j].StartTime); } else if(CY[j].Buff == 1) { x++; printf(" %s %d %d %d %d %d %3.2f" ,CY[j].Name,CY[j].ArrivalTime,CY[j].NeetTime,CY[j].StartTime,CY[j].FinishTime,CY[j].TurnaroundTime,CY[j].TurnaroundTimeWithWeight); } if(x==Number)//加到作业个数 { suma=0; sumb=0; printf(" =================================================================="); for(i=0;i<Number;i++) { a=10000*(int)CY[i].TurnaroundTime; suma+=a; b=(int)(CY[i].TurnaroundTime*10000)%10000; sumb+=b; } AverageTurnaroundTime=((float)(suma+sumb)/10000/Number); printf(" 平均周转时间为 %4.4f",AverageTurnaroundTime); suma=0; sumb=0; for(i=0;i<Number;i++) { a=10000*(int)CY[i].TurnaroundTimeWithWeight; suma+=a; b=(int)(CY[i].TurnaroundTimeWithWeight*10000)%10000; sumb+=b; } AverageTurnaroundTimeWithWeight=((float)(suma+sumb)/10000/Number); printf(" 平均带权周转时间为 %4.4f",AverageTurnaroundTimeWithWeight); } } getchar(); } void FCFS_Algorithm()//先来先服务算法 { int i,j,x,Time; Time=CY[0].ArrivalTime; getchar(); for(i=0;i<Number;i++) { x=1; while(x) { for(j=i;j<Number;j++) { if(CY[j].ArrivalTime<=Time) { CY[j].Buff=-1; x=0; } } } system("cls"); printf(" 当前系统时间为%d",Time); printf(" %d号作业%s进入",i+1,CY[i].Name); CY[i].StartTime=Time; CY[i].Buff=0; Output(); while(CY[i].NeetTime>(Time-CY[i].StartTime)) { Time++; } CY[i].FinishTime=Time; CY[i].TurnaroundTime=CY[i].FinishTime-CY[i].ArrivalTime; CY[i].TurnaroundTimeWithWeight=(float)CY[i].TurnaroundTime/CY[i].NeetTime; CY[i].Buff=1; system("cls"); printf(" 当前系统时间为%d",Time); printf(" %d号作业%s完成",i+1,CY[i].Name); Output(); if(i!=Number-1) { while(CY[i+1].ArrivalTime>Time) { Time++; } } } } void SJF_Algorithm()//最短作业优先算法 { int i,j,x,Time; WORK Temp; Time=CY[0].ArrivalTime; getchar(); for(i=0;i<Number;i++) { x=1; while(x) { for(j=i;j<Number;j++) { if(CY[j].ArrivalTime<=Time) { CY[j].Buff=-1; x=0; } } if(x==1) Time++; } for(j=i;j<Number;j++) { if((CY[i].NeetTime>CY[j].NeetTime)&&(CY[j].Buff==-1)) { Temp=CY[j]; CY[j]=CY[i]; CY[i]=Temp; } } system("cls"); printf(" 当前系统时间为%d",Time); printf(" %d号作业%s进入",i+1,CY[i].Name); CY[i].StartTime=Time; CY[i].Buff=0; Output(); while(CY[i].NeetTime>(Time-CY[i].StartTime)) { Time++; } CY[i].FinishTime=Time; CY[i].TurnaroundTime=CY[i].FinishTime-CY[i].ArrivalTime; CY[i].TurnaroundTimeWithWeight=(float)CY[i].TurnaroundTime/CY[i].NeetTime; CY[i].Buff=1; system("cls"); printf(" 当前系统时间为%d",Time); printf(" %d号作业%s完成",i+1,CY[i].Name); Output(); } } void HRRF_Algorithm()//最高响应比优先算法 { int i,j,x,Time; WORK Temp; Time=CY[0].ArrivalTime; getchar(); for(i=0;i<Number;i++) { x=1; while(x) { for(j=i;j<Number;j++) { if(CY[j].ArrivalTime<=Time) { CY[j].Buff=-1; CY[j].ResponseRation=(float)(1+(float)(Time-CY[j].ArrivalTime)/CY[j].NeetTime); x=0; printf(" * CY[%d](%s).ResponseRation=%3.2f * ",j,CY[j].Name,CY[j].ResponseRation); } } if(x==1) Time++; } for(j=i;j<Number;j++) { if((CY[i].ResponseRation<CY[j].ResponseRation)&&(CY[j].Buff==-1)) { Temp=CY[j]; CY[j]=CY[i]; CY[i]=Temp; } } system("cls"); printf(" 当前系统时间为%d",Time); printf(" %d号作业%s进入",i+1,CY[i].Name); CY[i].StartTime=Time; CY[i].Buff=0; Output(); while(CY[i].NeetTime>(Time-CY[i].StartTime)) { Time++; } CY[i].FinishTime=Time; CY[i].TurnaroundTime=CY[i].FinishTime-CY[i].ArrivalTime; CY[i].TurnaroundTimeWithWeight=(float)CY[i].TurnaroundTime/CY[i].NeetTime; CY[i].Buff=1; system("cls"); printf(" 当前系统时间为%d",Time); printf(" %d号作业%s完成",i+1,CY[i].Name); Output(); } }