#include<stdio.h> #include<conio.h> #include<string.h> #define MAX 12 struct partition{ char pn[10]; int begin; int size; int end; char status; }; typedef struct partition PART; PART Total[MAX] ; PART userjob ; void init(){ strcpy(Total[0].pn,"No.1"); Total[0].begin=0; Total[0].size=100; Total[0].end=100; Total[0].status='u'; } void input(int num){ int i=0; for(i=0;i<num;i++){ //作业名 printf("作业名:"); scanf("%s" ,&Total[i+1].pn); //开始内存大小 printf("内存大小:"); scanf("%d",&Total[i+1].size); //作业状态 Total[i+1].status = 'f'; //作业开始大小 Total[i+1].begin=Total[i].end; //作业结束大小 Total[i+1].end=Total[i+1].begin+Total[i+1].size; } } void output(int num){ int i=0; printf("内存名 开始 大小 结束 状态 "); for(i=0;i<MAX;i++) { if(Total[i].size!=0){ printf("%s %d %d %d %c " ,Total[i].pn,Total[i].begin,Total[i].size,Total[i].end,Total[i].status); } } } void inputjob(){ printf("作业名:"); scanf("%s" ,&userjob.pn); //开始内存大小 printf("内存大小:"); scanf("%d",&userjob.size); //作业状态 userjob.status='u'; //作业开始大小 userjob.begin=0; //作业结束大小 userjob.end=0; } //首次适应算法 int firstadapt(int num){ int i=0; for(i=0;i<=num;i++){ if(Total[i].status=='f'){ if(Total[i].size==userjob.size){ strcpy(Total[i].pn,userjob.pn); Total[i].status='u'; printf("%d ",userjob.size); output(num); return i; } else if(Total[i].size>userjob.size){ strcpy(Total[num+1].pn,Total[i].pn); Total[num+1].size=Total[i].size-userjob.size; strcpy(Total[i].pn,userjob.pn); Total[i].end=Total[i].begin+userjob.size; Total[i].size=userjob.size; Total[i].status='u'; Total[num+1].begin=Total[i].end; Total[num+1].end=Total[num+1].begin=+Total[num+1].size; Total[num+1].status='f'; output(num); return i; } else break; } } return 0; } //循环首次适应算法 void infofirstadapt(int num){ int i,j=0; int now = firstadapt(num); int flag = Total[now].size; for(i=0;i<=num;i++){ if(Total[i].status=='f'&&Total[i].size>=userjob.size){ printf("%d ",flag); for(j=i+1;j<=num;j++){ if(Total[j].status=='f'&&Total[j].size>=userjob.size&&Total[j].size>flag){ flag=Total[j].size; now=j; } } break; } } strcpy(Total[now].pn,userjob.pn); Total[now].status='u'; printf("%d ",userjob.size); Total[now].end=Total[now].begin+userjob.size; Total[now].size=userjob.size; output(num); } //最坏适应算法 void badlyfit(int num){ int i,j=0; int flag; int now; for(i=0;i<=num;i++){ if(Total[i].status=='f'&&Total[i].size>=userjob.size){ now=i; flag=Total[i].size; printf("%d ",flag); for(j=i+1;j<=num;j++){ if(Total[j].status=='f'&&Total[j].size>=userjob.size&&Total[j].size>flag){ flag=Total[j].size; now=j; } } break; } } strcpy(Total[now].pn,userjob.pn); Total[now].status='u'; printf("%d ",userjob.size); Total[now].end=Total[now].begin+userjob.size; Total[now].size=userjob.size; output(num); } void selecttypr(int select,int num){ switch(select){ case 1 : printf("首次适应算法 "); firstadapt(num); break; case 2 : printf("循环首次适应算法 "); infofirstadapt(num); break; case 3 : printf("最佳适应算法 "); break; case 4 : printf("最坏适应算法 "); badlyfit(num); break; default : printf("输入错误"); break; } } //回收内存 void test(){ } void main(){ int num; int select; printf("初始化,设内存的总量为512k "); printf("系统从低地址开始分配,占用100k "); init(); // printf("*********************************"); // printf("1、插入内存 2、回收内存"); printf("*********************************"); printf(" "); printf("输入内存空闲块:"); scanf("%d",&num); input(num); printf("*********************************"); printf(" "); output(num); printf("*********************************"); printf(" "); printf("用户输入作业: "); inputjob(); while(1){ printf("*********************************"); printf(" "); printf("用户选择分配算法 "); printf(" 1.首次适应算法 "); printf(" 2.循环首次适应算法 "); printf(" 3.最佳适应算法 "); printf(" 4.最坏适应算法 "); scanf("%d",&select); selecttypr(select,num); } }