//sun.cpp:定义控制台应用程序的入口点。//本算法包含四种调度:先到先服务,短作业优先,时间片轮转,优先级优先!#includestdio.h#defineN50voidmain(){voidsjp();voidfcfs();voidsjf();voidyxj();inta;while(true){printf(\n\n);printf(\t\t/*************************/);printf(\n\t\t/*1、先到先服务调度*/);printf(\n\t\t/*2、短作业优先调度*/);printf(\n\t\t/*3、时间片轮转调度*/);printf(\n\t\t/*4、优先级优先调度*/);printf(\n\t\t/*0、退出*/\n);printf(\t\t/*************************/);printf(\n\n\t请选择菜单项:\t);scanf(%d,&a);printf(\n);switch(a){case1:fcfs();break;case2:sjf();break;case3:sjp();break;case4:yxj();break;default:break;}if(a0&&a4)break;}}voidsjp(){inti,j,n,min,px,sjp,time;floatsum1,sum2;boolflag=true;printf(\t请输入有n个进程(0n=50):\t);scanf(%d,&n);while(n50||n=0){printf(n\t请重新输入:);scanf(%d,&n);}printf(\n\n);printf(\t请输入时间片大小(0sjp):\t);scanf(%d,&sjp);while(sjp=0){printf(n\t请重新输入:);scanf(%d,&sjp);}structGzuo{intid;//进程名字intdt;//到达时刻intst;//服务时间intwct;//完成时刻intst2;//标志是否完成floatzt;//周转时间floatdczt;//带权周转时间};Gzuoa[N];for(i=0;in;i++){a[i].id=i+1;printf(\t到达时间:);scanf(%d,&a[i].dt);printf(\t服务时间:);scanf(%d,&a[i].st);a[i].st2=a[i].st;printf(\n);}for(j=n-1;j=0;j--){for(i=0;ij;i++){if(a[i].dta[i+1].dt){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].st2;a[i].st2=a[i+1].st2;a[i+1].st2=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}time=a[0].dt;//printf(赋值后TIME值为:%d\n,time);min=0;while(minn){flag=true;for(i=0;in;i++){if(a[i].st20&&a[i].dt=time)flag=false;}for(i=0;in;i++){if(a[i].st20){if(a[i].dt=time){//printf(当前a[%d].st2值为:%d\n,i,a[i].st2);a[i].st2=a[i].st2-sjp;//printf(运算后当前a[%d].st2值为:%d\n,i,a[i].st2);//printf(当前TIME值为:%d\n,time);time=time+sjp;//printf(增加之后TIME值为:%d\n,time);if(a[i].st2=0){a[i].wct=time+a[i].st2;a[i].zt=(float)(a[i].wct-a[i].dt);a[i].dczt=a[i].zt/a[i].st;min++;}}elseif(flag){for(i=0;in;i++){if(a[i].st20&&a[i].dttime){time=a[i].dt;break;}}}}}}printf(\t1、按id号依次输出\n);printf(\t2、按完成顺序依次输出\n);printf(\n\t请选择输出顺序:\t);scanf(%d,&px);printf(\nid:到达时间\t服务时间\t完成时间\t周转时间\t带权周转时间\n);sum1=0;sum2=0;switch(px){case2:{for(i=0;in;i++){printf(%d:%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n,a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}printf(\n平均周转时间:%.2f\n,sum1/n);printf(\n平均带权周转时间:%.2f\n\n,sum2/n);break;}case1:{for(j=0;jn;j++){for(i=0;in;i++)if(a[i].id==j+1){printf(%d:%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n,a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}}printf(\n平均周转时间:%.2f\n,sum1/n);printf(\n平均带权周转时间:%.2f\n\n,sum2/n);break;}default:break;}}voidfcfs(){inti,j,n,min,px;floatsum1,sum2;printf(\t请输入有n个进程(0n=50):\t);scanf(%d,&n);while(n50||n=0){printf(n\t请重新输入:);scanf(%d,&n);}printf(\n\n);structGzuo{intid;//进程名字intdt;//到达时刻intst;//服务时间intwct;//完成时刻floatzt;//周转时间floatdczt;//带权周转时间};Gzuoa[N];for(i=0;in;i++){a[i].id=i+1;printf(\t到达时间:);scanf(%d,&a[i].dt);printf(\t服务时间:);scanf(%d,&a[i].st);printf(\n);}for(j=n-1;j=0;j--){for(i=0;ij;i++){if(a[i].dta[i+1].dt){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}a[0].wct=a[0].st+a[0].dt;a[0].zt=(float)a[0].st;a[0].dczt=a[0].zt/a[0].st;for(i=1;in;i++){if(a[i].dta[i-1].wct){a[i].wct=a[i].dt+a[i].st;a[i].zt=(float)a[i].st;a[i].dczt=a[i].zt/a[i].st;}else{a[i].wct=a[i-1].wct+a[i].st;a[i].zt=(float)(a[i].wct-a[i].dt);a[i].dczt=a[i].zt/a[i].st;}}printf(\t1、按id号依次输出\n);printf(\t2、按完成顺序依次输出\n);printf(\n\t请选择输出顺序:\t);scanf(%d,&px);printf(\nid:到达时间\t服务时间\t完成时间\t周转时间\t带权周转时间\n);sum1=0;sum2=0;switch(px){case2:{for(i=0;in;i++){printf(%d:%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n,a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}printf(\n平均周转时间:%.2f\n,sum1/n);printf(\n平均带权周转时间:%.2f\n\n,sum2/n);break;}case1:{for(j=0;jn;j++){for(i=0;in;i++)if(a[i].id==j+1){printf(%d:%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n,a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);sum1+=a[i].zt;sum2+=a[i].dczt;}}printf(\n平均周转时间:%.2f\n,sum1/n);printf(\n平均带权周转时间:%.2f\n\n,sum2/n);break;}default:break;}}voidsjf(){inti,j,n,min,px;intb=0,z;floatsum1,sum2;printf(\n\t\t请输入有n个进程(0n=50):\t);scanf(%d/n,&n);while(n50||n=0){printf(n\t请重新输入:);scanf(%d,&n);}printf(\n);structGzuo{intid;//进程名字intdt;//到达时刻intst;//服务时间intwct;//完成时刻floatzt;//周转时间floatdczt;//带权周转时间};Gzuoa[N];for(i=0;in;i++){a[i].id=i+1;printf(\t到达时间:);scanf(%d,&a[i].dt);printf(\t服务时间:);scanf(%d,&a[i].st);printf(\n);}min=a[0].dt;for(j=n-1;j=0;j--){for(i=0;ij;i++){if(a[i].dta[i+1].dt){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}if(a[i].dt==a[i+1].dt&&a[i].sta[i+1].st){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+1].st;a[i+1].st=min;min=a[i].id;a[i].id=a[i+1].id;a[i+1].id=min;}}}a[0].wct=a[0].st+a[0].dt;a[0].zt=(float)a[0].st;a[0].dczt=a[0].zt/a[0].st;for(i=1;in;i++){if(a[i].dta[0].wct);elseb=b+1;}for(j=b-1;j=1;j--){for(i=1;ij;i++){if(a[i].sta[i+1].st){min=a[i].dt;a[i].dt=a[i+1].dt;a[i+1].dt=min;min=a[i].st;a[i].st=a[i+