【问题描述】给出某系统中的进程请求表和空闲分配表,系统采用可变分区分配策略。
请采用最佳适应分配算法来处理这些进程序列的请求,分别打印出分区分配前后的空闲分区表以及分配过程。
【输入形式】
【输出形式】
【样例输入】
第一行为空闲分区数目n
接下来n行为空闲分区表(格式同上图一致)
第n+1为请求进程数目m
最后m行尾进程请求表(格式同上图一致)
【样例输出】(
说明:
1. 第1组数据为初始空闲表,第2组数据为按照给定算法进行排序后的空闲分区表,最后一组数据为分配后的分区表,中间为分配过程。
2. 每组数据中,每列两个数字之间用一个空格分开。
3. 如果没有分区满足要求,则输出:“Process * no partition.”
4. 分配后的空闲表输出时,不输入大小为0的分区。
5. 每次在输出空闲分区表时,最上面一行为空行。
)
1 32 100
2 10 150
3 5 200
4 218 220
5 96 530
3 5 200 //首次大小递增后的空闲分区表
2 10 150
1 32 100
5 96 530
4 218 220
Process A use 5 partition.
3 5 200 //分配A后大小递增后的空闲分区表
2 10 150
1 32 100
5 56 570
4 218 220
Process B use 3 partition.
3 0 205 //分配B后递增后的空闲分区表
2 10 150
1 32 100
5 56 570
4 218 220
Process C use 4 partition.
3 0 205 //分配C后递增后的空闲分区表
2 10 150
1 32 100
5 56 570
4 118 320
1 32 100 //所有分配结束后的空闲分区表
2 10 150
4 118 320
5 56 570
代码实现:
#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
class ApplyZone {
public:
ApplyZone(string name,int applyment) {
this->name = name;
this->applyment = applyment;
}
public:
string name;
int applyment;
};
class EmptyZone {
public:
EmptyZone(int i,int capacity,int begin) {
Zone = i;
Capacity = capacity;
Begining = begin;
}
public:
int Zone;
int Capacity;
int Begining;
};
bool Mycompare0(EmptyZone &p1, EmptyZone &p2) {
return p1.Capacity < p2.Capacity;
}
bool Mycompare2(EmptyZone& p1, EmptyZone& p2) {
return p1.Begining < p2.Begining;
}
void BestFit() {
vector<EmptyZone>p; int zone, capacity, begin,m,n;
ifstream txtfile;
txtfile.open("test.txt");
txtfile >> n;
for (int i = 0; i < n; i++) {
txtfile >> zone >> capacity >> begin;
EmptyZone* z = new EmptyZone(zone, capacity, begin);
p.push_back(*z);
}
txtfile >> m;
vector<ApplyZone>Az; string name; int applyment;
for (int i = 0; i < m; i++) {
txtfile >> name >> applyment;
ApplyZone* az = new ApplyZone(name, applyment);
Az.push_back(*az);
}
cout<<endl;
for (vector<EmptyZone>::iterator it = p.begin(); it != p.end(); it++) {
cout << (*it).Zone << " " << (*it).Capacity << " " << (*it).Begining << endl;
}
cout << endl;
sort(p.begin(), p.end(), Mycompare0);
for (vector<EmptyZone>::iterator it = p.begin(); it != p.end(); it++) {
cout << (*it).Zone << " " << (*it).Capacity << " " << (*it).Begining << endl;
}
bool isfind; int zone0;
for (vector<ApplyZone>::iterator it = Az.begin(); it != Az.end(); it++) {
isfind = 0;
for (vector<EmptyZone>::iterator it0 = p.begin(); it0 != p.end(); it0++) {
if ((*it0).Capacity >= (*it).applyment) {
(*it0).Capacity -= (*it).applyment;
(*it0).Begining += (*it).applyment;
isfind = 1;
zone0 = (*it0).Zone;
break;
}
}
if (isfind) {
cout << "Process " << (*it).name << " use " << zone0 << " partition." << endl;
cout << endl;
sort(p.begin(), p.end(), Mycompare0);
for (vector<EmptyZone>::iterator it1 = p.begin(); it1 != p.end(); it1++) {
cout << (*it1).Zone << " " << (*it1).Capacity << " " << (*it1).Begining << endl;
}
}
else {
cout << "Process "<<(*it).name<<" no partition." << endl;
}
}
cout << endl;
sort(p.begin(), p.end(), Mycompare2);
for (vector<EmptyZone>::iterator it = p.begin(); it != p.end(); it++) {
if((*it).Capacity!=0){
cout << (*it).Zone << " " << (*it).Capacity << " " << (*it).Begining << " " << endl;
}
}
}
int main() {
BestFit();
return 0;
}