/*固定分区分配的最佳分配算法模拟*/
#include<stdio.h>
#include<graphics.h>
#include<string.h>
#include<stdlib.h>
/*定义分区表结构*/
struct areaTable
{
int start;//起始地址长度以及状态
int length;
int state;
};
/*****************/
/*手动分区表*/
int initAT(struct areaTable* at,int n)
{
int i;/*for loop*/
printf("Please Allocate The Memory By Yourselt:\n");//划分内存固定大小分区
for(i=0;i<n;i++)
{
printf("Input the %d-area length:",i);//输入固定分区每个的长度
scanf("%d",&at[i].length);
if(i==0) at[i].start=40;//定义第一个分区的起始地址为40
else at[i].start=at[i-1].start+at[i-1].length;//表示下一个起始地址的起始状态表示的是分区长度加上起始地址长度
at[i].state=0;//表示状态为未分配
}
return 0;
}
/*****************/
/*根据分区表画图*/
int optiAlloc(struct areaTable* at,int n,int ask)
{
int difference=1000;
int areaNo=-1;
int i;
for(i=0;i<n;i++)
{
if(at[i].state==0&&at[i].length>=ask&&((at[i].length-ask)<difference))
{
difference=at[i].length-ask;
areaNo=i;
}
}
return areaNo;
}
/*****************/
/*根据分区表画图*/
int drawAT(struct areaTable* at,int n)
{
int gdriver,gmode;
int i=0;
int left,right;
char str10[10];
char str40[40];
/*设定图形模式*/
detectgraph(&gdriver,&gmode);
initgraph(&gdriver,&gmode,"c:\\tc");
/*画图*/
left = getmaxx() / 2 - 50;
right = getmaxx() / 2 + 50;
setcolor(2);
setfillstyle(SOLID_FILL,6);
rectangle(left,50,right,50+at[0].start);
floodfill(left+2,50+2,2);
outtextxy(right+5,50,"system area len=50");
setcolor(2);
setfillstyle(SOLID_FILL,4);
for(i=0;i<n;i++)
{
rectangle(left,50+at[i].start,right,50+at[i].start+at[i].length); if(at[i].state==1) floodfill(left+2,50+at[i].start+2,2);
if(at[i].state!=0) floodfill(left+2,50+at[i].start+2,2);
itoa(i,str10,10);
strcpy(str40,str10);
strcat(str40," len=");
itoa(at[i].length,str10,10);
strcat(str40,str10);
strcat(str40,at[i].state?" FULL":" FREE");
outtextxy(right+5,50+at[i].start,str40);
}
/*暂停屏幕并关闭图形模式*/
getch();
clrscr();
closegraph();
return 0;
}
/*****************/
void main()
{
struct areaTable* at;
int ask;
int n;
int allocNo=-1;
char ch=' ';
clrscr();
printf("Input how many area you allocate:");
scanf("%d",&n);
at=(struct areaTable*)malloc(n*sizeof(struct areaTable));
initAT(at,n);
drawAT(at,n);
do{
printf("\nInput the mem length you want:");
scanf("%d",&ask);//表示的是输入分区长度
allocNo = optiAlloc(at,n,ask);
if(allocNo!=-1)
{
printf("Allocate Successfully!\n");
at[allocNo].state=1;
drawAT(at,n);
}
else
{
printf("Allocate Failed");
}
printf("\ndo you want contimue to allocate(Y or N):");
ch=getch();
putch(ch);
}while(ch=='Y'||ch=='y');
free(at);
getch();
}
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
babaerzi17
- 粉丝: 2
- 资源: 11
最新资源
- 抖音电商操盘手实战项目玩法教程:从商品卡到直播间
- 店铺动销经营项目玩法教程:起店实操精准拉新0基础开店运营店铺动销全面升级
- #_ssm_159_mysql_高校在线请假与审批系统_.zip
- #_ssm_166_mysql_个人健康信息管理系统_.zip
- #_ssm_168_mysql_树品种资源数据管理系统_.zip
- #_ssm_103_mysql_团员管理系统_.zip
- #_ssm_107_mysql_医院收费系统_.zip
- 文博高一寒假作业英语及答案.zip
- #_ssm_111_mysql_编程类在线答题系统_.zip
- #_ssm_113_mysql_非遗视域下喀什旅游网_.zip
- Dify 是一个易用的 LLMOps 平台,旨在让更多人可以创建可持续运营的原生 AI 应用
- Video-2024-11-12晚上-项目提交规范+PPT.wmv
- 用HTML代码实现国际象棋
- #_ssm_119_mysql_大美新疆在线论坛交流系统_ 该这个.zip
- #_ssm_124_mysql_期末考试考务管理系统wlw_.zip
- #_ssm_122_mysql_喀什古城旅游网_.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)