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

📄 除法模拟器dlg.cpp

📁 基于MFC框架编写的模拟CPU对二进制整数除法的运算过程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 除法模拟器Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "除法模拟器.h"
#include "除法模拟器Dlg.h"
#include"Instruction.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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_nInput1 = _T("");
	m_nInput2 = _T("");
	m_nResult = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Text(pDX, IDC_INPUT_FIRST, m_nInput1);
	DDX_Text(pDX, IDC_INPUT_SECOND, m_nInput2);
	DDX_Text(pDX, IDC_RESULT, m_nResult);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_BDivide, OnBUTTONBDivide)
	ON_BN_CLICKED(IDC_BUTTON_Instruction, OnBUTTONInstruction)
	ON_BN_CLICKED(IDC_BUTTON_YDivide, OnBUTTONYDivide)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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
	m_nInput1="0.00000000";
	m_nInput2="0.00000000";
	m_nResult="\r\n\r\n           欢迎大家使用计算机定点小数除法模拟器         \r\n\r\n\r\n    本程序用于模仿计算机CPU对定点小数的除法计算,小数表示为一位符号位,八位数据位,输入时请输入二进制原码,数据位可以少于八位,但不可长于八位,符号位和小数点是必须的,否则将视为无效的输入。如果输入无效则无法完成计算!\r\n\r\n    由于整数的计算过程与小数的计算是很相似的,故本程序没有对整数的运算进行模拟,其中的算法基本上都是一样的,就不再重复。\r\n\r\n\r\n";
	m_nResult+="    制作人:\r\n\r\n            王萌\r\n\r\n";
	m_nResult+="      指导老师:\r\n\r\n            吴昊\r\n\r\n      ";
	m_nResult+="\r\n\r\n                                       制作日期:2008年5月";
	GetDlgItem(IDC_INPUT_FIRST)->SetWindowText(m_nInput1);
	GetDlgItem(IDC_INPUT_SECOND)->SetWindowText(m_nInput2);
	GetDlgItem(IDC_RESULT)->SetWindowText(m_nResult);
	return TRUE; // return TRUE  unless you set the focus to a control	
}

void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
bool IsValidString(CString A)		
{
	if(A.GetLength()>10||A[1]!='.')
	{
		AfxMessageBox("输入不正确,请您认真阅读程序使用说明!");
		return false;
	}
	else
	{
		if(A[0]!='0'&&A[0]!='1')
		{
			AfxMessageBox("输入不正确,请您认真阅读程序使用说明!");
			return false;
		}
		else 
		{
			for(int i=2;i<A.GetLength();i++)
			{
				if(A[i]!='0'&&A[i]!='1')
				{
					AfxMessageBox("输入不正确,请您认真阅读程序使用说明!");
					return false;
				}
			}
		}
	}
	return true;

}

int standard(CString &A)
{
	if(IsValidString(A))
	{
		for(int i=A.GetLength();i<10;i++)
			A.Insert(i,'0');
		return 0;
	}
	return -1;
}

bool IsZero(CString s2)
{
	for(int i=2;i<10;i++)
	{
		if(s2[i]=='1') return false;
	}
	return true;
}

CString Headd(CString A, CString B)
{
	CString carry="0000000000";
	CString result="0.00000000";
	
	for(int j=A.GetLength()-1;j>=2;j--)
	{
		if(A[j]=='1'&&B[j]=='1'&&carry[j]=='1')
		{
			result.SetAt(j,'1');
			carry.SetAt(j-1,'1');
		}
		else if((A[j]=='1'&&B[j]=='1'&&carry[j]=='0')||
			(A[j]=='1'&&B[j]=='0'&&carry[j]=='1')||
			(A[j]=='0'&&B[j]=='1'&&carry[j]=='1'))
		{
			result.SetAt(j,'0');
			carry.SetAt(j-1,'1');
		}
		else if((A[j]=='1'&&B[j]=='0'&&carry[j]=='0')||
			(A[j]=='0'&&B[j]=='1'&&carry[j]=='0')||
			(A[j]=='0'&&B[j]=='0'&&carry[j]=='1'))
		{
			result.SetAt(j,'1');
			carry.SetAt(j-1,'0');
		}
		else
		{
			result.SetAt(j,'0');
			carry.SetAt(j-1,'0');
		}

	}
	if((A[0]=='1'&&B[0]=='1'&&carry[1]=='1')||
		(A[0]=='1'&&B[0]=='0'&&carry[1]=='0')||
		(A[0]=='0'&&B[0]=='1'&&carry[1]=='0')||
		(A[0]=='0'&&B[0]=='0'&&carry[1]=='1'))
	{
		result.SetAt(0,'1');
	}
	else if((A[0]=='1'&&B[0]=='0'&&carry[1]=='1')||
		(A[0]=='1'&&B[0]=='1'&&carry[1]=='0')||
		(A[0]=='0'&&B[0]=='1'&&carry[1]=='1')||
		(A[0]=='0'&&B[0]=='0'&&carry[1]=='0'))
	{
		result.SetAt(0,'0');
	}

	result.SetAt(1,'.');
	return result;

}
CString BtoY(CString A)
{
	if(IsValidString(A))
	{
		CString result;
		standard(A);
		if(A[0]=='0')
		{
			return A;
		}
		else
		{
			for(int i=2;i<A.GetLength();i++)
			{
				if(A[i]=='0') A.SetAt(i,'1');
				else A.SetAt(i,'0');
			} 
			result=Headd(A,"0.00000001");
			result.SetAt(0,'1');
		}
		return result;
	}
	else return("0.00000000");
}

CString YtoB(CString A)
{
	if(IsValidString(A))
	{
		CString result;
		standard(A);
		if(A[0]=='0')
		{
			return A;
		}
		else
		{
			for(int i=2;i<A.GetLength();i++)
			{
				if(A[i]=='0') A.SetAt(i,'1');
				else A.SetAt(i,'0');
			} 
			result=Headd(A,"0.00000001");
			result.SetAt(0,'1');
		}
		return result;
	}
	else return("0.00000000");
}

int dividexor(int a,int b)
{
	return a*(1-b)+b*(1-a);
}

void divideadd(int a[],int b[],int c[])
{
	int flag=0;//////////进位
	for(int i=8;i>=0;i--)
	{
		if(flag==0)
		{
			c[i]=dividexor(a[i],b[i]);
			if(a[i]+b[i]>1)
				flag=1;
			else
				flag=0;
		}
		else if(flag==1)
		{
			c[i]=dividexor(1,dividexor(a[i],b[i]));
			if(a[i]+b[i]+1>1)
				flag=1;
			else
				flag=0;
		}
	}
}

void divideopposition(int from[],int to[])/////////取相反数
{
	int buf[9]={0,0,0,0,0,0,0,0,1};
	int acc[9];
	if(from[0]==1)
	{
		to[0]=0;
		for(int i=1;i<9;i++)
		{
			to[i]=dividexor(from[i],1);
		}
		divideadd(to,buf,acc);
		for(i=0;i<9;i++)
		{
			to[i]=acc[i];
		}
	}
	else
	{
		to[0]=1;
		for(int i=1;i<9;i++)
		{
			to[i]=dividexor(from[i],1);
		}
		divideadd(to,buf,acc);
		for(i=0;i<9;i++)
		{
			to[i]=acc[i];
		}
	}
}

void divideleft_move(int acc[],int answer[])
{
	for(int i=0;i<8;i++)
		acc[i]=acc[i+1];
	acc[8]=0;
	for(i=0;i<8;i++)
		answer[i]=answer[i+1];

⌨️ 快捷键说明

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