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

📄 binaryoperationview.cpp

📁 二进制计算器,位运算,计算器--用户界面设计,汇编语言学习工具
💻 CPP
字号:
// BinaryOperationView.cpp : implementation of the CBinaryOperationView class
//

#include "stdafx.h"
#include "BinaryOperation.h"

#include "BinaryOperationDoc.h"
#include "BinaryOperationView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBinaryOperationView

IMPLEMENT_DYNCREATE(CBinaryOperationView, CFormView)

BEGIN_MESSAGE_MAP(CBinaryOperationView, CFormView)
	//{{AFX_MSG_MAP(CBinaryOperationView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	ON_EN_CHANGE(IDC_EDIT_X0, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_X1, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_X2, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_X3, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_X4, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_X5, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_X6, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_X7, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_X, OnChangeEditX)
	ON_EN_CHANGE(IDC_EDIT_Y0, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_Y1, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_Y2, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_Y3, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_Y4, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_Y5, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_Y6, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_Y7, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_Y, OnChangeEditY)
	ON_EN_CHANGE(IDC_EDIT_X_HEX, OnChangeEditHexX)
	ON_EN_CHANGE(IDC_EDIT_Y_HEX, OnChangeEditHexY)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


static const CString hexStr[16]=
{
	"0000", /* 0 */
	"0001",
	"0010",
	"0011", /* 3 */
	"0100", /* 4 */
	"0101",
	"0110",
	"0111", /* 7 */
	"1000", /* 8 */
	"1001",
	"1010",
	"1011", /* B */
	"1100", /* C */
	"1101",
	"1110",
	"1111"  /* F */
};

static const int Sections[8]=
{
	0x0000000F,
	0x000000F0,
	0x00000F00,
	0x0000F000,
	0x000F0000,
	0x00F00000,
	0x0F000000,
	0xF0000000
};
/////////////////////////////////////////////////////////////////////////////
// CBinaryOperationView construction/destruction

CBinaryOperationView::CBinaryOperationView()
	: CFormView(CBinaryOperationView::IDD)
{
	//{{AFX_DATA_INIT(CBinaryOperationView)
	m_X = 0x12345678;
	m_Y = 0x01ABCDEF;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CBinaryOperationView::~CBinaryOperationView()
{
}

void CBinaryOperationView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBinaryOperationView)
	DDX_Text(pDX, IDC_EDIT_X_HEX, m_strXHex);
	DDV_MaxChars(pDX, m_strXHex, 8);
	DDX_Text(pDX, IDC_EDIT_Y_HEX, m_strYHex);
	DDV_MaxChars(pDX, m_strYHex, 8);
	DDX_Text(pDX, IDC_EDIT_X, m_X);
	DDX_Text(pDX, IDC_EDIT_Y, m_Y);
	DDX_Text(pDX, IDC_EDIT_X0, m_strX[0]);
	DDX_Text(pDX, IDC_EDIT_X1, m_strX[1]);
	DDX_Text(pDX, IDC_EDIT_X2, m_strX[2]);
	DDX_Text(pDX, IDC_EDIT_X3, m_strX[3]);
	DDX_Text(pDX, IDC_EDIT_X4, m_strX[4]);
	DDX_Text(pDX, IDC_EDIT_X5, m_strX[5]);
	DDX_Text(pDX, IDC_EDIT_X6, m_strX[6]);
	DDX_Text(pDX, IDC_EDIT_X7, m_strX[7]);
	DDX_Text(pDX, IDC_EDIT_Y0, m_strY[0]);
	DDX_Text(pDX, IDC_EDIT_Y1, m_strY[1]);
	DDX_Text(pDX, IDC_EDIT_Y2, m_strY[2]);
	DDX_Text(pDX, IDC_EDIT_Y3, m_strY[3]);
	DDX_Text(pDX, IDC_EDIT_Y4, m_strY[4]);
	DDX_Text(pDX, IDC_EDIT_Y5, m_strY[5]);
	DDX_Text(pDX, IDC_EDIT_Y6, m_strY[6]);
	DDX_Text(pDX, IDC_EDIT_Y7, m_strY[7]);
	DDV_MaxChars(pDX, m_strX[0], 4);
	DDV_MaxChars(pDX, m_strX[1], 4);
	DDV_MaxChars(pDX, m_strX[2], 4);
	DDV_MaxChars(pDX, m_strX[3], 4);
	DDV_MaxChars(pDX, m_strX[4], 4);
	DDV_MaxChars(pDX, m_strX[5], 4);
	DDV_MaxChars(pDX, m_strX[6], 4);
	DDV_MaxChars(pDX, m_strX[7], 4);
	DDV_MaxChars(pDX, m_strY[0], 4);
	DDV_MaxChars(pDX, m_strY[1], 4);
	DDV_MaxChars(pDX, m_strY[2], 4);
	DDV_MaxChars(pDX, m_strY[3], 4);
	DDV_MaxChars(pDX, m_strY[4], 4);
	DDV_MaxChars(pDX, m_strY[5], 4);
	DDV_MaxChars(pDX, m_strY[6], 4);
	DDV_MaxChars(pDX, m_strY[7], 4);
	//}}AFX_DATA_MAP
}

