给#include<iostream> #include<stdlib.h> #include<time.h> #include<stdio.h> using namespace std; void init(int map[500][500],int row,int col) { //初始化函数 int i,j,temp; temp = time(NULL); srand(temp); for(i = 0; i<row; i++) { for(j=0; j<col; j++) { map[i][j] = rand()%2; cout<<map[i][j]<<" "; } cout<<endl; } } void fenpei(int map[500][500],int row,int col) { //分配块号函数 int n,count=0,i,j,b; cout<<"输入需要的盘块数:"<<endl; cin>>n; for( i =0; i<row; i++) for( j=0; j<col; j++) { if(count<n) { if ( map[i][j] == 0 ) { map[i][j]=1; b = (i)* col +j; cout<<"第"<<++b<<"盘块被分配!"<<endl; count++; } } else break; } cout<<"分配后的信息:"<<endl; int num=0; for(i=0; i<row; i++) { for(j=0; j<col; j++) { if(map[i][j]==0) num++; cout<<map[i][j]<<" "; } cout<<endl; } cout<<"共有"<<num<<"个空闲块"<<endl; } void huishou(int map[500][500],int row,int col) { int n,count=0,i,j; cout<<"输入需要回收的盘块号:"<<endl; cin>>n; if( n>=row*col ) cout<<"你输入的盘块号不存在!"<<endl; else { i = n / col; j = n % col; if(map[i][j] ==0) cout<<"你输入的盘块号已空闲!"<<endl; else map[i][j]=0; } cout<<"分配后的信息:"<<endl; for(i=0; i<row; i++) { for(j=0; j<col; j++) cout<<map[i][j]<<" "; cout<<endl; } } int main() { int row,col,type; int map[500][500]; cout<<"请输入位示图的行和列:不能超过500!"<<endl; cin>>row>>col; init(map,row,col); while(1) { cout<<"请输入操作类型,1.分配.2:回收.3:退出!"<<endl; cin>>type; if(type==1) fenpei(map,row,col); else if(type == 2) huishou(map,row,col); else return 0; } return 0; }
时间: 2024-03-09 17:51:47 浏览: 25
以下是代码的注释:
```c++
#include<iostream> // 标准输入输出流
#include<stdlib.h> // 包含 rand 函数和 srand 函数
#include<time.h> // 包含 time 函数
#include<stdio.h> // 标准输入输出流
using namespace std; // 命名空间
void init(int map[500][500],int row,int col) { // 初始化函数
int i,j,temp;
temp = time(NULL); // 获取当前时间戳
srand(temp); // 用时间戳来初始化随机种子
for(i = 0; i<row; i++) {
for(j=0; j<col; j++) {
map[i][j] = rand()%2; // 生成随机数,为 0 或 1
cout<<map[i][j]<<" "; // 输出每个随机数
}
cout<<endl; // 输出换行符
}
}
void fenpei(int map[500][500],int row,int col) { // 分配块号函数
int n,count=0,i,j,b;
cout<<"输入需要的盘块数:"<<endl; // 提示输入需要的盘块数
cin>>n; // 读取输入的盘块数
for(i =0; i<row; i++)
for(j=0; j<col; j++) {
if(count<n) {
if ( map[i][j] == 0 ) { // 如果该盘块未被分配
map[i][j]=1; // 将该盘块状态改为已分配
b = (i)* col +j; // 计算盘块号
cout<<"第"<<++b<<"盘块被分配!"<<endl; // 输出已分配的盘块号
count++; // 计数器加 1
}
} else
break;
}
cout<<"分配后的信息:"<<endl; // 输出分配后的信息
int num=0;
for(i=0; i<row; i++) {
for(j=0; j<col; j++) {
if(map[i][j]==0) // 如果该盘块未被分配
num++; // 统计空闲盘块的数量
cout<<map[i][j]<<" "; // 输出每个盘块的状态
}
cout<<endl; // 输出换行符
}
cout<<"共有"<<num<<"个空闲块"<<endl; // 输出空闲盘块的数量
}
void huishou(int map[500][500],int row,int col) { // 回收盘块函数
int n,count=0,i,j;
cout<<"输入需要回收的盘块号:"<<endl; // 提示输入需要回收的盘块号
cin>>n; // 读取输入的盘块号
if( n>=row*col )
cout<<"你输入的盘块号不存在!"<<endl; // 提示输入错误
else {
i = n / col; // 计算盘块的行号
j = n % col; // 计算盘块的列号
if(map[i][j] ==0)
cout<<"你输入的盘块号已空闲!"<<endl; // 提示该盘块已空闲
else
map[i][j]=0; // 将该盘块状态改为未分配
}
cout<<"分配后的信息:"<<endl; // 输出分配后的信息
for(i=0; i<row; i++) {
for(j=0; j<col; j++)
cout<<map[i][j]<<" "; // 输出每个盘块的状态
cout<<endl; // 输出换行符
}
}
int main() {
int row,col,type;
int map[500][500]; // 定义表示位示图的二维数组
cout<<"请输入位示图的行和列:不能超过500!"<<endl; // 提示输入位示图的行和列
cin>>row>>col; // 读取输入的行和列
init(map,row,col); // 调用初始化函数
while(1) { // 循环进行分配和回收操作
cout<<"请输入操作类型,1.分配.2:回收.3:退出!"<<endl; // 提示输入操作类型
cin>>type; // 读取输入的操作类型
if(type==1)
fenpei(map,row,col); // 进行分配操作
else if(type == 2)
huishou(map,row,col); // 进行回收操作
else
return 0; // 退出程序
}
return 0; // 返回 0 表示程序正常结束
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)