📄 fenqu.cpp
字号:
// fenqu.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
typedef struct table
{
float adress;
float length;
int flag;//标识是空表目(0)还是非空表目(1)
table *next;
}table,*tablep;
void paixu(tablep &Q)
{//按照非空表目的地址由低到高进行排序
float temp1,temp2;
tablep p,q,small;
p=Q->next;
for(p;p->next!=NULL;p=p->next)
{
if(p->flag==1)
{
small=p;
for(q=p->next;q;q=q->next)
if(q->flag==1&&q->adress<small->adress)
small=q;
if(small!=p)
{//由于比较的都是flag=1的记录,所以不需要对flag进行交换
temp1=p->adress;
temp2=p->length;
p->adress=small->adress;
p->length=small->length;
small->adress=temp1;
small->length=temp2;
}
}
}
}
void display(tablep &Q)
{//把表中的信息显示出来
if(Q->next==NULL)//若分配则有提示信息
cout<<"主存还未进行分配"<<endl;
else
{
tablep p;
int i=1;
p=Q->next;
cout<<endl;
while(p!=NULL)
{
cout<<"第"<<i<<"个";
if(p->flag==0)
cout<<" 为空表目"<<endl;
else
cout<<" 地址:"<<p->adress<<" 长度"<<p->length<<endl;
p=p->next;
i++;
}
}
}
void create(tablep &use,tablep &free)
{//创建已分配表和空闲区表
int a,i;
tablep use1,free1;
use1=use;
free1=free;
cout<<endl<<endl<<endl<<endl;
cout<<" **************************************************"<<endl;
cout<<" * 欢迎使用本系统 *"<<endl;
cout<<" **************************************************"<<endl;
cout<<"请输入您需要已分配表的长度:";
cin>>a;
for(i=0;i<a;i++)
{
tablep p;
p=new table;
cout<<"第"<<i+1<<"条是否是空表目?(0表示为空,1表示不为空)";
cin>>p->flag;
if(p->flag==1)
{
cout<<"该作业的起始地址是多少?"<<endl;
cin>>p->adress;
cout<<"该作业的长度是多少?"<<endl;
cin>>p->length;
}
p->next=NULL;
use->next=p;
use=use->next;
}
use=use1;
cout<<endl<<"请输入您需要空闲表的长度:";
cin>>a;
for(i=0;i<a;i++)
{
tablep p;
p=new table;
cout<<"第"<<i+1<<"条是否是空表目?(0表示为空,1表示不为空)";
cin>>p->flag;
if(p->flag==1)
{
cout<<"该空闲区的起始地址是多少?";
cin>>p->adress;
cout<<"该空闲区的长度是多少?";
cin>>p->length;
}
p->next=NULL;
free->next=p;
free=free->next;
}
free=free1;
paixu(use);//将两表按照地址由小到大排序
paixu(free);
}
void distribute(tablep &use,tablep &free)
{//主存分配
float k;
tablep use1,free1;
cout<<endl<<"空闲表如下:";
display(free);
cout<<endl<<"请输入要执行的作业的长度:";
cin>>k;
free1=free->next;
while(free1!=NULL&&(free1->length<k)||free1->flag==0)
free1=free1->next;
if(free1==NULL)//free1移到了表尾
cout<<endl<<"空闲表中没有合适的空闲分区,请等待";
else
{
if(free1->length==k)
free1->flag=0;//此空闲区全部被分出,该项成为一个空表目
else
//若空闲区未全部分出,则只需把长度减去作业长度
free1->length=free1->length-k;
}
use1=use->next;
while(use1->flag==1)
use1=use1->next;
use1->adress=free1->adress;
use1->length=k;
use1->flag=1;//新作业占用空表目的一个项
paixu(use);//已分配表顺序可能改变,所以重新排序
cout<<endl<<"分区成功!"<<endl;
}
void recycle(tablep &use,tablep &free)
{//主存回收
int i,b,mm=0;
tablep use1,free1;
float ad,k;
cout<<endl<<"已分配表如下:";
display(use);
cout<<"要回收第几个作业所占空间?";
cin>>b;
use1=use->next;
for(i=1;i<b;i++)
use1=use1->next;
if(use1==NULL||use1->flag==0)//若指针移到表尾或者指到空表目
cout<<"对不起,您输出入的作业号有错误!";
else
{
ad=use1->adress;
k=use1->length;
use1->flag=0;
free1=free->next;
while(free1!=NULL&&free1->flag&&mm==0)//mm=0表示回收的空间还未被插入空闲表
{
if((free1->adress)+(free1->length)==ad)
{//被回收的空间与其上空闲区相邻
tablep free2;
free2=free1->next;
free1->length=free1->length+k;//将上空闲区的长度增长
while(free2!=NULL&&mm==0)//看被回收的空间是否同时与其下空闲区相邻
{
if(free1->length+free1->adress==free2->adress)
{
free1->length=free1->length+free2->length;
free2->flag=0;
mm=1;
}
free2=free2->next;
}
mm=1;
}
if(ad+k==free1->adress)
{//若被回收的空间与其下空闲区相邻
free1->length=free1->length+k;
free1->adress=ad;
mm=1;
}
free1=free1->next;
}
if(mm==0)
{//没有上下空闲区与它相联,则找一个空表目填入相应信息
free1=free->next;
while(free1->flag==1&&free1!=NULL)
free1=free1->next;
free1->adress=ad;
free1->length=k;
free1->flag=1;
}
paixu(free);//空闲表顺序可能改变,重新排序
}
cout<<endl<<" 回收成功!"<<endl;
}
int main(int argc, char* argv[])
{
tablep use,free;
use=new table;
free=new table;
use->next=NULL;
free->next=NULL;
int b;
char c;
create(use,free);
do
{
system("cls");
cout<<endl<<endl<<endl<<endl;
cout<<" **************************************************"<<endl;
cout<<" * 已分配表和空闲表已经生成,请选择如下几项功能: *"<<endl;
cout<<" * 1.主存分配 *"<<endl;
cout<<" * 2.主存回收 *"<<endl;
cout<<" * 3.显示主存分配情况 *"<<endl;
cout<<" * 0.退出 *"<<endl;
cout<<" **************************************************"<<endl;
cout<<" 请输入数字(0-3):";
cin>>b;
switch(b)
{
case 1:
distribute(use,free);
getchar();
break;
case 2:
recycle(use,free);
getchar();
break;
case 3:
cout<<"已分配表为:"<<endl;
display(use);
cout<<endl<<endl<<"空闲表为:"<<endl;
display(free);
getchar();
break;
case 0:
system("cls");
cout<<endl<<endl<<endl<<endl;
cout<<" **************************************************"<<endl;
cout<<" * ^-^ 谢谢使用 ^-^ * "<<endl;
cout<<" * * "<<endl;
cout<<" * * "<<endl;
cout<<" * * "<<endl;
cout<<" * 信息05-2 伊冰静 050814226 *"<<endl;
cout<<" **************************************************"<<endl;
exit(0);
default:
cout<<"对不起,您输入的代号有错误,请重新进行";
}
}while(b!=0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -