-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.cpp
More file actions
78 lines (63 loc) · 2.16 KB
/
2.cpp
File metadata and controls
78 lines (63 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
struct task{
int id;
int servicetime;
int starttime;
int finishtime;
int waittime;
};
typedef struct task task;
void calculateWaitTime(struct task tasks[], int n) {
int i;
int totalwaittime = 0;
for(i = 0; i < n; i++){
if(i==0) tasks[i].starttime = 0;
else tasks[i].starttime = tasks[i-1].finishtime;
tasks[i].finishtime = tasks[i].starttime + tasks[i].servicetime;
tasks[i].waittime = tasks[i].starttime;
totalwaittime += tasks[i].waittime;
printf("任务%d:开始时间:%d,结束时间:%d,等待时间:%d\n", tasks[i].id, tasks[i].starttime, tasks[i].finishtime, tasks[i].waittime);
}
float averagetime = totalwaittime / n;
printf("平均等待时间:%f\n",averagetime);
}
void scheduletasks(struct task tasks[], int n) {
// 排序任务,按照服务时间升序
int i = 0;
int j = 0;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (tasks[j].servicetime > tasks[j + 1].servicetime) {
// 交换两个任务的位置
struct task temp = tasks[j];
tasks[j] = tasks[j + 1];
tasks[j + 1] = temp;
}
}
}
}
int main(){
int i;
//输入任务的数量
printf("请输入任务的个数(不少于5个):");
int n;
scanf("%d",&n);
//检查输入的任务数量是否合法
if(n<5){printf("任务个数不少于5个");return 0;}
//定义一个存储任务结构体的数组
struct task tasks[n];
//循环输入编号对应的CPU占用时间,并存储
for (i = 0; i < n; i++) {
tasks[i].id = i + 1;
printf("输入任务%d的CPU占用时间: ", i + 1);
scanf("%d", &tasks[i].servicetime);
}
//按提交顺序执行
printf("1.按提交顺序执行:\n");
calculateWaitTime(tasks, n);
//按最短平均等待时间执行
printf("2.按最短平均等待时间顺序执行:\n");
scheduletasks(tasks, n);
calculateWaitTime(tasks, n);
return 0;
}