找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1328|回复: 0
打印 上一主题 下一主题
收起左侧

机器调度算法

[复制链接]
跳转到指定楼层
楼主
ID:189652 发表于 2017-5-28 10:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<iostream.h>
int flag;
typedef struct
{
       int ID;
       double time;
}zuoye;
double min(double x[],int n)
{
       if(n>1){
              double temp=x[1];
flag=1;
              for(inti=2;i<=n;i++)
                     if(x<temp){
                            temp=x;
                            flag=i;
                     }
                     return temp;
       }
       else
       {
              flag=1;
              return x[1];
       }
}
double max(double x[],int n)
{
       if(n>1){
              double temp=x[1];
              for(inti=2;i<=n;i++)
                     if(x>temp){
                            temp=x;
                     }
                     return temp;
       }
       else
       {
              return x[1];
       }
}
void exchange(zuoye &A,zuoye &B)
{
       zuoye temp=A;
       A=B;
       B=temp;
}
void Bubble(zuoye q[],int n)
{
       for(int i=1;i<n;i++)
              for(intj=i+1;j<=n;j++)
                     if(q.time<q[j].time )
                            exchange(q,q[j]);
}
void main()
{
       int i;
       int M,N;
       double Time=0;
       int POINT;
       cout<<"请输入机器数和作业数,要求作业数大于机器数:"<<endl;
       cout<<"机器数:";
       cin>>M;
       cout<<"作业数:";
       cin>>N;
       zuoye *p=new zuoye[N+1];
       if (M>=N)
              cout<<"Error!"<<endl;
       else
       {
              for(i=1;i<=N;i++)
              {  p.ID=i;
              }
              double* jiqi=newdouble[M+1];
              for(i=1;i<=N;i++)
              {cout<<"作业"<<i<<"所需时间是:";
              cin>>p.time;
              }
              Bubble(p,N);
              POINT=1;
              cout<<endl<<endl;
              for( i=1;i<=M;i++)
              {
                     jiqi=p[POINT].time;
                     cout<<"机器"<<i<<"当前正在处理作业"<<p[POINT].ID<<
                            endl<<"该作业所需时间为:"<<p[POINT].time<<endl;
                     POINT++;
                     cout<<endl;
              }
              while(POINT<=N){
                     doubleMIN=min(jiqi,M);
                     Time+=MIN;
                     cout<<MIN<<"分钟过后"<<endl<<"机器"<<flag<<"完成上次作业。开始处理作业:"<<p[POINT].ID<<endl<<
                            "该作业所需时间为:"<<p[POINT].time<<endl;
                     jiqi[flag]=p[POINT].time;
                     POINT++;
                     cout<<endl;
                     
                     for(i=1;i<=M;i++)
                     {
                            if(i!=flag){
                                   jiqi-=MIN;
                                   cout<<"机器"<<i<<"剩余处理时间"<<jiqi<<endl;
                                   cout<<endl;
                            }
                     }
              }
              Time+=max(jiqi,M);
              cout<<endl;
              cout<<"全部作业以处理完毕。消耗总时间为:"<<Time<<endl;
       }
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表