| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 740 人关注过本帖
标题:作业调度,求指教,同学习,同进步。
取消只看楼主 加入收藏
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:4 
作业调度,求指教,同学习,同进步。
#include <iostream>
#include <iomanip>
using namespace std;
typedef struct task
{    int id;
    char name[5];//作业名称
    float rtime;//提交时间
    float space;//存储空间
    float btime;//开始时间
    float ftime;//完成时间
    float wtime;//等待时间
    float runtime;//运行时间

}task;
void input(int n,task *a)
{    cout<<"请输入作业编号,名称,提交时间,运行时间,存储空间:"<<endl;

    for(int i=0;i<n;i++)
    {   
        cin>>a[i].id>>a[i].name>>a[i].rtime>>a[i].runtime>>a[i].space;
        a[i].btime=0;
        a[i].wtime=0;
        a[i].ftime=0;
    }

}
void fcfs(int n,task *a)
{
    a[0].btime=a[0].rtime;
    a[0].ftime=a[0].btime+a[0].runtime;
    a[0].wtime=a[0].btime-a[0].rtime;
    for(int j=1;j<n;j++)
    {a[j].btime=a[j-1].ftime;//开始时间
        a[j].ftime=a[j].btime+a[j].runtime;//完成时等待时间
        a[j].wtime=a[j].btime-a[j].rtime;//
    }
}

void sjf(int n,task *a)
{    int j,k,c,b;
    task t;
    a[0].btime=a[0].rtime;
    a[0].ftime=a[0].btime+a[0].runtime;
    a[0].wtime=a[0].btime-a[0].rtime;
    for( j=1;j<n;j++)
    {k=j;
        while(a[k].rtime<=a[j-1].ftime&&k<n)
        {
            k++;
        }
        for(c=j;c<k;c++)
        {
            for(b=c+1;b<k;b++)
            {
                if(a[c].runtime>a[b].runtime)
                {
                    t=a[c];
                    a[c]=a[b];
                    a[b]=t;
                }
            }
        }
        a[j].btime=a[j-1].ftime;
        a[j].ftime=a[j].btime+a[j].runtime;
        a[j].wtime=a[j].btime-a[j].rtime;
        
    }

            
        
}
void smory(int n,task *a)
{int j,k,c,b;
    task t;
    a[0].btime=a[0].rtime;
    a[0].ftime=a[0].btime+a[0].runtime;
    a[0].wtime=a[0].btime-a[0].rtime;
    for( j=1;j<n;j++)
    {k=j;
        while(a[k].rtime<=a[j-1].ftime&&k<n)
        {
            k++;
        }
        for(c=j;c<k;c++)//冒泡算法进行排序
        {
            for(b=c+1;b<k;b++)
            {
                if(a[c].space>a[b].space)
                {
                    t=a[c];
                    a[c]=a[b];
                    a[b]=t;
                }
            }
        }
        a[j].btime=a[j-1].ftime;
        a[j].ftime=a[j].btime+a[j].runtime;
        a[j].wtime=a[j].btime-a[j].rtime;
        
    }
}
void output(int n,task *a)
{    cout<<"id"<<setw(8)<<"name"<<setw(8)<<"btime"<<setw(8)<<"ftime"<<setw(8)<<"wtime"<<endl;
    for(int i=0;i<n;i++)
    cout<<a[i].id<<setw(8)<<a[i].name<<setw(8)<<a[i].btime<<setw(8)<<a[i].ftime<<setw(8)<<a[i].wtime<<endl;
}

int main()
{    task *a;
    int n;
    cout<<"请输入作业总数n:"<<endl;
    cin>>n;
    a=new task [n];
    input(n,a);
    cout<<"先来先服务:"<<endl;
    fcfs(n,a);
    output(n,a);
    cout<<"短作业优先:"<<endl;
    sjf(n,a);
    output(n,a);
    cout<<"存储空间最小:"<<endl;
    smory(n,a);
    output(n,a);
    return 0;
}
   
搜索更多相关主题的帖子: 存储 时间 include 
2012-12-29 10:37
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:0 
来提问,要是不用排序怎么弄啊?,这个写法的时间代价太大啊。没别的意思,只是为了学习。

同学习......同进步....你帮我......我帮你.....上善若水.....
2012-12-29 11:01
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:0 
恩啊,加上排序就是3重循环,时间代价是O(n^3)啊,不用排序行不?

同学习......同进步....你帮我......我帮你.....上善若水.....
2012-12-29 17:31
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:0 
同学习,共进步。

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-01-17 14:25
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:0 
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111166666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666

同学习......同进步....你帮我......我帮你.....上善若水.....
2014-12-03 14:54
快速回复:作业调度,求指教,同学习,同进步。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013039 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved