源代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct jcb {
char name[10];
char status;
int arrTime;
int reqTime;
int waiTime;
int startTime;
int finishTime;
int finish;
float TAtime, TAWtime;
float prio;
}jcb;
jcb jobArr[24], jobFin[24], job[24];
int systime=0;
int intArr, intFin, intJob;
void InputData(int);
void SortByArrTime(int, jcb*);
void DisPlay(int,int);
void Insert(int);
int Delete(int);
int DeleteTheUsedJobById(jcb, int);
void FCFS(int);
void SelectMethod(int);
void SortByReqTime(int,jcb*);
void SJF(int);
int FindTheShortArrTimeJobArray(int, int);
void check();
void runing(int, int, int, int, int);
int HRN(int);
void AvgTime(int);
int main() {
int scase;
printf("作业个数:");
scanf("%d",&intArr);
InputData(intArr);
SortByArrTime(intArr, jobArr);
DisPlay(intArr,0);
while(1) {
printf("
插入(1) 删除(2) 选择算法(3) 退出(4)
对应数字输入:");
fflush(stdin);
scanf("%d",&scase);
switch(scase) {
case 1:
intArr++;
Insert(intArr);
break;
case 2:
intArr = Delete(intArr);
printf("
删除且");
DisPlay(intArr,0);
break;
case 3:
SelectMethod(intArr);
break;
case 4:
exit(1);
break;
default:
break;
}
}
}
void InputData(int n) {
int i=0;
int temp = 0;
while(n) {
printf("
第%d个作业",i+1);
printf("
输入作业名:");
scanf("%s",jobArr[i].name);
printf("到达时间:");
scanf("%d",&jobArr[i].arrTime);
printf("要求服务时间:");
scanf("%d",&jobArr[i].reqTime);
i++;
n--;
}
}
void SortByArrTime(int n, jcb temp[24]) {
int i, j;
jcb temp1;
for (j = 0; j < n - 1; j++) {
for (i = 0; i < n - 1 - j; i++)
{
if(temp[i].arrTime > temp[i + 1].arrTime)
{
temp1 = temp[i];
temp[i] = temp[i+1];
temp[i+1] = temp1;
}
}
}
}
void DisPlay(int n, int s) {
int i=0;
switch(s) {
case 0:
printf("
按到达时间排序后,队列为:
");
printf(" name artime rqtime
");
while(n) {
printf("N%d %s %d %d
",i+1,jobArr[i].name,jobArr[i].arrTime,jobArr[i].reqTime);
i++;
n--;
}
break;
case 1:
printf(" name artime finTime rqtime TAtime TAWtime
");
while(n) {
printf("N%d %s %d %d %d %f %f
",i+1,jobArr[i].name,jobArr[i].arrTime,jobArr[i].finishTime,jobArr[i].reqTime,jobArr[i].TAtime,jobArr[i].TAWtime);
i++;
n--;
}
AvgTime(intArr);
break;
case 2:
printf(" name artime finTime rqtime TAtime TAWtime
");
while(n) {
printf("N%d %s %d %d %d %f %f
",i+1,jobArr[n-1].name,jobArr[n-1].arrTime,jobArr[n-1].finishTime,jobArr[n-1].reqTime,jobArr[n-1].TAtime,jobArr[n-1].TAWtime);
i++;
n--;
}
AvgTime(intArr);
break;
}
}
void AvgTime(int n){
float argTAtime = 0;
float argTAWtime = 0;
for(int i=0; i<n; i++) {
argTAtime += jobArr[i].TAtime;
argTAWtime += jobArr[i].TAWtime;
}
argTAtime /= n;
argTAWtime /= n;
printf("
平均周转时间:%f
",argTAtime);
printf("带权平均周转时间:%f",argTAWtime);
}
void Insert(int n) {
printf("
第%d个作业",n);
printf("
输入作业名:");
scanf("%s",jobArr[n-1].name);
printf("
到达时间:");
scanf("%d",&jobArr[n-1].arrTime);
printf("
要求服务时间:");
scanf("%d",&jobArr[n-1].reqTime);
}
void FCFS(int n) {
int i=0;
int arrfinish=0;
arrfinish = jobArr[i].arrTime + jobArr[i].reqTime;
jobArr[i].finishTime = arrfinish;
jobArr[i].TAtime = arrfinish - jobArr[i].arrTime;
jobArr[i].TAWtime = jobArr[i].TAtime/jobArr[i].reqTime;
while(n--){
i++;
if(arrfinish <= jobArr[i].arrTime)
arrfinish = jobArr[i].arrTime;
arrfinish = arrfinish + jobArr[i].reqTime;
jobArr[i].finishTime = arrfinish;
jobArr[i].TAtime = arrfinish - jobArr[i].arrTime;
jobArr[i].TAWtime = jobArr[i].TAtime/jobArr[i].reqTime;
}
}
void SJF(int n) {
int i = 0;
int arrfinish = 0;
int finArrCount = 0;
arrfinish = jobArr[i].arrTime + jobArr[i].reqTime;
jobArr[i].finishTime = arrfinish;
jobArr[i].TAtime = arrfinish - jobArr[i].arrTime;
jobArr[i].TAWtime = jobArr[i].TAtime/jobArr[i].reqTime;
n = DeleteTheUsedJobById(jobArr[i], n);
while(n) {
//得到jobFin数组元素个数
finArrCount = FindTheShortArrTimeJobArray(n, arrfinish);
if(finArrCount == 0) {
SortByArrTime(n, jobArr);
if(arrfinish <= jobArr[i].arrTime)
arrfinish = jobArr[i].arrTime + jobArr[i].reqTime;
else
arrfinish = arrfinish + jobArr[i].reqTime;
jobArr[i].finishTime = arrfinish;
jobArr[i].TAtime = arrfinish - jobArr[i].arrTime;
jobArr[i].TAWtime = jobArr[i].TAtime/jobArr[i].reqTime;
n = DeleteTheUsedJobById(jobArr[i], n);
continue;
}
while(finArrCount) { //2
//i = 0;
arrfinish = arrfinish + jobFin[i].reqTime;
jobFin[i].finishTime = arrfinish;
jobFin[i].TAtime = arrfinish - jobFin[i].arrTime;
jobFin[i].TAWtime = jobFin[i].TAtime/jobFin[i].reqTime;
for(int j=0; j < n; j++) {
if(strcmp(jobFin[i].name,jobArr[j].name) == 0) {
jobArr[j] = jobFin[i];
}
}
n = DeleteTheUsedJobById(jobFin[i], n);
i++;
finArrCount--;
}
i=0;
}
}
//组织服务时间最短的作业数组
int FindTheShortArrTimeJobArray(int n, int arrfinish) {
jcb temp;
int finArrCount = 0;
temp = jobArr[0];
for(int i=0; i < n; i++) {
if(jobArr[i].arrTime <= arrfinish) {
jobFin[finArrCount] = jobArr[i];
finArrCount++;
}
}
SortByReqTime(finArrCount, jobFin);
return finArrCount;
}
//通过服务时间排序
void SortByReqTime(int n, jcb temp1[24]) {
int i, j;
jcb temp;
for (j = 0; j < n - 1; j++) {
for (i = 0; i < n - 1 - j; i++)
{
if(temp1[i].reqTime > temp1[i + 1].reqTime)
{
temp = temp1[i];
temp1[i] = temp1[i+1];
temp1[i+1] = temp;
}
}
}
}
//通过id删除
int DeleteTheUsedJobById(jcb idJob, int n) {
jcb temp;
for(int i=0; i < n; i++) {
if(strcmp(idJob.name,jobArr[i].name) == 0)
break;
}
for( ; i < n - 1; i++) {
temp = jobArr[i];
jobArr[i] = jobArr[i+1];
jobArr[i+1] = temp;
}
return n-1;
}
int Delete(int n) {
char name[10];
jcb temp;
printf("
删除哪一个作业,请输入作业名:");
scanf(" %d",&name);
for(int i=0; i < n; i++) {
if(strcmp(jobArr[i].name, name)==0) {
for(int j=i; i<n; i++) {
temp = jobArr[i];
jobArr[i] = jobArr[i+1];
jobArr[i+1] = temp;
}
return n-1;
break;
}
}
return n;
}
void SelectMethod(int n) {
int s;
printf("
请选择调用的算法:
");
printf(" FCFS(1) SJF(2) (3)HRRN
对应数字输入:");
scanf(" %d",&s);
switch(s) {
case 1:
SortByArrTime(intArr, jobArr);
FCFS(n);
printf("
FCFS算法:
");
DisPlay(n,1);
break;
case 2:
SJF(n);
printf("
SJF算法:
");
DisPlay(n,2);
break;
case 3:
check();
printf("
SJF算法:
");
DisPlay(n,1);
break;
default:
printf("
输入错误!
");
}
}
void check() {
int i;
int staTime, endTime, sumTAtime=0.0, sumTAWtime=0.0, aveTAtime, aveTAWtime;
int current=0, times=0, pre=0; jobArr[pre].finishTime=0;
for(i=0; i<intArr; i++)
{
jobArr[i].finish=0;
}
staTime, endTime,sumTAtime=0.0, sumTAWtime=0.0, aveTAtime,aveTAWtime;
current=0; times=0;
pre=0;
jobArr[pre].finishTime=0;
for(i=0; i<intArr; i++)
{
jobArr[i].finish=0;
}
staTime, endTime,sumTAtime=0.0, sumTAWtime=0.0, aveTAtime, aveTAWtime;
current=0; times=0; pre=0; jobArr[pre].finishTime=0;
for(times=0; times<intArr; times++)
{
current=HRN(pre);
runing(current, times, pre, staTime, endTime);
pre=current;
}
for(i=0; i<intArr; i++)
{
sumTAtime+=jobArr[i].TAtime;
sumTAWtime+=jobArr[i].TAWtime;
}
aveTAtime=sumTAtime/intArr; aveTAWtime=sumTAWtime/intArr;
}
void runing(int i, int times, int pre, int staTime, int endTime) {
if(times==0)
{
jobArr[i].startTime=jobArr[i].arrTime;
jobArr[i].finishTime=jobArr[i].startTime+jobArr[i].reqTime;
jobArr[i].TAtime=jobArr[i].reqTime;
jobArr[i].TAWtime=1.0;
staTime=jobArr[i].startTime;
}else{
if(jobArr[i].arrTime>jobArr[pre].finishTime)
jobArr[i].startTime=jobArr[i].arrTime;
else
jobArr[i].startTime=jobArr[pre].finishTime;
jobArr[i].finishTime=jobArr[i].startTime+jobArr[i].reqTime;
jobArr[i].TAtime=jobArr[i].finishTime-jobArr[i].arrTime;
jobArr[i].TAWtime=jobArr[i].TAtime/jobArr[i].reqTime;
}
if(times==intArr-1)
endTime=jobArr[i].finishTime;
jobArr[i].finish=1;
}
int HRN(int pre) {
int current=1,i,j;
for(i=0; i<intArr; i++) {
jobArr[i].waiTime=jobArr[pre].finishTime-jobArr[i].arrTime; /*等待时间 =上一个业的完成时间-到达时间*/
jobArr[i].prio=(jobArr[i].waiTime+jobArr[i].reqTime)/jobArr[i].reqTime;
}
for(i=0; i<intArr; i++)
{
if(!jobArr[i].finish)
{
current=i; /*找到第一个还没完成的作业*/
break;
}
}
for( j=i; j<intArr; j++) /*和后面的作业比较*/
{
if( !jobArr[j].finish) /* 还没完成(运行)*/
{
if(jobArr[current].arrTime<=jobArr[pre].finishTime) /*如果作业在上一个作业完成之前到达*/
{
if(jobArr[j].arrTime<=jobArr[pre].finishTime && jobArr[j].prio>jobArr[current].prio )
current=j;/* 找出到达时间在上一个作业完成之前,优先权高的作业*/
}
else /* 如果作业是在上一个作业完成之后到达*/
{
if(jobArr[j].arrTime<jobArr[current].arrTime)
current=j; /* 找出比较早到达的一个*/
if(jobArr[j].arrTime==jobArr[current].arrTime) /* 如果同时到达*/
if(jobArr[j].prio>jobArr[current].prio)
current=j; /*找出服务时间比较短的一个*/
}
}
}
return current;/*返回当前作业*/
}
测试结果:
-FCFS
-SJF
-HRN