LRU最近最少使用算法

源代码在线查看: lru最近最少使用算法view.cpp

软件大小: 2193 K
上传用户: weizik
关键词: LRU 算法
下载地址: 免注册下载 普通下载 VIP

相关代码

				// LRU最近最少使用算法View.cpp : implementation of the CLRUView class
				//
				
				#include "stdafx.h"
				#include "LRU最近最少使用算法.h"
				
				#include "LRU最近最少使用算法Doc.h"
				#include "LRU最近最少使用算法View.h"
				
				void insertpage();
				void Main();
				int SORT(int pagenum);
				char CACHE[5];
				CString putin;
				struct sortpage
				{char page;
				struct sortpage *next;
				};
				
				struct sortpage *head;
				int x=0,y=0;
				CBrush color[10];
				#ifdef _DEBUG
				#define new DEBUG_NEW
				#undef THIS_FILE
				static char THIS_FILE[] = __FILE__;
				#endif
				
				/////////////////////////////////////////////////////////////////////////////
				// CLRUView
				
				IMPLEMENT_DYNCREATE(CLRUView, CView)
				
				BEGIN_MESSAGE_MAP(CLRUView, CView)
					//{{AFX_MSG_MAP(CLRUView)
						// NOTE - the ClassWizard will add and remove mapping macros here.
						//    DO NOT EDIT what you see in these blocks of generated code!
					//}}AFX_MSG_MAP
					// Standard printing commands
					ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
					ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
					ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
				END_MESSAGE_MAP()
				
				/////////////////////////////////////////////////////////////////////////////
				// CLRUView construction/destruction
				
				CLRUView::CLRUView()
				{
					// TODO: add construction code here
				
				}
				
				CLRUView::~CLRUView()
				{
				}
				
				BOOL CLRUView::PreCreateWindow(CREATESTRUCT& cs)
				{
					// TODO: Modify the Window class or styles here by modifying
					//  the CREATESTRUCT cs
				
					return CView::PreCreateWindow(cs);
				}
				
				/////////////////////////////////////////////////////////////////////////////
				// CLRUView drawing
				
				void CLRUView::OnDraw(CDC* pDC)
				{
				int i=0,size,j,flag,state,nextline=0;
				struct sortpage *p1;
				 int pagesize,count,ret;
					struct sortpage *p,*q;
					char one;
					count=0;
				    pDC->SetBkMode(1);	
					pDC->SetTextColor(0x0000);
					pDC->SelectStockObject(DEFAULT_GUI_FONT);
					pDC->SetTextAlign(TA_LEFT);
				    flag=pagesize=1;
				  putin=putin;  
				    q=(struct sortpage *)malloc(sizeof(struct sortpage));
				    q->next=NULL;
					head=q;
					p=q;
				    if(putin=="")
					   return;
				
					 int lenght=putin.GetLength();
					one=putin[0];
				     	
					pDC->TextOut(350,30,"LRU算法演示");
					pDC->TextOut(0,65,"页地址流:");
				    pDC->TextOut(70,65,putin);
					pDC->TextOut(0,95,"页面由新到旧的次序:");
				   for(i=0;i				   {
				   pDC->SelectObject(&color[i]);
				   pDC->Rectangle(CRect(150+i*30,95,180+i*30,115));
				   }
				   i=-1;
					while(lenght>0)
					 {
					    
					  i++;one=putin[i];
					  q=(struct sortpage *)malloc(sizeof(struct sortpage));
					   q->next=NULL;
					   q->page=one;
					   p->next=q;
					   p=q;
					   count++;
					   
					   lenght--;
					   
				
					  }
					 
				
				head->page=char(count);
				
				
				for(i=0;i				 CACHE[i]=' ';
				
				 p1=head->next;
				 size=head->page;
				state=0;
				 if(size>0)
				  for(i=0;i				  {   
				      flag=0;
				     for(j=0;j						 if(CACHE[j]==p1->page)
						 {flag=1;SORT(p1->page);break;}
						      if(flag==0)
							  {
				              CACHE[state]=p1->page;
							  state++;
							  }
					  if(p1->next==NULL)
					   break;
				     p1=p1->next;
				  }
				
				  if(size>5)
				  {  
					 for(j=0,i=0;i					  {pDC->SelectObject(&color[i]);
						pDC->Rectangle(CRect(50+30*(j),50+13*(i+10),75+30*(j),65+13*(i+10)));
						pDC->TextOut(57+30*(j),50+13*(i+10),CString(CACHE[i]));
				       }
				      x=j+2;
					  y=10;
				  while(p1)
				  {   nextline++;
				      if(nextline>=11)
					  {  y=y+9;nextline=0;x=0;}
					  ret=SORT(p1->page);
					  p1=p1->next;
					  i=0;
					  pDC->SelectObject(&color[i]);
				      pDC->TextOut(57+30*(x),50+13*(i+y-1),CString(CACHE[0]));
					  for(i=0;i					   {pDC->SelectObject(&color[i]);
						pDC->Rectangle(CRect(50+30*(x),50+13*(i+y),75+30*(x),65+13*(i+y)));
						pDC->TextOut(57+30*(x),50+13*(i+y),CString(CACHE[i]));
						
				       }
					  if(ret==1)
					  {   
					   pDC->TextOut(57+30*(x),50+13*(i+y+1),"命中");
					  }
					  else
					  {
					   pDC->TextOut(57+30*(x),50+13*(i+y+1),"替换");
					  }
					  x=x+2;
					  
				  }
				
				  }
				  else if(size!=0)
				  {
					
				   for(i=0;i					   { pDC->SelectObject(&color[i]);
						pDC->Rectangle(CRect(50+30*(j),50+13*(i+10),75+30*(j),65+13*(i+10)));
						pDC->TextOut(57+30*(j),50+13*(i+10),CString(CACHE[i]));
				       }
				  }
				
					CLRUDoc* pDoc = GetDocument();
					ASSERT_VALID(pDoc);
					}
				
				
				
				
				
				int SORT(int pagenum)
				{    char change;
					
					 if( pagenum==CACHE[1])
					 {
						      change=CACHE[0];CACHE[0]=CACHE[1];CACHE[1]=change;return 1;
					 }
					 else if( pagenum==CACHE[2])
					 {
						      change=CACHE[2];CACHE[2]=CACHE[1];CACHE[1]=CACHE[0];CACHE[0]=change;return 1;
					 }
					 else if(pagenum==CACHE[3])
					 {
						      change=CACHE[3];CACHE[3]=CACHE[2];CACHE[2]=CACHE[1];CACHE[1]=CACHE[0];CACHE[0]=change;return 1;
					 }
					 else if(pagenum==CACHE[4])
					 {
						      change=CACHE[4];CACHE[4]=CACHE[3];CACHE[3]=CACHE[2];CACHE[2]=CACHE[1];CACHE[1]=CACHE[0];CACHE[0]=change;return 1;
					 }
					 else if(pagenum!=CACHE[0]&&pagenum!=CACHE[1]&&pagenum!=CACHE[2]&&pagenum!=CACHE[3]&&pagenum!=CACHE[4])
					 {
						     CACHE[4]=CACHE[3];CACHE[3]=CACHE[2];CACHE[2]=CACHE[1];CACHE[1]=CACHE[0];CACHE[0]=pagenum;return 2;
					}
					 else
						 return 1;
				
				}
				
				
				
				/////////////////////////////////////////////////////////////////////////////
				// CLRUView printing
				
				BOOL CLRUView::OnPreparePrinting(CPrintInfo* pInfo)
				{
					// default preparation
					return DoPreparePrinting(pInfo);
				}
				
				void CLRUView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
				{
					// TODO: add extra initialization before printing
				}
				
				void CLRUView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
				{
					// TODO: add cleanup after printing
				}
				
				/////////////////////////////////////////////////////////////////////////////
				// CLRUView diagnostics
				
				#ifdef _DEBUG
				void CLRUView::AssertValid() const
				{
					CView::AssertValid();
				}
				
				void CLRUView::Dump(CDumpContext& dc) const
				{
					CView::Dump(dc);
				}
				
				CLRUDoc* CLRUView::GetDocument() // non-debug version is inline
				{
					ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLRUDoc)));
					return (CLRUDoc*)m_pDocument;
				}
				#endif //_DEBUG
				
				/////////////////////////////////////////////////////////////////////////////
				// CLRUView message handlers
				
				void CLRUView::OnInitialUpdate() 
				{
				
				 
					color[0].CreateSolidBrush(RGB(2,255,100));//白色
					color[1].CreateSolidBrush(RGB(2,250,150));//调入
					color[2].CreateSolidBrush(RGB(2,200,200));//替换
					color[3].CreateSolidBrush(RGB(2,150,250));//命中
					color[4].CreateSolidBrush(RGB(2,100,255));
					color[5].CreateSolidBrush(RGB(0,255,255));
					color[6].CreateSolidBrush(RGB(255,0,0));
					color[7].CreateSolidBrush(RGB(255,0,255));
					color[8].CreateSolidBrush(RGB(255,255,0));
					color[9].CreateSolidBrush(RGB(100,100,100));
				    
					
				
					CView::OnInitialUpdate();
					
					// TODO: Add your specialized code here and/or call the base class
					
				}
							

相关资源