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

📄 计算机小数计算模拟器dlg.cpp

📁 该软件以图形化界面显示在计算机内部定点小数补码加法、定点小数补码减法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 计算机小数计算模拟器Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "计算机小数计算模拟器.h"
#include "计算机小数计算模拟器Dlg.h"
#include "Instruction.h"
#include "AboutDlg.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);
	DDV_MaxChars(pDX, m_nInput1, 10);
	DDX_Text(pDX, IDC_INPUT_SECOND, m_nInput2);
	DDV_MaxChars(pDX, m_nInput2, 10);
	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_ADD, OnAdd)
	ON_BN_CLICKED(IDC_SUB, OnSub)
	ON_BN_CLICKED(IDC_OMULTIPLY, OnOmultiply)
	ON_BN_CLICKED(IDC_BMULTIPLY, OnBmultiply)
	ON_BN_CLICKED(IDC_ODEVIDE, OnOdevide)
	ON_BN_CLICKED(IDC_BDEVIDE, OnBdevide)
	ON_BN_CLICKED(IDC_INSTRUCTION, OnInstruction)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	//}}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
	
	 // return TRUE  unless you set the focus to a control
	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+="                    制作日期:2004年12月";
	GetDlgItem(IDC_INPUT_FIRST)->SetWindowText(m_nInput1);
	GetDlgItem(IDC_INPUT_SECOND)->SetWindowText(m_nInput2);
	GetDlgItem(IDC_RESULT)->SetWindowText(m_nResult);
	return TRUE; 
}

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");
}





CString ADD(CString s1,CString s2)
{
	CString output;
	CString result="0.00000000";
	bool overflow;
	int c[10]={0};
	for(int i=9;i>=0;i--)
	{
		if(s1[i]=='.')
		{
			result.SetAt(i,'.');
			c[i-1]=c[i];
		}
		else if(s1[i]=='0'&&s2[i]=='0')
		{
			if(c[i]==1)result.SetAt(i,'1');
			else  result.SetAt(i,'0');
			if(i>0) c[i-1]=0;
		}	
		else if((s1[i]=='0'&&s2[i]=='1')||(s1[i]=='1'&&s2[i]=='0'))
		{
			if(c[i]==1)
			{
				result.SetAt(i,'0');
				if(i>0) c[i-1]=1;
			}
			else 
			{
				result.SetAt(i,'1');
				if(i>0) c[i-1]=0;
			}
		}
		else //(s1[i]=='1'&&s2[i]=='1')
		{
			if(c[i]==1)
			{
				result.SetAt(i,'1');
				if(i>0)c[i-1]=1;
			}
			else
			{
				result.SetAt(i,'0');
				if(i>0)c[i-1]=1;
			}
		}
	}
	if(s1[0]==s2[0]&&s1[0]!=result[0])
	{
		AfxMessageBox("溢出");
		overflow=TRUE;
		
	}
	output="         ";
	output+=s1;
	output+="\r\n         ";
	output+=s2;
	output+="\r\n+_____________________\r\n         ";
	output+=result+"\r\n";
	CString tmp1,tmp2;
	tmp2=BtoY(result);
	tmp1="变为原码为:"+tmp2;
	output+=tmp1;
	return output;
}

CString LiuAdd(CString s1,CString s2)
{
	CString result="0.00000000";
	bool overflow;
	int c[10]={0};
	for(int i=9;i>=0;i--)
	{
		if(s1[i]=='.')
		{
			result.SetAt(i,'.');
			if(i>0) c[i-1]=c[i];
		}
		else if(s1[i]=='0'&&s2[i]=='0')
		{
			if(c[i]==1)result.SetAt(i,'1');
			else result.SetAt(i,'0');
			if(i>0) c[i-1]=0;
		}	
		else if((s1[i]=='0'&&s2[i]=='1')||s1[i]=='1'&&s2[i]=='0')
		{
			if(c[i]==1)
			{
				result.SetAt(i,'0');
				if(i>0) c[i-1]=1;
			}
			else 
			{
				result.SetAt(i,'1');
				if(i>0) c[i-1]=0;
			}
		}
		else
		{
			if(c[i]==1)
			{
				result.SetAt(i,'1');
				if(i>0) c[i-1]=1;
			}
			else
			{
				result.SetAt(i,'0');
				if(i>0) c[i-1]=1;
			}
		}
	}
	if(s1[0]==s2[0]&&s1[0]!=result[0])
	{
		AfxMessageBox("溢出");//cout<<"溢出"<<endl;
		overflow=TRUE;
		
	}
	return result;
}

