⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tr.c

📁 模拟实现磁盘空间的分配与回收.打包文件中附有详细的实验报告.
💻 C
字号:
/*本程序顺序分配空间,所以运行后开始几个数字都是'1'*/
#include<string.h>
#include<stdio.h>
#define ROW 12
#define COLUMN 16
#define AREA 192
int a[ROW][COLUMN]={1,1,1,0,0,1,0,1,1,1,0,0,1,0,0,0,
				1,0,1,0,0,0,1,0,1,0,1,0,1,1,1,1,
				1,1,1,0,1,0,1,1,0,1,0,0,1,1,0,1,
				0,0,1,0,1,1,0,1,0,1,1,0,1,0,1,0,
				1,0,1,1,0,1,0,1,0,1,1,1,1,0,1,0,
				1,0,0,1,1,0,1,0,0,0,1,0,1,0,1,0,
				0,1,0,1,1,1,0,1,0,0,0,1,0,1,0,1,
				0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,
				1,0,1,1,1,0,1,1,1,0,0,1,0,0,0,0,
				0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,
				1,1,0,0,1,0,1,1,0,0,0,1,0,1,0,0,
				0,0,0,1,0,0,0,1,1,1,0,1,0,1,1,0,
				};
int b[ROW][COLUMN]={0};int c[AREA][3]={0};int static y;
int input(int p); 
void distribute(int p);void callbackall(void);void callbackpointed(int n);
main()
{
	static int i,j,n,p;
	printf("**********************************");
	printf("********designed by weiwei********");
	printf("**********************************");
	printf("The distribution and callback of disk space.\n");
	printf("press any key to display the status of the disk.\n");
	getchar();
	for(i=0;i<ROW;i++)
	{
		for(j=0;j<COLUMN;j++)
		{
			printf("%d",a[i][j]);
		}
		printf("\n");
	}
		printf("Press any key to apply space from disk.\n");
		getchar();
	
		for(i=0;i<ROW;i++)
		{
			for(j=0;j<COLUMN;j++)
			{
				if(a[i][j])
					continue;
				else
				{
						c[p][0]=p+1;c[p][1]=i*16+j;p++;
				}
			}
		}
		y=p;
			printf("There are %d blocks to use.",p);
		distribute(n);
		printf("\nThe next step is callbacking the space,press any key.\n");
		printf("You have these choices:\n");
		printf("1:\t Free all the disk blocks you apply just now\n");
		printf("2:\t Free a disk block that is destined \n");
		printf("3:\t Exit\n");
		i=input(3);
		while(i)
		{
			switch(i)
			{
			case 1:
				callbackall(n);break;
			case 2:
				callbackpointed(n);
				printf("\n\nYou stil have these choices:\n");
				printf("1:\t free all the disk blocks you apply just now\n");
				printf("2:\t free a pointed disk block\n");
				printf("3:\t esc\n");
				printf("Please make a choice:\n");
				i=input(3);break;
			case 3:
				exit(1);break;
			}
		}
}
int input(int p)
{
	static int k;
	scanf("%d",&k);
	if(k<=0||k>p)
	{
		printf("Wrong input!!!!please enter a number that is greater than 0 and less than %d:\n",p);
		input(p);
	}
	return k;
}
void distribute(int x)
{
	static int i,j,k,p,m,n,q;

	printf("Please enter which disk block do you want to free from.\n");
	k=input(y);/*输入开始释放的量*/
	printf("Please enter how many disk blocks do you want to free.\n");
	printf("You must enssure the data is right\n");
	j=input(y);/*要释放多少个*/
	if(k+j>y)
	{
		printf("Wrong input!!! The blocks you want to distribute is beyond the mark.\n ");
		printf("Please enter another legal number to be the first one and the amount:\n\n ");
		distribute(y);
	}

	else
	{
	for(i=k;i<k+j;i++)
	{
		q=c[i-1][1];
		m=q/16;
		n=q%16;
		p=m*16+n;
		a[m][n]=1;b[m][n]=1;
		printf("%d:\t related block No. %d;cylinder No. %d;track No. %d;physic block No. %d\n",i,p,m,n/4,n%4);
	}

	printf("Press any key to compare this matrix to the last one\n");
		getchar();
	for(i=0;i<ROW;i++)
	{
		for(j=0;j<COLUMN;j++)
		{
			printf("%d",a[i][j]);
		}
		printf("\n");
	}
	}
}

void callbackall(int n)
{
	int i,j,m;
	for(i=0;i<ROW;i++)
	{
		for(j=0;j<COLUMN;j++)
		{
			if(!b[i][j])
				continue;
			else
			{
				a[i][j]=0;
			}
		}
	}
	printf("press any key to compare this matrix to the first one.\n");
		getchar();
		printf("you can see it recruits:\n");

		for(i=0;i<ROW;i++)
		{
			for(j=0;j<COLUMN;j++)
			{
				printf("%d",a[i][j]);
			}
			printf("\n");
		}
}

void callbackpointed(int n)
{
	static int i,k,q,m,j,p;
	printf("Please enter which disk block do you want to free from.\n");
	k=input(n);/*输入开始释放的量*/
	printf("Please enter how many disk blocks do you want to free.\n");
	printf("You must enssure the data is right\n");
	j=input(n);/*要释放多少个*/
	if(k+j>n+1)
	{
		printf("Wrong input!!! The blocks you want to free is beyond the mark.\n ");
		printf("Please enter another legal number to be the first one and the amount:\n\n ");
		callbackpointed(n);
	}
	else
	{
		for(i=k;i<k+j;i++)
		{
				q=c[i-1][1];
				m=q/16;
				p=q%16;
				a[m][p]=0;
				/*c[i-1][3]=1;c[k-1][3]=1表明已经被释放了*/
		}
		printf("The pointed disk block has been freeed,press any key to check the result.\n");
		getchar();
		for(i=0;i<ROW;i++)
		{
			for(j=0;j<COLUMN;j++)
			{
				printf("%d",a[i][j]);
			}
			printf("\n");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -