操作系统-c语言实现空闲块表的存储空间的分配和回收 ...

#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>

void init(int map[20][20],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[20][20],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;
	for(i=0;i<row;i++)
	{
		for(j=0;j<col;j++)
			cout<<map[i][j]<<"   ";
		cout<<endl;
	}

}

void huishou(int map[20][20],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;
	}
}
void main()
{
	int row,col,type;
	int map[20][20];
	cout<<"请输入位示图的行和列:不能超过20!"<<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;
	
	}

}





转载于:https://www.cnblogs.com/szm2019/p/7140910.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值