作业调度算法02--短作业优先 (操作系统)

作业调度算法02–短作业优先 (操作系统)

输入N(N>0)个作业,输入每个作业的名字,到达时间,服务时间,按照短作业优先算法,计算每个作业的完成时间,周转时间,带权周转时间(保留2位小数)。

输入格式:

第一行输入作业数目,第二行输入作业的名字,第三行输入到达时间,第四行输入服务时间。

输出格式:

按照到达时间从小到大排序,第一行输出作业的名字,第二行输出到达时间,第三行输出服务时间,第四行输出完成时间,第五行输出完成时间,第六行输出带权周转时间。

输入样例:

在这里给出一组输入。例如:

5
A B C D E
0 1 2 3 4
4 3 1 2 4

输出样例:

在这里给出相应的输出。例如:

作 业 名:A B C D E
到达时间:0 1 2 3 4
服务时间:4 3 1 2 4
完成时间:4 10 5 7 14
周转时间:4 9 3 4 10
带权周转时间:1.00 3.00 3.00 2.00 2.50
#include<stdio.h>

typedef struct xian{
    char name;
    double dao,service,finish,zhou;
    double daizhou;
    double indexNumber,beforNumber;
    double wait;
    bool flag;
}xian;

void printfXian(xian xians[],int n);
double findMin(int dao[],int index[],int service[],xian xians[],int sum,double beforeFinish);
double findNext(int index[],int dao[],int number,int sum);
double findMax(xian xians[],int index[],int dao[],int service[],double waitSum[],int sum,int indexOne);

int main(){
    int n,i,j;
    int tmp2=10000,tmp3=0;
    scanf("%d", &n);
    getchar();
    xian xians[n];
    int index[n],service[n],dao[n];
    double waitSum[n];
    for(i=0;i<n;i++){
    	xians[i].wait = 0;
        xians[i].flag = true;
        xians[i].finish = 0;
        xians[i].indexNumber = i;
        index[i] = i;
        scanf("%c",&xians[i].name);
        getchar();  //跳过空格
    }
    for(i=0;i<n;i++){
        scanf("%lf",&xians[i].dao);
        dao[i] = xians[i].dao;
        if(xians[i].dao<tmp2){
        	tmp2 = xians[i].dao;
        	tmp3 = i;
		}
    }
    for(i=0;i<n;i++){
        scanf("%lf",&xians[i].service);
        service[i] = xians[i].service;
        xians[i].beforNumber = tmp3;
    }
    
    double tar;
    char tmp;
    int  max,max2;
       
    for(i=0;i<n;i++){
         if(i==0){
             xians[tmp3].finish=xians[tmp3].dao+xians[tmp3].service;
             xians[tmp3].zhou=xians[tmp3].finish-xians[tmp3].dao;
             xians[tmp3].daizhou = xians[tmp3].zhou / xians[tmp3].service;    
			 xians[tmp3].flag = false;   
         }else {
         	if(i==1)
             max = findMax(xians,index,dao,service,waitSum,n,tmp3);
           
             if(xians[max].dao<=xians[(int)xians[max].beforNumber].finish)
             xians[max].finish = xians[(int)xians[max].beforNumber].finish + xians[max].service;
             else 
             xians[max].finish = xians[max].dao + xians[max].service;
             xians[max].zhou = xians[max].finish - xians[max].dao;
             xians[max].daizhou = xians[max].zhou / xians[max].service;
             xians[max].flag = false;
       
             max2 = findMax(xians,index,dao,service,waitSum,n,max);
             xians[max2].beforNumber = max;
             max = max2;   
         }
    }
     //对xians数组的到达时间进行从小到大排序==>冒泡排序
    for(i=0;i<n-1;i++){
        for(j=0;j<n-1-i;j++){
            if(xians[j].dao>xians[j+1].dao){
                tmp = xians[j].name;
                xians[j].name = xians[j+1].name;
                xians[j+1].name = tmp;
                
                tar = xians[j].dao;
                xians[j].dao = xians[j+1].dao;
                xians[j+1].dao = tar;
                
                tar = xians[j].service;
                xians[j].service = xians[j+1].service;
                xians[j+1].service = tar;
                
                tar = xians[j].finish;
                xians[j].finish = xians[j+1].finish;
                xians[j+1].finish = tar;
                
                tar = xians[j].zhou;
                xians[j].zhou = xians[j+1].zhou;
                xians[j+1].zhou = tar;
                
                tar = xians[j].daizhou;
                xians[j].daizhou = xians[j+1].daizhou;
                xians[j+1].daizhou = tar;
                
            }
        }
    }
      printfXian(xians,n);
     
    
    return 0;
}

