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

📄 allocationview.cpp

📁 操作系统内存管理模拟
💻 CPP
字号:
// AllocationView.cpp : implementation of the CAllocationView class
//

#include "stdafx.h"
#include "Allocation.h"

#include "AllocationDoc.h"
#include "AllocationView.h"
#include "MainFrm.h"

#include "ChoiceDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAllocationView

IMPLEMENT_DYNCREATE(CAllocationView, CView)

BEGIN_MESSAGE_MAP(CAllocationView, CView)
//{{AFX_MSG_MAP(CAllocationView)
ON_COMMAND(ID_MENUCHOOSE, OnMenuchoose)
ON_COMMAND(ID_TESTSOURCE, OnTestsource)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAllocationView construction/destruction

CAllocationView::CAllocationView()//初始化
{
	// TODO: add construction code here
	m_choose = -1;
	m_size = 0;
	
	joblist[0] = 1;
	joblist[1] = 2;
	joblist[2] = 3;
	joblist[3] = 2;
	joblist[4] = 4;
	joblist[5] = 3;
	joblist[6] = 1;
	joblist[7] = 5;
	joblist[8] = 6;
	joblist[9] = 7;
	joblist[10] = 6;
	
	jobtype[0] = 0;
	jobtype[1] = 0;
	jobtype[2] = 0;
	jobtype[3] = 1;
	jobtype[4] = 0;
	jobtype[5] = 1;
	jobtype[6] = 1;
	jobtype[7] = 0;
	jobtype[8] = 0;
	jobtype[9] = 0;
	jobtype[10] = 1;
	
	jobsize[0] = 130;
	jobsize[1] = 60;
	jobsize[2] = 100;
	jobsize[3] = 60;
	jobsize[4] = 200;
	jobsize[5] = 100;
	jobsize[6] = 130;
	jobsize[7] = 140;
	jobsize[8] = 60;
	jobsize[9] = 50;
	jobsize[10] = 60;

}

CAllocationView::~CAllocationView()
{
}

BOOL CAllocationView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAllocationView drawing

void CAllocationView::OnDraw(CDC* pDC)//重画函数
{
	CAllocationDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	pDC->SetMapMode(MM_LOENGLISH);
	
	for(int i=0;i<8;i++) {
		for(int j=0;j<8;j++) {
			COLORREF color = pDoc->GetSquare(i,j);
			CBrush brush(color);
			int x1 = (j*50) + 50;
			int y1 = (i*-50) - 50;
			int x2 = x1 + 50;
			int y2 = y1 - 50;
			CRect rect(x1,y1,x2,y2);
			pDC->FillRect(rect,&brush);
		}
	}
	
	for(int x=50;x<=450;x+=50) {
		pDC->MoveTo(x,-50);
		pDC->LineTo(x,-450);
	}
	
	for(int y=-50;y>=-450;y-=50) {
		pDC->MoveTo(50,y);
		pDC->LineTo(450,y);
	}
	
}

/////////////////////////////////////////////////////////////////////////////
// CAllocationView diagnostics

#ifdef _DEBUG
void CAllocationView::AssertValid() const
{
	CView::AssertValid();
}

