用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

c++磁盘存储空间的管理模拟(UNIX存储管理的成组链接法的设计与实现)

2013-03-09 作者: 小蜜锋举报

[c++]代码库

#include<stdio.h>
#include<string.h>
#include<iostream.h>

const int MAXGROUP=10;//定义组的大小
const int MAXJOB=100;//定义一个作业最大能申请的块数

//结构体定义
typedef struct node
{
	int quantity;
	int cell[MAXGROUP];
	struct node *next;
} group;

typedef struct node1
{
	char name[20];
	int quantity;
	int cell[MAXJOB];
	struct node1 *next;
} job;

group *head;
int total;

job *jhead;

//初始化组函数
group *initial()
{
	int i;
	group *p;

	p=new group;

	p->quantity=0;
	p->next=NULL;

	for ( i=0; i<MAXGROUP; i++ )
	{
		p->cell[i]=-1;
	}

	return p;
}

//初始化作业函数
job *initial_job()
{
	int i;
	job *p;

	p=new job;

	strcpy ( p->name,"" );
	p->quantity=0;
	p->next=NULL;

	for ( i=0; i<MAXGROUP; i++ )
	{
		p->cell[i]=-1;
	}

	return p;
}

//读入空闲块流文件
void readData()
{
	FILE *fp;
	char fname[20];
	int temp;
	group *p;

	cout<<"请输入初始空闲块数据文件名:";

	cin>>fname;

	if ( ( fp=fopen ( "5unix.txt","r" ) ) ==NULL )
	{
		cout<<"错误,文件打不开,请检查文件名:)"<<endl;
	}
	else
	{
		cout<<"=================================================="<<endl;
		cout<<"读入的初始空闲块号:";
		while ( !feof ( fp ) )
		{
			fscanf ( fp,"%d ",&temp );
			if ( head->quantity<MAXGROUP )
			{
				head->cell[head->quantity]=temp;
				head->quantity++;
			}
			else
			{
				p=initial();
				p->next=head;
				head=p;
				p->cell[p->quantity]=temp;
				p->quantity++;
			}
			total++;
//输出初始数据
			cout<<temp<<" ";
		}

		cout<<endl<<"总空闲块数:"<<total<<endl;
	}
}

//查看专用块函数
void view()
{
	int i;

	cout<<endl<<"专用块数据如下:"<<endl;
	cout<<"-------------------------------"<<endl;
	cout<<"所存储的空闲块号:";
	for ( i=0; i<head->quantity; i++ )
	{
		cout<<head->cell[i]<<" ";
	}
	cout<<endl<<"专用块空闲块数为:"<<head->quantity;
	cout<<endl<<"总空闲块数:"<<total<<endl;
}


//新申请函数
void bid()
{
	char jobname[20];
	int number;
	int i;
	job *p;

	cout<<"----------------------------------"<<endl;
	cout<<"请输入新专业名:";
	cin>>jobname;
	cout<<"所需内存块数:";
	cin>>number;

	if ( number>total )
	{
		cout<<"所需内存块数大于当前空闲块数,请稍候再试:)"<<endl;
	}
	else
	{
		p=initial_job();
		strcpy ( p->name,jobname );
		p->next=jhead->next;
		jhead->next=p;
		p->quantity=number;
		cout<<"所申请到的空闲块号流:";
		for ( i=0; i<number; i++ )
		{
			if ( head->quantity>1 )
			{
				cout<<head->cell[head->quantity-1]<<" ";
				head->quantity--;
				p->cell[i]=head->cell[head->quantity-1];
			}
			else
			{
				cout<<head->cell[0]<<" ";
				p->cell[i]=head->cell[head->quantity-1];
				head->quantity--;
				if ( head->next!=NULL )
				{
					head=head->next;
				}
			}
			total--;
		}
	}
	cout<<endl;
}

//撤消作业
void finish()
{
	char jobname[20];
	int i;
	job *p,*q;
	group *r;

	cout<<"请输入要撤消的作业名:";
	cin>>jobname;

	q=jhead;
	p=jhead->next;
	while ( ( p!=NULL ) && ( strcmp ( p->name,jobname ) ) )
	{
		q=q->next;
		p=p->next;
	}

	if ( p==NULL )
	{
		cout<<"Sorry,没有该作业"<<endl;
	}
	else
	{
		for ( i=0; i<p->quantity; i++ )
		{
			if ( head->quantity<MAXGROUP )
			{
				head->cell[head->quantity]=p->cell[i];
				head->quantity++;
			}
			else
			{
				r=initial();
				r->next=head;
				head=r;
				r->cell[r->quantity]=p->cell[i];
				r->quantity++;
			}
		}
		total+=p->quantity;

		q->next=p->next;
		delete p;
	}
}

//显示版权信息函数
void version()
{
	cout<<endl<<endl;

	cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
	cout<<" ┃     模拟UNIX的成组链接法        ┃"<<endl;
	cout<<" ┠───────────────────────┨"<<endl;
	cout<<" ┃   (c)All Right Reserved Neo       ┃"<<endl;
	cout<<" ┃      sony006@163.com          ┃"<<endl;
	cout<<" ┃     version 2004 build 1122      ┃"<<endl;
	cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;

	cout<<endl<<endl;
}


void main()
{
	int f=1;
	int chioce;

	version();
	head=initial();
	total=0;
	jhead=initial_job();

	readData();

	while ( f==1 )
	{
		cout<<"===================================================="<<endl;
		cout<<" 模拟UNIX的成组链接法 "<<endl;
		cout<<"===================================================="<<endl;
		cout<<"1.申请空闲块 2.撤消作业 3.查看专用块 0.退出"<<endl;
		cout<<"请选择:";
		cin>>chioce;

		switch ( chioce )
		{
		case 1:
			bid();
			break;
		case 2:
			finish();
			break;
		case 3:
			view();
			break;
		case 0:
			f=0;
			break;
		default:
			cout<<"选择错误!"<<endl;
		}
	}

}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...