void printfXian(xian xians[],int n){
    int i;
   
    printf("作 业 名:");
    for(i=0;i<n;i++){
        if(i==n-1)
        printf("%c\n",xians[i].name);
        else
        printf("%c ",xians[i].name);
    }
    printf("到达时间:");
    for(i=0;i<n;i++){
        if(i==n-1)
        printf("%.0lf\n",xians[i].dao);
        else
        printf("%.0lf ",xians[i].dao);
    }
    printf("服务时间:");
    for(i=0;i<n;i++){
        if(i==n-1)
        printf("%.0lf\n",xians[i].service);
        else
        printf("%.0lf ",xians[i].service);
    }
    printf("完成时间:");
    for(i=0;i<n;i++){
        if(i==n-1)
        printf("%.0lf\n",xians[i].finish);
        else
        printf("%.0lf ",xians[i].finish);
    }
    printf("周转时间:");
    for(i=0;i<n;i++){
        if(i==n-1)
        printf("%.0lf\n",xians[i].zhou);
        else
        printf("%.0lf ",xians[i].zhou);
    }
    printf("带权周转时间:");
    for(i=0;i<n;i++){
        if(i==n-1)
        printf("%.2lf\n",xians[i].daizhou);
        else
        printf("%.2lf ",xians[i].daizhou);
    }
}

double findMin(int dao[],int index[],int service[],xian xians[],int sum,double beforeFinish){
    int i,j,min;
    double number,tar;
    int n = 1;
    bool flag1 = true;
    while(flag1&&n<=5){
        for(i=0;i<n;i++)
        for(j=0;j<sum-1-i;j++){
            if(service[j]<service[j+1]){
            	number = dao[j];
            	dao[j] = dao[j+1];
            	dao[j+1] = number; 
                number = index[j];
                index[j] = index[j+1];
                index[j+1] = number;
                tar = service[j];
                service[j] = service[j+1];
                service[j+1]= tar;
            }
        }
        min = index[sum-n];

        if(xians[min].dao<=(int)beforeFinish&&xians[min].flag == true&&(int)xians[min].finish == 0){
            flag1 = false;
        }
        else n++;
    }
    if(n==6) min = findNext(index,dao,(int)beforeFinish,sum);
    return min;
}

double findNext(int index[],int dao[],int number,int sum){
	int i,j;
	double number1;
	for(i=0;i<sum-1;i++)
	for(j=0;j<sum-1-i;j++){
		if(dao[j]>dao[j+1]){
			number1 = dao[j];
			dao[j] = dao[j+1];
			dao[j+1] = number1;
			number1 = index[j];
			index[j] = index[j+1];
			index[j+1] = number1;
		}
	} 
	for(i=0;i<sum;i++){
		if(number<dao[i]){
			break;
		}
	}
	return index[i];
}

double findMax(xian xians[],int index[],int dao[],int service[],double waitSum[],int sum,int indexOne){
	
	int i;
	double tmp;
	for(i=0;i<sum;i++){
		if(xians[index[i]].finish == 0&&xians[index[i]].dao<=xians[indexOne].finish){
			waitSum[i] = 1 + (xians[indexOne].finish - xians[index[i]].dao) / xians[index[i]].service*1.0;
		}else if(xians[index[i]].finish == 0&&xians[index[i]].dao>xians[indexOne].finish){
			waitSum[i] = -1;
		}
}
		
	for(i=0;i<sum-1;i++){
		if(waitSum[i]>waitSum[i+1]){
			tmp = waitSum[i];
			waitSum[i] = waitSum[i+1];
			waitSum[i+1] = tmp;
			tmp = index[i];
			index[i] = index[i+1];
			index[i+1] = tmp;
			tmp = dao[i];
			dao[i] = dao[i+1];
			dao[i+1] = tmp;
		}
	}
	if(xians[indexOne].finish<xians[index[sum-1]].dao||indexOne == index[sum-1]||xians[index[sum-1]].flag==false){
		for(i=0;i<sum-1;i++){
			if(waitSum[i]==0||waitSum[i+1]==0) break;
			 else if(dao[i]<dao[i+1]){
				tmp = waitSum[i];
				waitSum[i] = waitSum[i+1];
				waitSum[i+1] = tmp;
				tmp = index[i];
				index[i] = index[i+1];
				index[i+1] = tmp;
				tmp = dao[i];
				dao[i] = dao[i+1];
				dao[i+1] = tmp;
			}
			
		}
		if(xians[index[i]].flag == false){
		waitSum[i-1] = 0;
		return index[i-1];	
		}
		else{
			waitSum[i] = 0;
		return index[i];
		}
		
	}    
	waitSum[sum-1] = 0;
	return index[sum-1];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值