c++代码完成基于顺序搜索的动态分区分配算法编写并完成动态分区分配算法的图形式交互界面设计
时间: 2023-10-12 20:11:03 浏览: 101
基于c语言实现动态分区分配算法【100011543】
以下是基于顺序搜索的动态分区分配算法的C++代码:
```c++
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
struct MemoryBlock {
int startAddr, size;
string processName;
};
vector<MemoryBlock> memory;
// 初始化内存
void initMemory(int memorySize) {
MemoryBlock m;
m.startAddr = 0;
m.size = memorySize;
m.processName = "";
memory.push_back(m);
}
// 遍历内存块
void traverseMemory() {
cout << "Memory Status:" << endl;
for (int i = 0; i < memory.size(); i++) {
cout << "Start Addr: " << memory[i].startAddr << " Size: " << memory[i].size;
if (memory[i].processName != "") {
cout << " Process Name: " << memory[i].processName << endl;
}
else {
cout << endl;
}
}
cout << endl;
}
// 分配内存
void allocateMemory(string processName, int processSize) {
for (int i = 0; i < memory.size(); i++) {
if (memory[i].processName == "" && memory[i].size >= processSize) {
MemoryBlock m;
m.startAddr = memory[i].startAddr;
m.size = processSize;
m.processName = processName;
memory[i].startAddr += processSize;
memory[i].size -= processSize;
memory.insert(memory.begin() + i, m);
return;
}
}
cout << processName << " allocation failed, no enough memory." << endl;
}
// 释放内存
void freeMemory(string processName) {
for (int i = 0; i < memory.size(); i++) {
if (memory[i].processName == processName) {
memory[i].processName = "";
// 如果前后两个内存块都为空,合并两个内存块
if (i > 0 && memory[i - 1].processName == "") {
memory[i - 1].size += memory[i].size;
memory.erase(memory.begin() + i);
}
if (i < memory.size() - 1 && memory[i + 1].processName == "") {
memory[i].size += memory[i + 1].size;
memory.erase(memory.begin() + i + 1);
}
return;
}
}
cout << processName << " not found, free failed." << endl;
}
int main() {
int memorySize, choice, processSize;
string processName;
cout << "Input the memory size: ";
cin >> memorySize;
initMemory(memorySize);
while (true) {
cout << "1. Traverse memory" << endl;
cout << "2. Allocate memory" << endl;
cout << "3. Free memory" << endl;
cout << "4. Exit" << endl;
cout << "Please select: ";
cin >> choice;
switch (choice) {
case 1:
traverseMemory();
break;
case 2:
cout << "Input the process name: ";
cin >> processName;
cout << "Input the process size: ";
cin >> processSize;
allocateMemory(processName, processSize);
break;
case 3:
cout << "Input the process name: ";
cin >> processName;
freeMemory(processName);
break;
case 4:
return 0;
default:
cout << "Invalid choice." << endl;
break;
}
}
return 0;
}
```
以下是基于顺序搜索的动态分区分配算法的图形式交互界面设计:
```
+----------------------------------+
| |
| Dynamic Partition Alloca- |
| tion Algorithm |
| |
+----------------------------------+
| |
| Memory Size: [ 1024 ] |
| Process Name: [ ] |
| Process Size: [ ] |
| |
| 1. Traverse memory |
| 2. Allocate memory |
| 3. Free memory |
| 4. Exit |
| |
+----------------------------------+
| |
| Status Bar |
| |
+----------------------------------+
```
界面中有一个文本框用于输入内存大小,以及两个文本框用于输入进程名和进程大小。下面有四个按钮,分别是遍历内存、分配内存、释放内存和退出程序。在界面底部,会显示一些状态信息,如分配成功或失败的提示信息,以及内存状态的详细信息。
阅读全文