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

📄 os2dlg.cpp

📁 Unix 成组链接法 模拟UNIX系统的空闲块成组链接法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	pFr.EnableWindow(1);
	pFREE.EnableWindow(1);
	pOc.EnableWindow(1);
	pOccupycon.EnableWindow(1);
	ShowFree();
	ShowFiles();
}

void COS2Dlg::OnAlloc() 
{
	// TODO: Add your control notification handler code here
	CString fn="",fs="",fn1,fs1,bn1;
	int fst=0,bn=0;
	MyFiles *pMyfiles,*pMyfiles1;
	pEfilename.GetWindowText(fn);
	pEfilesize.GetWindowText(fs);
    fst=atoi(fs);
	if(fn.GetLength()>8)
	{
		MessageBox("文件名太长了!");
		pEfilename.SetWindowText("");
	    pEfilename.SetFocus();
		return;
	}
	if(fn=="")
	{
		MessageBox("文件名不能为空!");
		pEfilename.SetWindowText("");
	    pEfilename.SetFocus();
		return;
	}
	if(fst!=0)
	{
	 if(fst%Blocksize==0)
		bn=fst/Blocksize;
	 else
		bn=fst/Blocksize+1;
	}
	else
	{
	  MessageBox("分配空间不合法!");
	  return;
	}
	if(fst>Remainsize)
	{
	  MessageBox("磁盘所剩的空间不足以分配此文件!");
	  pEfilesize.SetWindowText("");
	  return;
	}
	fn1.Format("%-10s",fn);
	fs1.Format("%-10s",fs+"KB");
	bn1.Format("%-10d",bn);
	pMyfiles = new MyFiles(fn,fst,bn);
	int MA[200];
	for(int i=0;i<200;i++)
	{
	 MA[i]=-1;
	}
	int Maxcount=0;
	int countthis=0;
	for (int nIndex=plistfiles.GetCount()-1;nIndex>=0;nIndex--)
	{
	    pMyfiles1=(MyFiles *)plistfiles.GetItemDataPtr(nIndex);
		if (pMyfiles->filename==pMyfiles1->filename)
		{
		  MessageBox("该文件在磁盘中已经存在,请重新输入!");
		  pEfilename.SetWindowText("");
		  return;
		}
	}
	if(bn>0)
	{
	 for(int count=0;count<bn;count++)
	 {
		 if(Blocks[0][0]==0)
		 {
		   MessageBox("空闲块已经分配完毕!");
		   pEfilename.SetWindowText("");
		   pEfilesize.SetWindowText("");
		   return;
		 }
		 while(Blocks[0][countthis]!=-1)
		 {
		  MA[countthis]=Blocks[0][countthis];
		  countthis=countthis+1;
		 }
	    MA[countthis]=-1;
	   countthis=0;
	   if (MA[0]>1)
	   {
	     if(MA[1]!=0)
		 {
		  Maxcount=MA[0];
	      pMyfiles->block[count+1]=MA[Maxcount];
		  if(count==bn-1)
			  pMyfiles->block[count+2]=-1;
		  MA[Maxcount]=-1;
		  MA[0]=MA[0]-1;
		  while(MA[countthis]!=-1)
		  {
			 Blocks[0][countthis]=MA[countthis];
	         countthis=countthis+1;	 
		  }
		  Blocks[0][countthis]=-1;
		  countthis=0;
		 }
		 else
		 {
		   Maxcount=MA[0]+1;
	       pMyfiles->block[count+1]=MA[Maxcount];
		   if(count==bn-1)
			  pMyfiles->block[count+2]=-1;
		   MA[Maxcount]=-1;
		   MA[0]=MA[0]-1;
		   while(MA[countthis]!=-1)
		   {
			 Blocks[0][countthis]=MA[countthis];
	         countthis=countthis+1;	 
		   }
		   Blocks[0][countthis]=-1;
		   countthis=0;
		 }
	   }
	   else
	   {
		   if(MA[0]==1)
		   {
			   if(MA[1]!=0)
			   {
				   int newbase=Blocks[0][1]; 
				   while(Blocks[newbase][countthis]!=-1)
					{
					   Blocks[0][countthis]=Blocks[newbase][countthis];
					   countthis=countthis+1;
					}
				    Blocks[0][countthis]=-1;
				   countthis=0;
				   pMyfiles->block[count+1]=MA[1];
				   if(count==bn-1)
			          pMyfiles->block[count+2]=-1;
			   }
			   else
			   {   
				   pMyfiles->block[count+1]=MA[2];
				   if(count==bn-1)
			          pMyfiles->block[count+2]=-1;
				   Blocks[0][0]=0;
			       Blocks[0][1]=-1;
				   ShowFree();
				   ShowFiles();
				   MessageBox("空闲块已经分配完毕!");
			   }
		   }
	   }
	 }
	}
	else
	{
	  MessageBox("您输入的大小不足以分配一块!");
	  return;
	}
	nIndex=plistfiles.AddString(fn1+fs1+bn1);
	plistfiles.SetItemDataPtr(nIndex,pMyfiles);
	pEfilename.SetWindowText("");
	pEfilesize.SetWindowText("");
	Remainsize-=bn*Blocksize;
	ShowFree();
	ShowFiles();
}

