- #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;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/110120131550.html
来源: http://www.codesnippet.cn/detail/110120131550.html