BOOL CBinaryOperationView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

int CBinaryOperationView::StrToInt(CString aStr)
{
	int i;
	for(i=0; i<16; i++)
		if ( aStr == hexStr[i]) 
			return i;

	return -1;

}


void CBinaryOperationView::UpdateResults()
{
	int notX= ~m_X;
	int XandY= m_X & m_Y;
	int XorY = m_X | m_Y;
	int XxorY = m_X ^ m_Y;

	SetDlgItemInt(IDC_EDIT_NOT_X_DEC, notX);
	SetDlgItemInt(IDC_EDIT_X_AND_Y_DEC, XandY);
	SetDlgItemInt(IDC_EDIT_X_OR_Y_DEC, XorY);
	SetDlgItemInt(IDC_EDIT_X_XOR_Y_DEC, XxorY);

	CString cst;
	cst=ToBinary(notX);
	SetDlgItemText(IDC_EDIT_NOT_X, cst);

	cst=ToBinary(XandY);
	SetDlgItemText(IDC_EDIT_X_AND_Y, cst);

	cst=ToBinary(XorY);
	SetDlgItemText(IDC_EDIT_X_OR_Y, cst);

	cst=ToBinary(XxorY);
	SetDlgItemText(IDC_EDIT_X_XOR_Y, cst);

}

CString CBinaryOperationView::ToBinary(int x)
{
	CString result;
	int i;
	unsigned int secX,shrW,uX;

	result="";
	uX =(unsigned int) x;
	for(i=7; i>=0; i--)
	{
		shrW=4*i;
		secX= ( uX & Sections[i]);
		if ( shrW > 0)
			secX = secX >> shrW;
		tmpstr = hexStr[secX];
		result += tmpstr;

		if (i>0)
			result += " ";

	}
	
	/////result.Format("%08X",x);
	return result;
}

void CBinaryOperationView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	int i;
	unsigned int secX,shrW,u;

	u =(unsigned int) m_X;
	for(i=0; i<8; i++)
	{
		shrW=4*i;
		secX= ( u & Sections[i]);
		if ( shrW > 0)
			secX = secX >> shrW;
		m_strX[i] = hexStr[secX];
	}

	u =(unsigned int) m_Y;
	for(i=0; i<8; i++)
	{
		shrW=4*i;
		secX= (u & Sections[i]);
		if ( shrW > 0)
			secX = secX >> shrW;
		m_strY[i] = hexStr[secX];
	}

	m_strXHex.Format("%08X",m_X);
	m_strYHex.Format("%08X",m_Y);

	UpdateData( FALSE );

	UpdateResults();

}

void CBinaryOperationView::OnChangeEditHexX()
{
	oldstrXHex=m_strXHex;

	UpdateData( TRUE );
	int len,i,ok;
	unsigned int secX,shrW,uX;

	if (oldstrXHex==m_strXHex)
		return;

	len=m_strXHex.GetLength();
	m_strXHex.MakeUpper();
	ok=TRUE;
	for(i=0; i<len && ok; i++)
	{
		ok = m_strXHex[i]>='0' && m_strXHex[i]<='9' ||
			 m_strXHex[i]>='A' && m_strXHex[i]<='F';
	}

	if (!ok)
	{
		m_strXHex=oldstrXHex;
		UpdateData( FALSE );
		return;
	}

	sscanf(m_strXHex,"%X",&m_X);

	uX = (unsigned int) m_X;
	for(i=0; i<8; i++)
	{
		shrW=4*i;
		secX= (uX & Sections[i]);
		if ( shrW > 0)
			secX = secX >> shrW;
		m_strX[i] = hexStr[secX];
	}

	UpdateData( FALSE );
	UpdateResults();
}

void CBinaryOperationView::OnChangeEditHexY()
{
	oldstrYHex=m_strYHex;

	UpdateData( TRUE );
	int len,i,ok;
	unsigned int secX,shrW,uY;

	if (oldstrYHex==m_strYHex)
		return;

	len=m_strYHex.GetLength();
	m_strYHex.MakeUpper();
	ok=TRUE;

	for(i=0; i<len && ok; i++)
	{
		ok = m_strYHex[i]>='0' && m_strYHex[i]<='9' ||
			 m_strYHex[i]>='A' && m_strYHex[i]<='F';
	}

	if (!ok)
	{
		m_strYHex=oldstrYHex;
		UpdateData( FALSE );
		return;
	}

	sscanf(m_strYHex,"%X",&m_Y);

	uY = (unsigned int) m_Y;
	for(i=0; i<8; i++)
	{
		shrW=4*i;
		secX= (uY & Sections[i]);
		if ( shrW > 0)
			secX = secX >> shrW;
		m_strY[i] = hexStr[secX];
	}

	UpdateData( FALSE );
	UpdateResults();

}

