⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mec.c

📁 用VC写的模拟内存分配管理的代码,经测试运行完全正确
💻 C
字号:
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#define NULL 0


typedef struct mc
{
	int num;
	float size;
	float ad;
	int st;
	struct mc  *prior;
	struct mc  *next;
}mc,*linkmc;
linkmc p2;


linkmc Intialize(linkmc head)
{
	linkmc fm;
	fm=(linkmc)malloc(sizeof(mc));
    


	fm->num=1;
	fm->size=100;
	fm->ad=1;
	fm->st=0;

    head->next=fm;
    fm->prior=head;
	fm->next=NULL;
   // fm->next=head;
	return head;
}



linkmc require(linkmc head)
{  
  	linkmc p1;
	
    p2=(linkmc)malloc(sizeof(mc));
    
	printf("\nPlease input the size you want to require:\n");
    scanf("%f",&p2->size);
    
    p2->next=NULL;
	p1=head->next;
	while((p1->size<p2->size)&&(p1->next!=NULL))
	{   
	  
		p1=p1->next;
	}
	 if(p1->size<p2->size) 
	{
		printf("Sorry, the  main memorysize left is not enough!!\n");

	}
	else if(p1->size==p2->size)
	{   p2->ad=p1->ad;
		p1->prior->next=p1->next;
		if(p1->next!=NULL)
		p1->next->prior=p1->prior;
	                                     

	}
	else if(p1->size>p2->size)
	{
	    p2->ad=p1->ad;
		p1->size=p1->size-p2->size;
    	p1->ad=p1->ad+p2->size;
		
	}
	
	return(head);
	
}

/*linkmc require2(linkmc head)
{   linkmc p2;
  	linkmc p1;
	linkmc p3;
	linkmc head2;
    p2=(linkmc)malloc(sizeof(mc));
    head2=(linkmc)malloc(sizeof(mc));
	printf("\nPlease input the size you want to require:\n");
    scanf("%f",&p2->size);
    
    p2->next=NULL;
    p3=head2;
	while(p3->next!=NULL)



	p1=head->next;
	while((p1->size<p2->size)&&(p1->next!=NULL))
	{   
	  
		p1=p1->next;
	}
	 if(p1->size<p2->size) 
	{
		printf("Sorry, the  main memorysize left is not enough!!\n");

	}
	else if(p1->size==p2->size)
	{   p2->ad=p1->ad;
		p1->prior->next=p1->next;
		p1->next->prior=p1->prior;
	    head2->next=p2;

	}
	else if(p1->size>p2->size)
	{
	    p2->ad=p1->ad;
		p1->size=p1->size-p2->size;
    	p1->ad=p1->ad+p2->size;
		head2->next=p2;
	}
	

	return(p2);
}*/



linkmc release(linkmc head1,linkmc head2,float size)
 
{   
    linkmc p0;
	linkmc p1;
	linkmc s;
	
	p0=(linkmc)malloc(sizeof(mc));
	
	printf("Please input the size you want to release! \n");
    scanf("%f",&size);
	
	if(head2->next!=NULL)
	{
	  p0=head2->next;
	}
	while((p0->size!=size)&&(p0->next!=NULL))
	{
		p0=p0->next;
	
	}	
	if(p0->size!=size)
		printf("Sorry,the size is not required,please input a required size!\n");
	
      else 
	  { 
		  p1=head1->next;
	
	

    	while((p1->ad<p0->ad)&&(p1->next!=NULL))
		{
          p1=p1->next;
	
		}

	
		
    s=(linkmc)malloc(sizeof(mc));
	//s->num=p0->num;
	s->size=p0->size;
	s->ad=p0->ad;

	if(p1->prior->ad+p1->prior->size==s->ad)
	{
		p1->prior->size=p1->prior->size+s->size;
		p0->prior->next=p0->next;
	   if(p0->next!=NULL)
		p0->next->prior=p0->prior;

	
	}
	else if(s->ad+s->size==p1->ad)
	{   
        
		
		s->size=s->size+p1->size;
		s->prior=p1->prior;
		p1->prior->next=s;
		s->next=p1->next;
	    p0->prior->next=p0->next;
		if(p0->next!=NULL)
		 p0->next->prior=p0->prior;
 
	}
	else 
	{
	s->prior=p1->prior;
	p1->prior->next=s;
	s->next=p1;
	p1->prior=s;
	
	p0->prior->next=p0->next;
if(p0->next!=NULL)
	p0->next->prior=p0->prior;

	}
	  }
	return head1;
 }