void COS2Dlg::OnUnAlloc() 
{
	// TODO: Add your control notification handler code here
	MyFiles *pMyfiles;
	int MA[200];
	for(int i=0;i<200;i++)
	{
	 MA[i]=-1;
	}
	int nIndex=plistfiles.GetCurSel();
	if(nIndex==LB_ERR)
	{
	  MessageBox("您没有选择要回收的磁盘空间里的文件!");
	  return;
	}
    pMyfiles=(MyFiles *)plistfiles.GetItemDataPtr(nIndex);
    int count=1;
	int countthis=0;
	int Maxcount=0;
	int getbn=0;
	int fst=pMyfiles->block[0];
	while(pMyfiles->block[count]!=-1)
	{
		if(Blocks[0][0]==0)
		{
		 Blocks[0][1]=0;
		 Blocks[0][2]=-1;
		}
		if(Blocks[0][1]!=0)
		{
		  while(Blocks[0][countthis]!=-1)
		  {
		   MA[countthis]=Blocks[0][countthis];
		   countthis=countthis+1;
		  }
	      MA[countthis]=-1;
	      countthis=0;
          Maxcount=MA[0];
		  getbn=pMyfiles->block[count];
		  if(Maxcount<Groupsize)
          {
			 MA[Maxcount+1]=getbn;
		     MA[Maxcount+2]=-1;
			 MA[0]+=1;
			 while(MA[countthis]!=-1)
			 {
			  Blocks[0][countthis]=MA[countthis];
	          countthis=countthis+1;	 
			 }
		      Blocks[0][countthis]=-1;
		      countthis=0;
		  }
		  else
		  {  
		     int num=0;
			 while(MA[num]!=-1)
			 {
				 Blocks[getbn][num]=MA[num];
				 num=num+1;
			 }
			 Blocks[getbn][num]=-1;
			 Blocks[0][0]=1;
			 Blocks[0][1]=getbn;
			 Blocks[0][2]=-1;
		  }
		  count=count+1;
		}
		else
		{
		  while(Blocks[0][countthis]!=-1)
		  {
		   MA[countthis]=Blocks[0][countthis];
		   countthis=countthis+1;
		  }
	      MA[countthis]=-1;
	      countthis=0;
          Maxcount=MA[0];
		  getbn=pMyfiles->block[count];
		  if(Maxcount<Groupsize-1)
		  {
		     MA[Maxcount+2]=getbn;
             MA[Maxcount+3]=-1;
			 MA[0]+=1;
			 while(MA[countthis]!=-1)
			 {
			   Blocks[0][countthis]=MA[countthis];
	           countthis=countthis+1;	 
			 }
		     Blocks[0][countthis]=-1;
		     countthis=0;
		  }
		  else
		  {
		     int num=0;
			 while(MA[num]!=-1)
			 {
				 Blocks[getbn][num]=MA[num];
				 num=num+1;
			 }
			 Blocks[getbn][num]=-1;
			 Blocks[0][0]=1;
			 Blocks[0][1]=getbn;
			 Blocks[0][2]=-1;
		  }
		  count=count+1;
		}
	}
	delete (MyFiles *)plistfiles.GetItemDataPtr(nIndex);
	plistfiles.DeleteString(nIndex);
	Remainsize+=fst*Blocksize;
	ShowFree();
	ShowFiles();
}

void COS2Dlg::ShowFree()
{
  int groupbase=0;
  int thisgroupbase=0;
  int thisgroupbnum=0;
  int countgroup=0;
  int count=0;
  CString tp="",tp1,tp2="",temp1,temp2="";
  if(Blocks[groupbase][0]==0)
  {
    pFREE.SetWindowText("没有空闲块!");
    return;
  }
 while(Blocks[groupbase][1]!=0)
 {    
	  temp2="";
	  countgroup=countgroup+1;
	  tp1.Format("%d",countgroup);
	  thisgroupbase=groupbase;
	  thisgroupbnum=Blocks[thisgroupbase][0];
	  tp2.Format("%d",thisgroupbnum);
	  groupbase=Blocks[thisgroupbase][1];
	  count=1;
	  while(Blocks[thisgroupbase][count]!=-1)
	  {
		  temp1.Format("%d",Blocks[thisgroupbase][count]);
		  temp2+=temp1+"  ";
		  count++;
	  }
	  tp+="第"+tp1+"组  共"+tp2+"个空闲块 "+temp2+"\r\n";
	  
  }
  if(Blocks[groupbase][1]==0)
  {  
	 countgroup=countgroup+1;
	 tp1.Format("%d",countgroup);
     count=2;
	 temp2="";
	 thisgroupbase=groupbase;
	 thisgroupbnum=Blocks[thisgroupbase][0];
	 tp2.Format("%d",thisgroupbnum);
	  while(Blocks[thisgroupbase][count]!=-1)
	  {
		  temp1.Format("%d",Blocks[thisgroupbase][count]);
		  temp2+=temp1+"  ";
		  count++;
	  }
     tp+="第"+tp1+"组  共"+tp2+"个空闲块 "+temp2;
  }
  pFREE.SetWindowText(tp);
}

void COS2Dlg::ShowFiles()
{
    MyFiles *pMyfiles;
	CString tp="",tp1="",temp="",temp1="";
    for (int nIndex=0;nIndex<=plistfiles.GetCount()-1;nIndex++)
	{   
		int count=1;
		tp1="";
	    pMyfiles=(MyFiles *)plistfiles.GetItemDataPtr(nIndex);
		while(pMyfiles->block[count]!=-1)
		{
			temp.Format("%d",pMyfiles->block[count]);
			tp1+=temp+" ";
			count++;
		}
		temp1.Format("%d",pMyfiles->block[0]);
		tp+="文件名:"+pMyfiles->filename+"  占用"+temp1+"块  块号分别为"+tp1+"\r\n";
	}
	pOccupycon.SetWindowText(tp);
}

void COS2Dlg::OnQuit() 
{
	// TODO: Add your command handler code here
	DestroyWindow();
}

void COS2Dlg::OnHelp() 
{
	// TODO: Add your command handler code here
	CAboutDlg pAbout;
	pAbout.DoModal();
}

⌨️ 快捷键说明

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