1 #include<stdio.h> 2 #include<conio.h> 3 #include<string.h> 4 #define MAX 24 5 void showmenu(int num); 6 7 struct partition{ 8 9 char pn[10]; 10 int begin; 11 int size; 12 int end; 13 char status; 14 }; 15 typedef struct partition PART; 16 17 PART Total[MAX] ; 18 PART userjob ; 19 20 void init(){ 21 22 strcpy(Total[0].pn,"No.1"); 23 Total[0].begin=0; 24 Total[0].size=100; 25 Total[0].end=100; 26 Total[0].status='u'; 27 } 28 29 30 void input(int num){ 31 32 int i=0; 33 for(i=0;i<num;i++){ 34 //内存名 35 printf("内存名: "); 36 scanf("%s" ,&Total[i+1].pn); 37 //开始内存大小 38 printf("内存大小: "); 39 scanf("%d",&Total[i+1].size); 40 //内存状态 41 printf("内存状态: "); 42 scanf("%s",&Total[i+1].status); 43 //作业开始大小 44 Total[i+1].begin=Total[i].end; 45 // printf("%d ",Total[i+1].begin); 46 //作业结束大小 47 Total[i+1].end=Total[i+1].begin+Total[i+1].size; 48 // printf("%d ",Total[i+1].end); 49 50 } 51 52 } 53 54 55 void output(int num){ 56 int i=0; 57 printf("内存名 开始 大小 结束 状态 "); 58 for(i=0;i<MAX;i++) 59 { 60 if(Total[i].size!=0){ 61 printf("%s %d %d %d %c " ,Total[i].pn,Total[i].begin,Total[i].size,Total[i].end,Total[i].status); 62 } 63 } 64 65 66 } 67 68 void inputjob(){ 69 printf("================================="); 70 printf(" "); 71 printf(" 用户输入作业 "); 72 printf("内存名: "); 73 scanf("%s" ,&userjob.pn); 74 //开始内存大小 75 printf("内存大小: "); 76 scanf("%d",&userjob.size); 77 //作业状态 78 userjob.status='u'; 79 //作业开始大小 80 userjob.begin=0; 81 //作业结束大小 82 userjob.end=0; 83 84 85 } 86 87 void firstadapt(int num){ //首次适应算法 88 int i=0; 89 for(i=0;i<=num;i++) 90 { 91 92 if(Total[i].status=='f') 93 { 94 if(Total[i].size==userjob.size) 95 { 96 strcpy(Total[i].pn,userjob.pn); 97 Total[i].status='u'; 98 printf("%d ",userjob.size); 99 output(num); 100 } 101 else if(Total[i].size>userjob.size){ 102 strcpy(Total[num+1].pn,Total[i].pn); 103 Total[num+1].size=Total[i].size-userjob.size; 104 strcpy(Total[i].pn,userjob.pn); 105 Total[i].end=Total[i].begin+userjob.size; 106 Total[i].size=userjob.size; 107 Total[i].status='u'; 108 Total[num+1].begin=Total[i].end; 109 Total[num+1].end=Total[num+1].begin=+Total[num+1].size; 110 Total[num+1].status='f'; 111 output(num); 112 113 } 114 else 115 break; 116 117 } 118 119 120 } 121 122 123 124 } 125 126 //最佳适应算法 127 128 void bestfit(int num){ 129 int i,j; int flag; int now; 130 j=0; 131 for(i=0;i<=num;i++) 132 { 133 if(Total[i].status=='f'&&Total[i].size>=userjob.size) 134 { now=i; 135 flag=Total[i].size; 136 printf("%d ",flag); 137 for(j=i+1;j<=num;j++) 138 { 139 if(Total[j].status=='f'&&Total[j].size>=userjob.size&&Total[j].size<flag) 140 { 141 142 flag=Total[j].size; 143 now=j; 144 } 145 146 } 147 148 break; 149 } 150 151 } 152 153 strcpy(Total[now].pn,userjob.pn); 154 Total[now].status='u'; 155 printf("%d ",userjob.size); 156 Total[now].end=Total[now].begin+userjob.size; 157 Total[now].size=userjob.size; 158 output(num); 159 160 161 } 162 163 //最坏适应算法 164 void badlyfit(int num){ 165 int i,j; int flag; int now; 166 j=0; 167 for(i=0;i<=num;i++) 168 { 169 if(Total[i].status=='f'&&Total[i].size>=userjob.size) 170 { now=i; 171 flag=Total[i].size; 172 printf("%d ",flag); 173 for(j=i+1;j<=num;j++) 174 { 175 if(Total[j].status=='f'&&Total[j].size>=userjob.size&&Total[j].size>flag) 176 { 177 flag=Total[j].size; 178 now=j; 179 } 180 181 } 182 183 break; 184 } 185 186 } 187 188 strcpy(Total[now].pn,userjob.pn); 189 Total[now].status='u'; 190 printf("%d ",userjob.size); 191 Total[now].end=Total[now].begin+userjob.size; 192 Total[now].size=userjob.size; 193 output(num); 194 195 196 } 197 198 199 200 201 202 void selecttypr(int num){ 203 int select;int end=0; 204 printf("================================="); 205 printf(" "); 206 printf(" 用户选择分配算法 "); 207 printf(" 1.首次适应算法 "); 208 printf(" 2.循环首次适应算法 "); 209 printf(" 3.最佳适应算法 "); 210 printf(" 4.最坏适应算法 "); 211 scanf("%d",&select); 212 switch(select){ 213 case 1 : 214 printf("首次适应算法 "); 215 firstadapt(num); 216 break; 217 case 2 : 218 printf("循环首次适应算法 "); 219 break; 220 case 3 : 221 printf("最佳适应算法 "); 222 bestfit(num); 223 break; 224 case 4 : 225 printf("最坏适应算法 "); 226 badlyfit(num); 227 break; 228 default : 229 printf("输入错误 "); 230 break; 231 232 } 233 234 } 235 236 237 void conback(int id,int num){ 238 int i; 239 if(id>num){ 240 printf(" 输入的id大于总的"); 241 } 242 if(Total[id-1].status=='f'&&Total[id+1].status=='f'&&Total[id-1].end==Total[id].begin&&Total[id].end==Total[id+1].begin){ 243 for(i=id-1;i<=num-id+1;i++){ 244 Total[id-1].size=Total[id+1].end-Total[id-1].begin; 245 Total[id-1].end=Total[id+1].end; 246 Total[id-1].status='f'; 247 248 } 249 250 } 251 252 else if(Total[id-1].status=='f'&&Total[id+1].status=='u'){ 253 254 255 256 } 257 258 } 259 260 261 262 263 264 265 void select(int flag,int num){ 266 int id; 267 switch(flag){ 268 case 1 : 269 inputjob(); 270 selecttypr(num); 271 break; 272 case 2 : 273 printf(" 请输入需要收回内存id "); 274 scanf("%d",&id); 275 // conback(id,num); 276 break; 277 default : 278 printf("输入错误"); 279 break; 280 281 282 } 283 284 } 285 286 287 void showmenu(int num){ 288 int flas; 289 printf("================================="); 290 printf(" "); 291 printf(" 用户选择分配算法 "); 292 printf(" 1.插入内存 "); 293 printf(" 2.回收内存 "); 294 scanf("%d",&flas); 295 select(flas,num); 296 297 } 298 299 300 void main(){ 301 int num; 302 printf("初始化,设内存的总量为512k "); 303 printf("系统从低地址开始分配,占用100k "); 304 init(); 305 306 printf("================================="); 307 printf(" "); 308 printf("模拟内存 "); 309 scanf("%d",&num); 310 input(num); 311 312 printf("================================="); 313 printf(" "); 314 output(num); 315 316 317 318 showmenu(num); 319 320 }