float Print(linkmc head)
{
    linkmc p;
	float size;
	p=head->next;
	size=0.00;
printf("空闲分区:\n");
	while(p!=head && p!=NULL)
	{   
		size=size+p->size;
		printf("\n      块号:    %d ",p->num);
	    printf("        大小:    %f",p->size);
	    printf("        起始地址:%f \n",p->ad);
        
	    p=p->next;
	}

	 printf("\n     The total size of left memory :%f\n",size);
 return(size);
}

void Print2(linkmc head)
{
    linkmc p;
	float size;
	p=head->next;
	size=0.00;
printf("已分配分区:\n");
	while(p!=head && p!=NULL)
	{   
		size=size+p->size;
	//	printf("\n      块号:%d ",p->num);
	    
		printf("      起始地址:%f  ",p->ad);
		printf("          大小:%f\n",p->size);
	    
        
	    p=p->next;
	}

	 printf("\n     The total size of requirted memory :%f\n",size);

}

linkmc sort(linkmc head)
{
	linkmc p0;
	int i=1;
	p0=head;
	p0->num=0;
	while(p0->next!=NULL)
	{
		p0=p0->next;
		p0->num=i;
		
		
		
		i++;
	}
return(head);

}


linkmc sort2(linkmc head)
{ 
	linkmc j;
    j=head->next;
	if(j!=NULL)
	{
    while(j->next!=NULL)
	{
		if(j->size<j->next->size)
		{  if(j->next->next!=NULL)
			j->next->next->prior=j;
			j->prior->next=j->next;
			j->next->prior=j->prior;
            
			j->next->next=j;
			j->prior=j->next;
           
			j->next=j->next->next;
		
		}
	
	}
	}
  return(head);
}

void main()
 { 	 linkmc head,head2,head3;
     linkmc p1;
	 int  f1;
	 char ch[10],cf[10];
	 float lsize,rsize;
     head=(linkmc)malloc(sizeof(mc));
     head2=(linkmc)malloc(sizeof(mc));
	 head3=(linkmc)malloc(sizeof(mc));
	 head2->next=NULL;
	
     printf("\n");
	 printf("          ************************************************************\n");
     printf("\n");
	 //printf("          %c         ",001);
	 printf("          ※         Welcome  to the  memory manager  system !      ※ ");
     //printf(" %c",001);
	 printf("\n");
	 printf("\n");
    
     printf("                      首次适应算法---f          最佳适应算法---b\n");
     printf("\n");
	 printf("                      Require--------q          Release--------l\n");
	 printf("\n");
	 printf("\n");
	 printf("\n");
	 printf("          ************************************************************\n");
	 head=Intialize(head);
	 lsize=Print(head);
     printf("\n");
 
 loop:  
	 do{
     printf("  Please choose the algorithm:\n  首次适应算法---f          最佳适应算法---b\n");
	 scanf("%s",&cf);
     if(strlen(cf)==1)
	 {
		 switch(cf[0])
		 {
			 case'f':
				 f1=0;
				break;
             case'b':
				 f1=1;
				 break;
             default:
				 {
					 f1=2;
                     printf("Uncorrect order,please input the right operation.\n") ;
					 goto loop;
					 
				 }
             break;
		    
		 }
	 
	 }
	  else
	  {
		  printf("Uncorrect order,please input the right order.\n");
          goto loop;
	  }
	 printf("  Please input your operation:\n  Require---q  Release---l\n");
	  
	  //ch=getchar();
	  scanf("%s",&ch);
	 //getchar();
	 if(strlen(ch)==1) 
	 {  
		 switch(ch[0]){
	 //if (strcmp(ch,"q")==0)
	  case 'q':
	 {  p1=head2;
	    if(lsize!=0)
		{ 
          while(p1->next!=NULL)
	   {
		   p1=p1->next;
	   
	   }

	      head=require(head);getchar();
		  if(f1==1)
		  head=sort2(head);
		  head=sort(head);
	      lsize=Print(head);
		  
		  p1->next=p2;
		  p2->prior=p1;
		  Print2(head2);
         // goto loop;
		}
		
	}
	 break;
	 //else if(strcmp(ch,"l")==0)
	  case 'l':
		 {	
			 printf("haha!\n");
			 
			 head=sort(head);
             
			 head2=sort(head2);
			 release(head,head2,rsize);getchar();
             if(f1==1)
		      head=sort2(head);
		 	 head=sort(head);
			 Print(head);
			 head2=sort(head2);
			 Print2(head2);
			// goto loop;
		 }
		 break;
	//else //if((strcmp(ch,"q")!=0)&&(strcmp(ch,"q")!=0)) 
	  default:
	 {
		 printf("Uncorrect order,please input the right order.\n");
		 //goto loop;
	 
	 }
	 break;
	  }
	 
	 }
	 else
		 printf("Uncorrect order,please input the right order.\n");
	 // ch=getchar();
	 }while(1);
		 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -