// yemianDlg.cpp : implementation file
//
#include "stdafx.h"
#include "yemian.h"
#include "yemianDlg.h"
#include <afxtempl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.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()
/////////////////////////////////////////////////////////////////////////////
// CYemianDlg dialog
CYemianDlg::CYemianDlg(CWnd* pParent /*=NULL*/)
: CDialog(CYemianDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CYemianDlg)
m_memory = 0;
m_total = 0;
m_random = _T("");
m_change = 0;
m_absent = 0;
m_changef = 0.0;
m_absentf = 0.0;
m_m = _T("");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CYemianDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CYemianDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_EDIT1, m_memory);
DDX_Text(pDX, IDC_EDIT2, m_total);
DDX_Text(pDX, IDC_EDIT3, m_random);
DDX_Text(pDX, IDC_EDIT4, m_change);
DDX_Text(pDX, IDC_EDIT6, m_absent);
DDX_Text(pDX, IDC_EDIT5, m_changef);
DDX_Text(pDX, IDC_EDIT7, m_absentf);
DDX_Text(pDX, IDC_EDIT8, m_m);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CYemianDlg, CDialog)
//{{AFX_MSG_MAP(CYemianDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnFIFO)
ON_BN_CLICKED(IDC_BUTTON2, OnLRU)
ON_BN_CLICKED(IDC_BUTTON3, OnOPT)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CYemianDlg message handlers
BOOL CYemianDlg::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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE);*/ // Set small icon
// TODO: Add extra initialization here
CheckDlgButton(IDC_RADIO2,BST_CHECKED);//单选按钮初始化默认选中
GetDlgItem(IDC_EDIT8)->EnableWindow(FALSE);//初始化不可编辑
return TRUE; // return TRUE unless you set the focus to a control
}
void CYemianDlg::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 CYemianDlg::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();
}
}
HCURSOR CYemianDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//先进先出页面置换算法
void CYemianDlg::OnFIFO()
{
UpdateData(1);
CArray<int,int> myt;//存放随机生成的所有页面号的数组
CArray<int,int> mym;//存放物理块中现有的页面号
int radio[2];//用于记下单选按钮的选择状态
int m=0;
int as=0;//置换页面计数
bool x; //页面是否需要置换
CString str1="";
CString str2="";
myt.SetSize(m_total,-1);
mym.SetSize(m_memory,-1);
if(m_memory<m_total)
{
m_list.ResetContent();
m_random="";
//srand((unsigned int)1);
radio[0]=IsDlgButtonChecked(IDC_RADIO1);//获取单选按钮状态
radio[1]=IsDlgButtonChecked(IDC_RADIO2);
if(radio[0]==1)//手动输入
{
if(m_m.GetLength()==0)
{
MessageBox("请输入页面序列!");
return;
}//这个判断可要可不要 下面那些大于小于可以将其(空)概括
char *sz=(char *)m_m.GetBuffer(m_m.GetLength());
//str1 = m_m;
char *str;
char seps[]=" ";
str=strtok(sz,seps);
int d=0;
while(str!=NULL)
{
int i=0;int j=1;int k=strlen(str);
for(int a=0;a<k;a++)
{
if(str[k-a-1]<48||str[k-a-1]>57)
{
MessageBox("输入序列中含有非数字字符!");
return;
}
i=i+(str[k-a-1]-48)*j;
j=j*10;
}
str=strtok(NULL,seps);
if(d>=m_total)
{
MessageBox("输入的页面个数大于总页面数!");
return;
}
myt.SetAt(d,i);//内存错误就在这里 原来已经指定了数组的长度是等于总页面数 当这里输入过多的时候,不断分赋值就会越界
if(i==0)
{
MessageBox("页面号不能是零!");
return;
}
if(i>m_total)
{
MessageBox("序列中不能输入大于页面总数的页面号!");
return;
}
str2.Format("%d",i);
if(d==0)
str1=str2;
else
str1=str1+" "+str2;//转化为字符串数组,便于输出
d++;
}
if(d<m_total)
{
MessageBox("输入的页面个数小于总页面数!");
return;
}
}
else if(radio[1]==1)//随机生成
{
for(int i=0;i<m_total;i++)
{
myt.SetAt(i,rand()%m_total+1);//i);
str2.Format("%d",myt.GetAt(i));
str1=str1+" "+str2;//转化为字符串数组,便于输出
}
m_random=str1;
UpdateData(0);
}
if(radio[0]==1)m_m=str1;//str1;//手动输入的时候保留显示
//m_random=str1;
//UpdateData(0);
for(int i=0;i<m_total;i++)
{
str1="";
str2="";
x=true;
for(int k=0;k<m_memory;k++)
{
if(myt.GetAt(i)==mym.GetAt(k))
{
x=false;//判断物理块中是不是有跟当前须替换的页面
break;
}
}
if(x)//||i<m_memory
{
int a;
a=(i-m)%m_memory;
mym.SetAt(a,myt.GetAt(i));
as++;
}
else
{
m++;
}
int b;
for(int j=0;j<m_memory;j++)
{
b=mym.GetAt(j);
str2.Format("%d",b);
str1=str1+" "+str2;
}
m_list.InsertString(-1,str1);
}
m_absent=as;
m_absentf=(double)as/m_total;
if((as-m_memory)<=0)
{
m_change=0;
m_changef=0;
}
else
{
m_change=as-m_memory;
m_changef=(double)(as-m_memory)/m_total;
}
UpdateData(0);
}
else
MessageBox("页面总数小于物理块,不需要进行交换操作!");
}
//最久未使用页面置换算法
void CYemianDlg::OnLRU()
{
UpdateData(1);
CArray<int,int> myt;//存放随机生成的所有页面号的数组
CArray<int,int> mym;//存放物理块中现有的页面号
CArray<int,int> myb;
int radio[2];//用于记下单选按钮的选择状态
int m=0,a;
int as=0; //置换页面计数
bool x;
CString str1=""
MFC实现的操作系统页面置换(FIFO、LRU、OPT)
5星 · 超过95%的资源 需积分: 9 121 浏览量
2011-07-07
19:12:22
上传
评论 4
收藏 123KB RAR 举报
Setting009
- 粉丝: 0
- 资源: 7
- 1
- 2
- 3
前往页