void CAllocationView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CAllocationDoc* CAllocationView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAllocationDoc)));
	return (CAllocationDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAllocationView message handlers

void CAllocationView::OnMenuchoose() //手动分配或释放内存空间
{
	// TODO: Add your command handler code here
	CAllocationDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CChoiceDialog dlg;
	
	if(dlg.DoModal() == IDOK) {
		m_choose = dlg.m_choose;
		m_size = dlg.m_size;
		m_jobnumber = dlg.m_jobnumber;
		CAllocationDoc* pDoc = GetDocument();
		COLORREF clrCurrentColor = pDoc->GetCurrentColor();
		if(m_choose == 0)
		{
			pDoc->Allocation(pDoc->Node(),m_size,m_jobnumber);
		}
		if(m_choose == 1)
		{
			pDoc->Free(m_jobnumber);
		}
		
		for(int k = 0;k<8;k++)
		{
			for(int m = 0;m<8;m++)
			{
				pDoc->Setsquare(k,m,RGB(255,255,255));
			}
		}
		
		Space p = pDoc->Startnode();
		int excursion;
		int num;
		int line;
		int column;
		COLORREF color;
		for(int i=0;i<pDoc->getJobcount();i++)
		{
			excursion = pDoc->getJobarray(i) - p;
			num = pDoc->getJobsize(i);
			for(int j = excursion;j<excursion+num;j++)
			{
				line = j/80;
				column = (j%80)/10;
				color = setColor(pDoc->getJoblist(i));
				pDoc->Setsquare(line,column,color);
			}
		}

		/*
		for(int i = 0;i<8;i++)
		{
		for(int j = 0;j<8;j++)
		{
		pDoc->Setsquare(i,j,RGB(255,0,0));
		}
		}
		
		  int num;
		  int excursion;
		  int line;
		  int column;
		  
			
			  Space p = pDoc->Node();
			  Space q = pDoc->Startnode();
			  int count = 0;
			  do
			  {
			  excursion = p - q;
			  num = p->size;
			  for(int i = excursion;i<excursion+num;i++)
			  {
			  line = (i%640)/80;
			  column = (i%80)/10;
			  pDoc->Setsquare(line,column,RGB(255,255,255));
			  }
			  p=p->nextlink;
			  }
			  while(p&&(p-pDoc->Node()!=0));
			  
		*/
		
		Invalidate();
	}
}

COLORREF CAllocationView::setColor(int jobnumber)//设置颜色
{
	int num;
	COLORREF color;
	num = jobnumber%10;
	switch(num)
	{
	case 1:
		color = RGB(255,0,0);
		break;
	case 2:
		color = RGB(255,255,0);
		break;
	case 3:
		color = RGB(0,255,0);
		break;
	case 4:
		color = RGB(0,0,255);
		break;
	case 5:
		color = RGB(255,128,0);
		break;
	case 6:
		color = RGB(64,128,128);
		break;
	case 7:
		color = RGB(255,0,128);
		break;
	case 8:
		color = RGB(128,128,128);
		break;
	case 9:
		color = RGB(128,128,0);
		break;
	case 0:
		color = RGB(0,255,255);
		break;
	default:
		break;
	}
	return color;
}

void CAllocationView::OnTestsource() //测试试验数据
{
	// TODO: Add your command handler code here
	CAllocationDoc* pDoc;
	COLORREF clrCurrentColor;
	CString msg = "";

	for(int a = 0;a<11;a++)
	{
		pDoc = GetDocument();
		clrCurrentColor = pDoc->GetCurrentColor();
		if(jobtype[a] == 0)
		{
			pDoc->Allocation(pDoc->Node(),jobsize[a],joblist[a]);
		}
		if(jobtype[a] == 1)
		{
			pDoc->Free(joblist[a]);
		}
		
		for(int k = 0;k<8;k++)
		{
			for(int m = 0;m<8;m++)
			{
				pDoc->Setsquare(k,m,RGB(255,255,255));
			}
		}
		
		Space p = pDoc->Startnode();
		int excursion;
		int num;
		int line;
		int column;
		COLORREF color;
		for(int i=0;i<pDoc->getJobcount();i++)
		{
			excursion = pDoc->getJobarray(i) - p;
			num = pDoc->getJobsize(i);
			for(int j = excursion;j<excursion+num;j++)
			{
				line = j/80;
				column = (j%80)/10;
				color = setColor(pDoc->getJoblist(i));
				pDoc->Setsquare(line,column,color);
			}
		}

		if(jobtype[a] == 0)
		{
			msg.Format("%s%d%s%d","作业",joblist[a],"申请",jobsize[a]); 
		}
		else
		{
			msg.Format("%s%d%s%d","作业",joblist[a],"释放",jobsize[a]); 
		}

		::AfxMessageBox(msg);

		Invalidate();

	}

}


⌨️ 快捷键说明

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