3、最佳适应算法#includestdio.hstructkongkuai{intstartaddr;intsize;intflag;}kongxq[6]={{10,20,1},{50,50,1},{150,100,1},{300,60,1},{400,150,1},{700,200,1}};intallocate(intjobsize){inti;intt=0;for(i=0;i6;i++)if(kongxq[i].flag==1&&kongxq[i].sizejobsize){kongxq[i].startaddr+=jobsize;kongxq[i].size-=jobsize;t=1;returnkongxq[i].startaddr-jobsize;}elseif(kongxq[i].flag==1&&kongxq[i].size==jobsize){kongxq[i].flag=0;t=1;returnkongxq[i].startaddr;}if(t==0)returnfalse;return1;}circle(){inti,j;structkongkuaitemp;for(i=0;i6;i++)for(j=0;j6;j++)if(kongxq[j].sizekongxq[j+1].size){temp.startaddr=kongxq[j].startaddr;temp.size=kongxq[j].size;temp.flag=kongxq[j].flag;kongxq[j].startaddr=kongxq[j+1].startaddr;kongxq[j].size=kongxq[j+1].size;kongxq[j].flag=kongxq[j+1].flag;kongxq[j+1].startaddr=temp.startaddr;kongxq[j+1].size=temp.size;kongxq[j+1].flag=temp.flag;}for(i=0;i6;i++)for(j=0;j6;j++)if(kongxq[j].flag==0&&kongxq[j+1].flag==1){temp.startaddr=kongxq[j].startaddr;temp.size=kongxq[j].size;temp.flag=kongxq[j].flag;kongxq[j].startaddr=kongxq[j+1].startaddr;kongxq[j].size=kongxq[j+1].size;kongxq[j].flag=kongxq[j+1].flag;kongxq[j+1].startaddr=temp.startaddr;kongxq[j+1].size=temp.size;kongxq[j+1].flag=temp.flag;}return1;}callback(){ints,len,t1=0,t2=0,t3=0,i,j;printf(请输入回收区的起始地址:\n);scanf(%d,&s);printf(请输入回收区的大小:\n);scanf(%d,&len);for(i=0;i6;i++){if((kongxq[i].startaddr==s+len)&&(kongxq[i].flag==1)){len+=kongxq[i].size;t1=1;for(j=0;j6;j++)if((kongxq[j].startaddr+kongxq[j].size==s)&&(kongxq[j].flag==1)){kongxq[i].flag=0;//kongxq[j].startaddr=kongxq[j].size=kongxq[j].size+len;t2=1;break;}if(t2==0){kongxq[i].startaddr=s;kongxq[i].size=len;break;}}}if(t1==0){for(i=0;i6;i++){if((kongxq[i].startaddr+kongxq[i].size==s)&&(kongxq[i].flag==1)){kongxq[i].size+=len;t3=1;break;}if(t3==0)for(j=0;j6;j++)if(kongxq[j].flag==0){kongxq[j].startaddr=s;kongxq[j].size=len;kongxq[j].flag=1;break;}}}return1;}voidprint(){inti;printf(\n起始地址|大小|标记\n\n);for(i=0;i6;i++){printf(%3d|%3d|%3d\n,kongxq[i].startaddr,kongxq[i].size,kongxq[i].flag);}printf(\n);}voidmain(){intjobsize,start;charend;printf(\n是否有作业请求空闲区?yorn:);while((end=getchar())=='y'){printf(初始空闲区状态:\n);circle();print();printf(请输入请求空闲区的作业大小:);scanf(%d,&jobsize);start=allocate(jobsize);circle();printf(分配后空闲区状态:\n);print();if(!start)printf(没有适合的空闲区大小!\n);elseprintf(作业起始地址:%d\n,start);printf(作业大小:%d\n,jobsize);callback();circle();print();printf(是否有其他的作业请求?yorn:);end=getchar();}}