void CBinaryOperationView::OnChangeEditY()
{
	int i;
	for(i=0; i<8; i++)
	{
		oldstrY[i]=m_strY[i];
	}

	oldstrYHex=m_strYHex;

	oldY=m_Y;

	UpdateData( TRUE );

	int len,Index,strYChgCount=0;
	BOOL ok;

	/////////////////////////
	for(i=0; i<8; i++)
	{
		if (oldstrY[i] == m_strY[i]) continue;

		tmpstr=m_strY[i];
		strYChgCount++;

		len=tmpstr.GetLength();
		ok = TRUE;
		for( Index=0; Index<len && ok; Index++)
		{
			ok = tmpstr[Index]=='0' || tmpstr[Index]=='1';
		}

		if ( !ok) 
		{
			m_strY[i]=oldstrY[i];
			UpdateData( FALSE );
			return;
		}
	}


	int aInt,partInt;
	CString errMsg;

	//////////////////////////////
	if (strYChgCount>0)
	{
		partInt=0;
		for(i=7; i>=0; i--)
		{
			aInt = StrToInt( m_strY[i]);
			if ( aInt==-1)
			{
				errMsg.Format("m_strY[%d] Error?!",i);
				MessageBox(errMsg);
				return;
			}

			partInt=16*partInt+aInt;
		}

		m_Y=partInt;
		m_strYHex.Format("%08X",m_Y);

		UpdateData( FALSE );
		UpdateResults();
		return;
	}


	//////////////////////////////////////////
	unsigned int secX,shrW,uY;


	if ( oldY != m_Y )
	{
		uY = (unsigned int) m_Y;

		for(i=0; i<8; i++)
		{
			shrW=4*i;
			secX= (uY & Sections[i]);
			if ( shrW > 0)
				secX = secX >> shrW;
			m_strY[i] = hexStr[secX];
		}

		m_strYHex.Format("%08X",m_Y);

		UpdateData( FALSE );
		UpdateResults();
	}
}

void CBinaryOperationView::OnChangeEditX()
{
	int i;
	for(i=0; i<8; i++)
	{
		oldstrX[i]=m_strX[i];
	}

	oldstrXHex=m_strXHex;

	oldX=m_X;

	UpdateData( TRUE );

	int len,Index,strXChgCount=0;
	BOOL ok;

	for(i=0; i<8; i++)
	{
		if (oldstrX[i] == m_strX[i]) continue;

		tmpstr=m_strX[i];
		strXChgCount++;

		len=tmpstr.GetLength();
		ok = TRUE;
		for( Index=0; Index<len && ok; Index++)
		{
			ok = tmpstr[Index]=='0' || tmpstr[Index]=='1';
		}

		if ( !ok) 
		{
			m_strX[i]=oldstrX[i];
			UpdateData( FALSE );
			return;
		}
	}

	int aInt,partInt;
	CString errMsg;

	//////////////////////////////
	if (strXChgCount>0)
	{
		partInt=0;
		for(i=7; i>=0; i--)
		{
			aInt = StrToInt( m_strX[i]);
			if ( aInt==-1)
			{
				errMsg.Format("m_strX[%d] Error?!",i);
				MessageBox(errMsg);
				return;
			}

			partInt=16*partInt+aInt;
		}

		m_X=partInt;
		m_strXHex.Format("%08X",m_X);
		UpdateData( FALSE );
		UpdateResults();
		return;
	}


	//////////////////////////////////////////
	unsigned int secX,shrW;
	unsigned int uX=(unsigned int) m_X;

	if ( oldX != m_X )
	{

		for(i=0; i<8; i++)
		{
			shrW=4*i;
			secX= (uX & Sections[i]);
			if ( shrW > 0)
				secX = secX >> shrW;

			m_strX[i] = hexStr[secX];
		}

		m_strXHex.Format("%08X",m_X);

		UpdateData( FALSE );
		UpdateResults();
	}

}
/////////////////////////////////////////////////////////////////////////////
// CBinaryOperationView diagnostics

#ifdef _DEBUG
void CBinaryOperationView::AssertValid() const
{
	CFormView::AssertValid();
}

void CBinaryOperationView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CBinaryOperationDoc* CBinaryOperationView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBinaryOperationDoc)));
	return (CBinaryOperationDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBinaryOperationView message handlers

⌨️ 快捷键说明

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