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

📄 yemianzhihuandlg.cpp

📁 我做的操作系统课程设计
💻 CPP
字号:
// yemianzhihuanDlg.cpp : implementation file
//

#include "stdafx.h"
#include "yemianzhihuan.h"
#include "yemianzhihuanDlg.h"
#include "afxtempl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CYemianzhihuanDlg dialog

CYemianzhihuanDlg::CYemianzhihuanDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CYemianzhihuanDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CYemianzhihuanDlg)
		// NOTE: the ClassWizard will add member initialization here
    memory = 0;
	total = 0;
	random = _T("");
	change = 0;
	absent = 0;
	changef = 0.0;
	absentf = 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CYemianzhihuanDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CYemianzhihuanDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	DDX_Control(pDX, IDC_LIST1, list);
	DDX_Text(pDX, IDC_EDIT1, memory);
	DDX_Text(pDX, IDC_EDIT2, total);
	DDX_Text(pDX, IDC_EDIT3, random);
	DDX_Text(pDX, IDC_EDIT4, change);
	DDX_Text(pDX, IDC_EDIT6, absent);
	DDX_Text(pDX, IDC_EDIT5, changef);
	DDX_Text(pDX, IDC_EDIT7, absentf);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CYemianzhihuanDlg, CDialog)
	//{{AFX_MSG_MAP(CYemianzhihuanDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnFIFO)
	ON_BN_CLICKED(IDC_BUTTON2, OnLRU)
	ON_EN_CHANGE(IDC_EDIT6, OnChangeEdit6)
	ON_EN_CHANGE(IDC_EDIT4, OnChangeEdit4)
	ON_EN_CHANGE(IDC_EDIT5, OnChangeEdit5)
	ON_EN_CHANGE(IDC_EDIT7, OnChangeEdit7)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CYemianzhihuanDlg message handlers

BOOL CYemianzhihuanDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CYemianzhihuanDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CYemianzhihuanDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CYemianzhihuanDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CYemianzhihuanDlg::OnFIFO() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	CArray<int,int> myt;//存放随机生成的所有页面号的数组
	CArray<int,int> mym;//存放物理块中现有的页面号
	int m=0;
	int b,i,j;
	int	count=0;//置换页面计数
	bool x;  //页面是否需要置换
	CString str1="";
	CString str2="";
	myt.SetSize(total,-1);
	mym.SetSize(memory,-1);
	if(memory<total)
	{ 
		list.ResetContent();
		random="";
		//srand((unsigned int)1);

		for(i=0;i<total;i++)
		{
			myt.SetAt(i,rand()%total+1);
			str2.Format("%d",myt.GetAt(i));
			str1=str1+" "+str2;//MessageBox(str1);
		}
		random=str1;
		UpdateData(0);
		
		for(i=0;i<total;i++)
		{ 
			str1="";
			str2="";
			x=true;
	
			  for(int k=0;k<memory;k++)
			  {
				  if(myt.GetAt(i)==mym.GetAt(k))
				  {
					  x=false;//判断物理块中是不是有跟当前须替换的页面
					  break;
				  }
			  }
		      if(x)//||i<memory
			  {			   	  
				  int a;
				  a=(i-m)%memory;
				  mym.SetAt(a,myt.GetAt(i));
				  count++;
			  }
			  else 
			  {
				  m++;
			  }
			 
			  for(j=0;j<memory;j++) 
			  {
				  b=mym.GetAt(j);
				  str2.Format("%d",b);
				  str1=str1+" "+str2;
			  }
			  list.InsertString(-1,str1);
		}
		absent=count;
		absentf=(double)count/total;
		if((count-memory)<=0)
		{
			change=0;
			changef=0;
		}
		else
		{
			change=count-memory;
			changef=(double)(count-memory)/total;
		}
		UpdateData(0);
	}
	else
		MessageBox("页面总数小于物理块,不需要进行交换操作!");
	
}

void CYemianzhihuanDlg::OnLRU() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	CArray<int,int> myt;
	CArray<int,int> mym;
	CArray<int,int> myb;
	int m=0,a,k;
	int b,j;
	int c=0,d,h;
	int count=0;  //置换页面计数
	bool x;	
	CString str1="";
	CString str2="";
	myt.SetSize(total,-1);
	mym.SetSize(memory,-1);
	myb.SetSize(memory,-1);
	if(memory<total)
	{ 
		list.ResetContent();
		random="";
		//生成随机页面数组队列
		for(int i=0;i<total;i++)
		{
			myt.SetAt(i,rand()%total+1);//i);
			str2.Format("%d",myt.GetAt(i));
			str1=str1+" "+str2;//转化为字符串数组,便于输出	
		}
		random=str1;
		UpdateData(0);		
		for(i=0;i<total;i++)
		{ 
			str1="";
			str2="";
			x=true;
	        for(k=0;k<memory;k++)
			  {
				  if(myt.GetAt(i)==mym.GetAt(k))//判断物理块中的页面是否与当前页面相等
				  {
					  myb.SetAt(k,0);
					  for(int j=0;j<memory;j++)
						{
							if(j!=k)
							myb.SetAt(j,myb.GetAt(j)+1);//使物理块中的每个未使用页面的时间增一
						}
					  x=false;//判断物理块中是不是有跟当前须替换的页面
					  m++;		  
					  break;
				  }
			  }
		      if(x) 
			  {			   	  
					
				if((i-m)<memory)
				{
					a=i-m;   
				}  //当前物理页面未填满时直接装入
				else 
				{
					d=myb.GetAt(0);
					for(h=0;h<memory;h++)
					{
						if(d<myb.GetAt(h))
						{
							d=myb.GetAt(h);
							c=h;
						}
					}
					a=c; //找出物理块中最久未使用的页面号
				}
			    mym.SetAt(a,myt.GetAt(i));//将其替换
				myb.SetAt(a,0);
				for(k=0;k<memory;k++)
				{
					if(k!=a)
						myb.SetAt(k,myb.GetAt(k)+1);//使物理块中的每个未改变页面的时间增一
				}
				myb.SetAt(a,0);
				count++;
			  }
			  for(j=0;j<memory;j++) 
			  {
				  
				  
				  b=mym.GetAt(j);
				  str2.Format("%d",b);
				  str1=str1+" "+str2;
			  }
		    list.InsertString(-1,str1);
		}
		absent=count;
		absentf=(double)count/total;
		change=count-memory;
		changef=(double)(count-memory)/total;
		UpdateData(0);
	}
	else
		MessageBox("页面总数小于物理块,不需要进行交换操作!");
	
}


void CYemianzhihuanDlg::OnChangeEdit4() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CYemianzhihuanDlg::OnChangeEdit5() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}
void CYemianzhihuanDlg::OnChangeEdit6() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}
void CYemianzhihuanDlg::OnChangeEdit7() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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