登录 注册
当前位置:主页 > 资源下载 > 11 > MFC 计算器代码

MFC 计算器代码

  • 更新:2024-11-26 11:58:38
  • 大小:29.95MB
  • 推荐:★★★★★
  • 来源:网友上传分享
  • 类别:C/C++ - 课程资源
  • 格式:RAR

资源介绍

计算器 mfc 代码 基于VC++简易计算器实验详细过程 硬件环境: 软件环境:WinXP+VC++6.0 一、实验目的:构造一个类似Windows自带的计算器一样的简易计算器,能够连续进行加、减、乘、除四则整数运算,并能随时清除计算结果进行下一次计算。 二、具体实验步骤: 1、添加编辑框对应的变量m_Display 2、添加运算符类型变量 char CompuType; //用于存储运算符类型 CString FirstInput; //用于保存运算符前面的输入值 3、添加各种按钮的单击事件处理方法的函数 控件名称列表: 主对话框标识:IDD_CALC_DIALOG 关于对话框标识:IDD_ABOUTBOX 编辑框: IDC_EDIT 运算符按钮标识: 加: IDC_BUTTONADD 减: IDC_BUTTONSUBTRACT 乘: IDC_BUTTONMULTIPLY 除: IDC_BUTTONDIVIDE 等于: IDC_BUTTONEQUER 0~9数字符按钮标识: 7:IDC_BUTTONSEVEN 8:DC_BUTTONEIGHT 9: IDC_BUTTONNINE 4:IDC_BUTTONFOUR 5:IDC_BUTTONFIVE 6:IDC_BUTTONSIX 1:C_BUTTONONE 2:IDC_BUTTONTWO 3:IDC_BUTTONTHREE 0:IDC_BUTTONZERO 清除按钮标识:IDC_BUTTONCLEAR 关于按钮标识:IDC_BUTTONABOUT 类的成员变量: class CCalcDlg : public CDialog { // Construction public: char CompuType; //用于存储运算符类型 CCalcDlg(CWnd* pParent = NULL); // standard constructor //… (省略) protected: CString FirstInput; //用于保存运算符前面的输入值 //… (省略) }; 按钮单击的事件对应方法程序列表: void CCalcDlg::OnButtonone() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("1"); UpdateData(FALSE); } void CCalcDlg::OnButtontwo() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("2"); UpdateData(FALSE); } void CCalcDlg::OnButtonthree() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("3"); UpdateData(FALSE); } void CCalcDlg::OnButtonfour() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("4"); UpdateData(FALSE); } void CCalcDlg::OnButtonfive() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("5"); UpdateData(FALSE); } void CCalcDlg::OnButtonsix() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("6"); UpdateData(FALSE); } void CCalcDlg::OnButtonseven() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("7"); UpdateData(FALSE); } void CCalcDlg::OnButtoneight() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("8"); UpdateData(FALSE); } void CCalcDlg::OnButtonnine() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("9"); UpdateData(FALSE); } void CCalcDlg::OnButtonzero() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("0"); UpdateData(FALSE); } void CCalcDlg::OnButtonadd() { // TODO: Add your control notification handler code here FirstInput = m_Display; m_Display = _T(""); UpdateData(FALSE); CompuType='+'; } void CCalcDlg::OnButtonsubtract() { // TODO: Add your control notification handler code here FirstInput = m_Display; m_Display = _T(""); UpdateData(FALSE); CompuType='-'; } void CCalcDlg::OnButtondivide() { // TODO: Add your control notification handler code here FirstInput = m_Display; m_Display = _T(""); UpdateData(FALSE); CompuType='/'; } void CCalcDlg::OnButtonmultiply() { // TODO: Add your control notification handler code here FirstInput = m_Display; m_Display = _T(""); UpdateData(FALSE); CompuType='*'; } void CCalcDlg::OnButtonequer() { // TODO: Add your control notification handler code here int TempResult; //定义存储结果的临时变量 int FirstInputValue,NewInputValue; //定义进行运算的临时变量 FirstInputValue=atoi(FirstInput.GetBuffer(24)); //将字符串变量转换成整型值 FirstInput.ReleaseBuffer(-1); NewInputValue=atoi(m_Display.GetBuffer(24)); //将字符串变量转换成整型值 m_Display.ReleaseBuffer(-1); switch(CompuType) //开关语句判断运算符类型 { case '+': TempResult = FirstInputValue + NewInputValue; //将加法运算结果传给出临时变量TempResult break; case '-': TempResult = FirstInputValue - NewInputValue; //将减法运算结果传给出临时变量TempResult break; case '*': TempResult = FirstInputValue * NewInputValue; //将乘法运算结果传给出临时变量TempResult break; case '/': TempResult = FirstInputValue / NewInputValue; //将除法运算结果传给出临时变量TempResult break; } sprintf(m_Display.GetBuffer(24),"%d",TempResult); //将运算结果变量值传递给出编辑控件变量m_Display m_Display.ReleaseBuffer(-1); UpdateData(FALSE); //刷新编辑控件变量,并在编辑框中显示运算结果 } void CCalcDlg::OnButtonclear() { // TODO: Add your control notification handler code here m_Display.Empty(); //清空存储在变量m_Display中的数据 UpdateData(FALSE); } void CCalcDlg::OnButtonabout() { // TODO: Add your control notification handler code here CAboutDlg DlgAbout; DlgAbout.DoModal();