码迷,mamicode.com
首页 > 其他好文 > 详细

实验二 作业调度模拟程序(终)

时间:2015-05-08 23:21:25      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

//先来先服务算法
#include <stdio.h>
#include <stdlib.h>
#define N 24
main()
{
    int i,n;
    int reachtime[N],needtime[N],starttime[N],finishtime[N],cycletime[N];//周转时间
    float cltime[N];//带权周转时间
    char name[N];
    printf("\n 请输入作业的个数:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
      printf("\n name:");
      scanf("%s",&name[i]);
      printf("\n reachtime:");
      scanf("%d",&reachtime[i]);
      printf("\n needtime:");
      scanf("%d",&needtime[i]);
      if(i==0)
      {
        starttime[i]=reachtime[i];
      }
      finishtime[i]=starttime[i]+needtime[i];
      cycletime[i]=finishtime[i]-reachtime[i];
      cltime[i]=cycletime[i]*1.0/needtime[i];
      starttime[i+1]=finishtime[i];
    }
    printf("\n\tname  reachtime  needtime  startime  finishtime  cycletime  cltime  ");
    for(i=0;i<n;i++)
    {
        printf("\n\t %c\t  %d\t   %d\t      %d\t        %d\t      %d\t      %.2f",name[i],reachtime[i],needtime[i],
            starttime[i],finishtime[i],cycletime[i],cltime[i]);
    }
}

运行结果:
技术分享

//最短作业优先算法
#include <stdio.h>
#include <stdlib.h>
#define N 24
main()
 {
    int i,j,n,temp;
    int reachtime[N],needtime[N],starttime[N],finishtime[N],cycletime[N];
    float cltime[N];
    char name[N],ctemp;
    printf("\n 请输入作业的个数:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
      printf("\n name:");
      scanf("%s",&name[i]);
      printf("\n reachtime:");
      scanf("%d",&reachtime[i]);
      printf("\n needtime:");
      scanf("%d",&needtime[i]);
      
    }
     
     for(i=0;i<n;i++)
     {
        for(j=1;j<n-1-i;j++)
        {
            if(needtime[j] > needtime[j+1])
            {
                temp = needtime[j+1];
                needtime[j+1] = needtime[j];
                needtime[j] = temp;
                ctemp=name[j+1];
                name[j+1]=name[j];
                name[j]=ctemp;
                temp=reachtime[j+1];
                reachtime[j+1]=reachtime[j];
                reachtime[j]=temp;
            }
            else if(needtime[j]==needtime[j+1]&&reachtime[j]>reachtime[j+1])
            {
                temp = needtime[j+1];
                needtime[j+1] = needtime[j];
                needtime[j] = temp;
                ctemp=name[j+1];
                name[j+1]=name[j];
                name[j]=ctemp;
                temp=reachtime[j+1];
                reachtime[j+1]=reachtime[j];
                reachtime[j]=temp;
            }
        }
    }
    for(i=0;i<n;i++)
    {
                if(i==0)
                {
                  starttime[i]=reachtime[i];
                }
                 finishtime[i]=starttime[i]+needtime[i];
                 cycletime[i]=finishtime[i]-reachtime[i];
                 cltime[i]=cycletime[i]*1.0/needtime[i];
                 starttime[i+1]=finishtime[i];
            }

    printf("\n\tname  reachtime  needtime  startime  finishtime  cycletime  cltime  ");
    for(i=0;i<n;i++)
    {
        printf("\n\t %c\t  %d\t   %d\t      %d\t        %d\t      %d\t     %.2f",name[i],reachtime[i],needtime[i],
            starttime[i],finishtime[i],cycletime[i],cltime[i]);
    }
}

运行结果:
技术分享

实验二 作业调度模拟程序(终)

标签:

原文地址:http://www.cnblogs.com/summer--liumengyun/p/4488919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!