#include #include #include #include using namespace std; vector process; vector partitions; void readFile(vector &process, vector &partitions) { fstream file; file.open("input.txt", ios::in); if (file.is_open()) { int n; file >> n; for (int i = 0; i < n; i++) { int temp; file >> temp; partitions.push_back(temp); } file >> n; for (int i = 0; i < n; i++) { int temp; file >> temp; process.push_back(temp); } } file.close(); } void writeFile(vector partitions, vector allocation) { ofstream file; file.open("Output.txt", ios::out); if (file.is_open()) { file << allocation.size() << "\n"; for (int i = 0; i < allocation.size(); i++) { file << allocation[i] << "\t"; } file << "\n"; file << partitions.size() << "\n"; for (int i = 0; i < partitions.size(); i++) { file << partitions[i] << "\t"; } } file.close(); } void first_fit(vector process, vector partitions) { vector allocation(process.size()); for (int i = 0; i < allocation.size(); i++) { allocation[i] = -1; } for (int i = 0; i < process.size(); i++) { for (int j = 0; j < partitions.size(); j++) { if (process[i] <= partitions[j]) { allocation[i] = j; partitions[j] -= process[i]; break; } } } writeFile(partitions, allocation); } void best_fit(vector process, vector partitions) { vector allocation(process.size()); for (int i = 0; i < allocation.size(); i++) { allocation[i] = -1; } for (int i = 0; i < process.size(); i++) { int min = numeric_limits::max(); int index = -1; for (int j = 0; j < partitions.size(); j++) { if (min > partitions[j] && process[i] <= partitions[j]) { min = partitions[j]; index = j; } } if (index != -1) { allocation[i] = index; partitions[index] -= process[i]; } } writeFile(partitions, allocation); } void worst_fit(vector process, vector partitions) { vector allocation(process.size()); for (int i = 0; i < allocation.size(); i++) { allocation[i] = -1; } for (int i = 0; i < process.size(); i++) { int max = numeric_limits::min(); int index = -1; for (int j = 0; j < partitions.size(); j++) { if (max < partitions[j] && process[i] <= partitions[j]) { max = partitions[j]; index = j; } } if (index != -1) { allocation[i] = index; partitions[index] -= process[i]; } } writeFile(partitions, allocation); } int main() { readFile(process, partitions); // first_fit(process, partitions); // best_fit(process, partitions); worst_fit(process, partitions); return 0; }