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

📄 addressexchangedlg.cpp

📁 实现虚拟地址向物理地址的转化
💻 CPP
字号:
// Address ExchangeDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Address Exchange.h"
#include "Address ExchangeDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

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

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CAddressExchangeDlg 对话框




CAddressExchangeDlg::CAddressExchangeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAddressExchangeDlg::IDD, pParent)
	, m_nSegment(0)
	, m_nLeaf(0)
	, m_nAdd(0)
	, m_nPage(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAddressExchangeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_Segment, m_nSegment);
	DDX_Text(pDX, IDC_EDIT_Leaf, m_nLeaf);
	DDX_Text(pDX, IDC_EDIT_Add, m_nAdd);
	DDX_Text(pDX, IDC_EDIT_Page, m_nPage);
}

BEGIN_MESSAGE_MAP(CAddressExchangeDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_ExChange, &CAddressExchangeDlg::OnBnClickedButtonExchange)
END_MESSAGE_MAP()


// CAddressExchangeDlg 消息处理程序

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

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	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);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	UpdateData();
	ReadSegment();
	ReadLeaf();
	ReadData();
	ReadFast();
	SegmentLen=m_MapSegment.size()-1;

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

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

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CAddressExchangeDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CAddressExchangeDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CAddressExchangeDlg::ReadSegment(void)
{
	wordfile.open("Segment.txt");
	CString str;
	str="无法打开";
	str+="Segment.txt";
	int number=0, status=0,len=0,add=0,index=0;
 

	if(!wordfile)
	{
		MessageBox(str);
	}

	while(wordfile.get(ch))
	{
		//转换成相应的数字
		if(ch==' ')
		{
			index++;
			switch(index)
			{
			case 1:
				number=atoi(strfile.c_str());
				break;
			case 2:
				status=atoi(strfile.c_str());
				break;
			case 3:
				len=atoi(strfile.c_str());
				break;
				
			}
			strfile="";
		}

		else if(ch=='\n')
		{
			index=0;
			add=atoi(strfile.c_str());
			strfile="";
			m_MapSegment.insert(make_pair(number,make_pair(status,make_pair(len,add))));
		}

        else strfile+=ch;
	}

	wordfile.clear();
	wordfile.close();
}

void CAddressExchangeDlg::ReadLeaf(void)
{
	wordfile.open("Leaf.txt");
	CString str;
	str="无法打开";
	str+="Leaf.txt";
	int add=0, number=0,status=0,mass=0,index=0;
 

	if(!wordfile)
	{
		MessageBox(str);
	}

	while(wordfile.get(ch))
	{
		//转换成相应的数字
		if(ch==' ')
		{
			index++;
			switch(index)
			{
			case 1:
				add=atoi(strfile.c_str());
				break;
			case 2:
				number=atoi(strfile.c_str());
				break;
			case 3:
				status=atoi(strfile.c_str());
				break;
				
			}
			strfile="";
		}

		else if(ch=='\n')
		{
			index=0;
			mass=atoi(strfile.c_str());
			strfile="";
			map<int,map<int,pair<int,int>>>::iterator iter=m_MapLeaf.find(add);
			if(iter==m_MapLeaf.end())
			{
				map<int,pair<int,int>> temp;
				temp.insert(make_pair(number,make_pair(status,mass)));
			    m_MapLeaf.insert(make_pair(add,temp));
			}
			else  iter->second.insert(make_pair(number,make_pair(status,mass)));

		}

        else strfile+=ch;
	}

	wordfile.clear();
	wordfile.close();
}

void CAddressExchangeDlg::ReadFast(void)
{
	wordfile.open("Fast.txt");
	CString str;
	string fast="";
	int segment=0,leaf=0,mass=0,index=0;
	str="无法打开";
	str+="Fast.txt";
	if(!wordfile)
	{
		MessageBox(str);
		return;
	}
	while(wordfile.get(ch))
	{
		if(ch==' ')
		{
			index++;
			switch(index)
			{
			case 1:
				segment=atoi(strfile.c_str());
				break;
			case 2:
				leaf=atoi(strfile.c_str());
				break;
			}
			strfile="";
		}
		else if(ch=='\n')
		{
			mass=atoi(strfile.c_str());
			strfile="";
			index=0;
			m_MapFast.insert(make_pair(make_pair(segment,leaf),mass));
		}
		else strfile+=ch;
	}

	wordfile.clear();
	wordfile.close();
}

void CAddressExchangeDlg::ReadData(void)
{
	wordfile.open("Data.txt");
	CString str;
	str="无法打开";
	str+="Data.txt";
	int add=0;
	string data="";
 

	if(!wordfile)
	{
		MessageBox(str);
	}

	while(wordfile.get(ch))
	{
		//转换成相应的数字
		if(ch==' ')
		{
			add=atoi(strfile.c_str());
			strfile="";
		}

		else if(ch=='\n')
		{
			data=strfile;
			strfile="";
			m_MapData.insert(make_pair(add,data));
		}

        else strfile+=ch;
	}

	wordfile.clear();
	wordfile.close();
}

void CAddressExchangeDlg::ExChange(void)
{
	UpdateData();
	int add=0;
	int mass=0;
	string s;
	//先查快表
	map<pair<int,int>,int>::iterator iter0=m_MapFast.find(make_pair(m_nSegment,m_nLeaf));
	if(iter0!=m_MapFast.end())
	{
		MessageBox(L"该段表在快表中命中");
		mass=iter0->second;
	}
	//查段表
	else
	{
		if(m_nSegment>SegmentLen)
		{
			MessageBox(L"段号不合法,越界中断!");
			return;
		}

		map<int,pair<int,pair<int,int>>>::iterator iter1;
		iter1=m_MapSegment.find(m_nSegment);

		if(iter1->second.first==0)
		{
			MessageBox(L"该段不在内存中,缺段中断!");
			return;
		}

		if(m_nLeaf>iter1->second.second.first)
		{
			MessageBox(L"页号不合法,越界中断");
			return;
		}
		
		add=iter1->second.second.second;
		map<int,map<int,pair<int,int>>>::iterator iter2=m_MapLeaf.find(add);
		if(iter2==m_MapLeaf.end())
		{
			MessageBox(L"该段尚未建立页表!");
			return;
		}

		map<int,pair<int,int>>::iterator iter3=iter2->second.find(m_nLeaf);
		if(iter3==iter2->second.end())
		{
			MessageBox(L"该表项不存在,尚未建立!");
			return;
		}
		if(iter3->second.first==0)
		{
			MessageBox(L"该表项不在内存中,缺页中断");
			return;
		}
	  
		mass=iter3->second.second;
	}


	address=mass*m_nPage*1024+m_nAdd;
	CString str;
	str="物理地址是:";
	CString c1;
	c1.Format(L"%d",address);
	str+=c1;
	str+="\r\n";

	map<int,string>::iterator iter4=m_MapData.find(mass);
	str+="要访问的物理块号是:";
	c1.Format(L"%d",mass);
	str+=c1;
	str+="\r\n";
	
	if(iter4==m_MapData.end())
	{
		c1="数据区不存在";
	}
	else  
	{
		str+="要访问的数据是:";
		s=iter4->second;
		c1=s.data();
	}
	str+=c1;
	SetDlgItemText(IDC_EDIT_Info,str);
}

void CAddressExchangeDlg::OnBnClickedButtonExchange()
{
	// TODO: 在此添加控件通知处理程序代码
	ExChange();
}

⌨️ 快捷键说明

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