输入
输入机器数量M,作业数量n程序中定义,每一个作业的运行时间随机生成。
输出
每个作业的运行开始时间结束时间使用的机器编号的列表 mmsp.h文件:
#define N 7
struct JOB {
int jobid;
int runningtime;
int starttime;
int machineid;
int order;
};
struct JOB job[N];
struct MACHINE {
int jobid;
int totaltime;
};
quicksort.h文件:
#include "mmsp.h"
void quicksort(struct JOB job[], int low, int high);
int split(struct JOB job[], int low, int high)
void quicksort(struct JOB a[], int low, int high)
{
int middle;
if (low >= high) return;
middle = split(a, low, high);
quicksort(a, low, middle - 1);
quicksort(a, middle + 1, high);
}
int split(struct JOB a[], int low, int high)
{
int part_element = a[a[low].order].runningtime;
int temp = a[low].order;
for (;;) {
while (low < high && part_element >= a[a[high].order].runningtime)
high--;
if (low >= high) break;
a[low++].order = a[high].order;
while (low < high && a[a[low].order].runningtime >= part_element)
low++;
if (low >= high) break;
a[high--].order = a[low].order;
}
a[high].order = temp;
return high;
}
main.cpp文件:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "quicksort.h"
void getData(struct JOB job[], int n);
void result_output(struct JOB job[], int n)
void mmsp(struct JOB job[], int n, int m);
int main(void)
{
int m;
time_t t;
srand((unsigned)time(&t)); /* 设置随机数起始值 */
getData(job, N); /* 获得数据放入数组a中 */
printf("Unordered datas: \n");
result_output(job, N);
quicksort(job, 0, N - 1);
printf("In sorted order: \n");
result_output(job, N);
printf("\nM=");
scanf_s("%d", &m);
mmsp(job, N, m);//
printf("\nresult: \n");
result_output(job, N);
system("pause");
return 0;
}
void mmsp(struct JOB job[], int n, int m)//
{
MACHINE *machine = 0; //定义MACHINE型结构体数组
machine = (struct MACHINE*)malloc(sizeof(struct MACHINE)*m); //为动态结构体数组分配内存空间
int i;
for (i = 0; i<n; i++) {
if (i<m) {
job[job[i].order].machineid = i;
job[job[i].order].starttime = 0;
machine[i].jobid = job[job[i].order].jobid;
machine[i].totaltime = job[job[i].order].runningtime;
}
else {
int j, p = 0;
int starttime = machine[0].totaltime;
for (j = 1; j<m; j++)
if (machine[j].totaltime < starttime)
p = j;
job[job[i].order].machineid = p;
job[job[i].order].starttime = machine[p].totaltime;
machine[p].jobid = job[job[i].order].jobid;
machine[p].totaltime += job[job[i].order].runningtime;
}
}
}
void getData(struct JOB job[], int n)
{
int i;
for (i = 0; i < n; i++) {
job[i].jobid = i;
job[i].runningtime = 1 + rand() % 30; /* 获得1-30之间的整数值 */
job[i].machineid = 0;
job[i].starttime = 0;
job[i].order = i;
}
}
void result_output(struct JOB job[], int n)
{
int i;
printf("jobid runningtime machineid starttime order\n");
for (i = 0; i < n; i++)
printf("%5d %11d %9d %9d %5d\n", job[i].jobid, job[i].runningtime, job[i].machineid, job[i].starttime, job[i].order);
printf("\n");
}