CString SUB(CString s1,CString s2)
{
	CString output;
	CString result;
	CString minus_s2="0.00000000";
	for(int i=0;i<10;i++)
	{
		if(s2[i]=='1')
			minus_s2.SetAt(i,'0');
		else if(s2[i]=='0')
			minus_s2.SetAt(i,'1');
		else minus_s2.SetAt(i,'.');
	}
	minus_s2=LiuAdd(minus_s2,"0.00000001");
	result=LiuAdd(s1,minus_s2);
	if(s1[0]==minus_s2[0]&&s1[0]!=result[0])
	{
		AfxMessageBox("溢出");
		
	}
	output="        ";
	output+=s1;
	output+="\r\n";
	output+="        ";
	output+=s2;
	output+="\r\n";
	output+="-__________________";
	output+="       ";
	output+="       \r\n        ";
	output+=result+"\r\n";
	CString tmp1,tmp2;
	tmp2=BtoY(result);
	tmp1="变为原码为:"+tmp2;
	output+=tmp1;
	return output;
}





CString OMultiply(CString s1,CString s2)
{
	CString output;
	
	CString xstar="0.00000000";
	CString signx=s1[0];
	CString ystar="0.00000000";
	CString signy=s2[0];
	CString bufenji="0.00000000";
	CString chenshu="00000000";
	CString sign="0";
	xstar.SetAt(0,'0');
	xstar.SetAt(1,'.');
	ystar.SetAt(0,'0');
	ystar.SetAt(1,'.');

	for(int i=2;i<10;i++)
	{
		xstar.SetAt(i,s1[i]);
		ystar.SetAt(i,s2[i]);
	}
	bufenji.SetAt(0,'0');
	bufenji.SetAt(1,'.');
	for(i=2;i<10;i++)
	{
		chenshu.SetAt(i-2,s2[i]);
		bufenji.SetAt(i,'0');
	}
	CString one,two,three;
	one="    ";
	two="    ";
	three="─────────────";
	output=one+"部 分 积"+two+" 乘 数"+"\r\n"+three+"\r\n";
	CString tmp;
    tmp=one+bufenji+two+chenshu+"\r\n";
	output+=tmp;
	for(int j=0;j<8;)
	{


		if(chenshu[7]=='1')
		{
			one.SetAt(1,'+');
			tmp=one+xstar+two+"\r\n"+three+"\r\n";
			output+=tmp;
		
			bufenji=Headd(bufenji,xstar);
			one.SetAt(1,' ');
			tmp=one+bufenji+"\r\n";
			output+=tmp;
//移位操作
			for(int i=7;i>0;i--)
			{
				chenshu.SetAt(i,chenshu[i-1]);
			}
			chenshu.SetAt(0,bufenji[9]);
			for(i=9;i>2;i--)
			{
				bufenji.SetAt(i,bufenji[i-1]);
			}
			bufenji.SetAt(2,bufenji[0]);
			bufenji.SetAt(0,'0');
			tmp=one+bufenji+two+chenshu+"\r\n";
			output+=tmp;
			
			j++;
		}

		else 
		{	
			one.SetAt(1,'+');
			tmp=one+"0.00000000"+two+"\r\n"+three+"\r\n";
			output+=tmp;
		
			bufenji=Headd(bufenji,"0.00000000");
			one.SetAt(1,' ');
			tmp=one+bufenji+"\r\n";
			output+=tmp;
//移位操作
			for(int i=7;i>0;i--)
			{
				chenshu.SetAt(i,chenshu[i-1]);
			}
			chenshu.SetAt(0,bufenji[9]);
			for(i=9;i>2;i--)
			{
				bufenji.SetAt(i,bufenji[i-1]);
			}
			bufenji.SetAt(2,bufenji[0]);
			bufenji.SetAt(0,'0');
			tmp=one+bufenji+two+chenshu+"\r\n";
			output+=tmp;
			
			j++;
		}
	}

	if((signx=='1'&&signy=='1')||(signx=='0'&&signy=='0'))
		sign="0";
	else sign="1";

	tmp="\r\n加上符号位为:"+sign+"\r\n";
	output+=tmp;
	bufenji.SetAt(0,sign[0]);
	tmp="结果为:\r\n";
	output+=tmp;
	tmp=one+bufenji+chenshu+"\r\n";
	output+=tmp;
	tmp="截去超出的位,最终的结果为:\r\n";
	output+=tmp;
	tmp=one+bufenji;
	output+=tmp;



return output;
}


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

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

void opposition(int from[],int to[])/////////取相反数
{
	int buf[10]={0,0,0,0,0,0,0,0,0,1};
	int acc[10];
	if(from[0]==1)
	{
		to[0]=0;
		to[1]=0;
		for(int i=2;i<10;i++)
		{
			to[i]=xor(from[i],1);
		}
		huadd(to,buf,acc);
		for(i=0;i<10;i++)
		{
			to[i]=acc[i];
		}
	}
	else
	{
		to[0]=1;
		to[1]=1;
		for(int i=2;i<10;i++)
		{
			to[i]=xor(from[i],1);
		}
		huadd(to,buf,acc);
		for(i=0;i<10;i++)
		{
			to[i]=acc[i];
		}
	}
}

⌨️ 快捷键说明

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