C++创建一个八行八列的数组并且初值全赋为0,要求实现四个功能:1.显示位示图;2.文件分配;3.文件收回;4.退出。给出代码(用C++实现)
时间: 2024-03-01 15:51:49 浏览: 19
以下是用C++实现的八行八列数组,并实现了四个功能的代码:
```cpp
#include <iostream>
#include <fstream>
using namespace std;
const int ROW = 8;
const int COL = 8;
int bitmap[ROW][COL] = {0};
void display_bitmap() {
cout << "Bit Map:" << endl;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
cout << bitmap[i][j] << " ";
}
cout << endl;
}
}
void file_allocation() {
int row, col;
cout << "Enter the row and column number to allocate file: ";
cin >> row >> col;
if (bitmap[row][col] == 0) {
bitmap[row][col] = 1;
cout << "File allocated successfully!" << endl;
} else {
cout << "File already allocated at this location!" << endl;
}
}
void file_recovery() {
int row, col;
cout << "Enter the row and column number to recover file: ";
cin >> row >> col;
if (bitmap[row][col] == 1) {
bitmap[row][col] = 0;
cout << "File recovered successfully!" << endl;
} else {
cout << "No file found at this location!" << endl;
}
}
int main() {
int choice;
do {
cout << "Select an option:" << endl;
cout << "1. Display Bitmap" << endl;
cout << "2. File Allocation" << endl;
cout << "3. File Recovery" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
display_bitmap();
break;
case 2:
file_allocation();
break;
case 3:
file_recovery();
break;
case 4:
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice! Please select a valid option." << endl;
}
} while (choice != 4);
return 0;
}
```
该程序中,`bitmap`数组表示位示图,初始值全为0,每个元素表示一个磁盘块是否被占用。`display_bitmap`函数用于显示位示图,`file_allocation`函数用于实现文件分配,`file_recovery`函数用于实现文件收回。在`main`函数中,通过循环提供四个选项,当用户选择退出时,程序结束。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)