MFC,VC++计算器小程序

大学期末没课,某个中午闲来无聊,正好在自学MFC,于是想用MFC、C++写点东西,由于能力有限,当然的写个简单点的啦,于是花了两个小时写了这个计算器的小程序,希望对初学VC++和MFC的朋友有所帮助。程序运行效果如下:




(1)首先按上图添加控件;

(2)然后在Dlg类的头文件中添加如下的变量,功能如注释所示:


(3)下面是程序的主要代码:

void CMyDlg::OnZero() 
{
	// TODO: Add your control notification handler code here
	m_showend+="0";
	UpdateData(false);
}


void CMyDlg::OnOne() 
{
	// TODO: Add your control notification handler code here
		m_showend+="1";
	UpdateData(false);
}


void CMyDlg::OnTwo() 
{
	// TODO: Add your control notification handler code here
		m_showend+="2";
	UpdateData(false);
}


void CMyDlg::OnThree() 
{
	// TODO: Add your control notification handler code here
		m_showend+="3";
	UpdateData(false);
}


void CMyDlg::OnFour() 
{
	// TODO: Add your control notification handler code here
		m_showend+="4";
	UpdateData(false);
}


void CMyDlg::OnFive() 
{
	// TODO: Add your control notification handler code here
		m_showend+="5";
	UpdateData(false);
}


void CMyDlg::OnSix() 
{
	// TODO: Add your control notification handler code here
		m_showend+="6";
	UpdateData(false);
}


void CMyDlg::OnSeven() 
{
	// TODO: Add your control notification handler code here
		m_showend+="7";
	UpdateData(false);
}


void CMyDlg::OnEight() 
{
	// TODO: Add your control notification handler code here
		m_showend+="8";
	UpdateData(false);
}


void CMyDlg::OnNine() 
{
	// TODO: Add your control notification handler code here
		m_showend+="9";
	UpdateData(false);
}


void CMyDlg::OnClearEditBox() //此函数用于清空编辑框信息
{
	// TODO: Add your control notification handler code here
	m_showend="";
	operand_one=0.0;
	operand_two=0.0;
	UpdateData(false);	//更新编辑框信息
}


void CMyDlg::OnRun() 
{
	// TODO: Add your control notification handler code here
	operand_two=atof(m_showend);
	double end=0.0;


	//此处为判断操作的类型;
	if(operate==add)
	{
		end=operand_one+operand_two;
		m_showend.Format("%g",end);
	}
	else if(operate==subtraction)
	{
		end=operand_one-operand_two;
		m_showend.Format("%g",end);
	}
	else if(operate==multiplication)
	{
		end=operand_one*operand_two;
		m_showend.Format("%g",end);
	}
	else if(operate==division)
	{
		end=operand_one/operand_two;
		m_showend.Format("%g",end);
	}
	UpdateData(false);
}


void CMyDlg::OnDivision() //除法运算
{
	// TODO: Add your control notification handler code here
	operand_one=atof(m_showend);
	m_showend="";
	UpdateData(false);


	operate=division;//操作类型为“除”;
}


void CMyDlg::OnMultiplication() //乘法运算
{
	// TODO: Add your control notification handler code here
	operand_one=atof(m_showend);
	m_showend="";
	UpdateData(false);


	operate=multiplication;//操作类型为“乘”;
}


void CMyDlg::OnSubtraction() //减法运算
{
	// TODO: Add your control notification handler code here
	operand_one=atof(m_showend);
	m_showend="";
	UpdateData(false);


	operate=subtraction;//操作类型为“减”;
}


void CMyDlg::OnAdd() //加法运算
{
	// TODO: Add your control notification handler code here
	operand_one=atof(m_showend);
	m_showend="";
	UpdateData(false);


	operate=add;//操作类型为“加”;


}


void CMyDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	AfxMessageBox("345234");
	switch(nChar)
	{
		case 48:CMyDlg::OnZero();break;	
		case 49:CMyDlg::OnOne();break;
		case 50:CMyDlg::OnTwo();break;
		case 51:CMyDlg::OnThree();break;
		case 52:CMyDlg::OnFour();break;
		case 53:CMyDlg::OnFive();break;
		case 54:CMyDlg::OnSix();break;
		case 55:CMyDlg::OnSeven();break;
		case 56:CMyDlg::OnEight();break;
		case 57:CMyDlg::OnNine();break;
		default:break;
	}
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}


BOOL CMyDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message == WM_KEYDOWN)
	{
		if(pMsg->wParam==VK_ESCAPE)	//忽略掉键盘上的esc按键消息
		{
			return true;
		}
		//CMyDlg::OnKeyDown(nChar, nRepCnt, nFlags) ;
		switch(pMsg->wParam)	
		{
			case 48:CMyDlg::OnZero();break;	
			case 49:CMyDlg::OnOne();break;
			case 50:CMyDlg::OnTwo();break;
			case 51:CMyDlg::OnThree();break;
			case 52:CMyDlg::OnFour();break;
			case 53:CMyDlg::OnFive();break;
			case 54:CMyDlg::OnSix();break;
			case 55:CMyDlg::OnSeven();break;
			case 56:CMyDlg::OnEight();break;
			case 57:CMyDlg::OnNine();break;
			default:break;
		
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

以上是程序的主要代码;

当然了,还得为编辑框添加一个CString类型的变量,用来保存输入的数据;下面是对话框头文件中需要定义的变量


主要的代码就这些吧,怎么添加响应函数就不用说了吧,嘿嘿


这是资源的下载地址:

计算器MFC+VC++ - 下载频道 - CSDN.NET
http://download.csdn.net/detail/rl529014/8842801

  • 12
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值