ciw_blue吧 关注:22贴子:259
  • 2回复贴,共1

【源代码】简单计算器

只看楼主收藏回复

早期作品,很简单的MFC程序...

void CMfc_CounterDlg::OnOK() 
{
// TODO: Add extra validation here
}

void CMfc_CounterDlg::OnAdd() //按下了+号键,计算 + 运算
{
// TODO: Add your control notification handler code here

m_isBtnDown=TRUE;  //判断+号是否按下
m_countType='+';
 
OnAmount() ;    //计算结果
}

void CMfc_CounterDlg::OnMinus() //按下了-号键,计算 - 运算
{
// TODO: Add your control notification handler code here

m_isBtnDown=TRUE;  //判断-号是否按下
    m_countType='-';


OnAmount() ;    //计算结果
}

void CMfc_CounterDlg::OnChu() //按下了/号键,计算 / 运算
{
// TODO: Add your control notification handler code here

m_isBtnDown=TRUE;  //判断/号是否按下
    m_countType='/';

OnAmount() ;    //计算结果
}


void CMfc_CounterDlg::OnRide() //按下了*号键,计算 * 运算
{
// TODO: Add your control notification handler code here

m_isBtnDown=TRUE;  //判断*号是否按下
    m_countType='*';
    
OnAmount() ;    //计算结果
}

void CMfc_CounterDlg::OnEight() //按下了“8” 
{
OnCount('8');  //是按下那个数字
}

void CMfc_CounterDlg::OnFive() //按下了“5” 
{
OnCount('5');  //是按下那个数字
}

void CMfc_CounterDlg::OnFour() //按下了“4” 
{
OnCount('4');  //是按下那个数字
}

void CMfc_CounterDlg::OnNine() //按下了“9” 
{
OnCount('9'); //是按下那个数字
}

void CMfc_CounterDlg::OnOne() //按下了“1” 
{
// TODO: Add your control notification handler code here

OnCount('1');  //是按下那个数字
}

void CMfc_CounterDlg::OnSeven() //按下了“7” 
{
// TODO: Add your control notification handler code here
OnCount('7');  //是按下那个数字
}

void CMfc_CounterDlg::OnSix() //按下了“6” 
{
// TODO: Add your control notification handler code here
OnCount('6'); //是按下那个数字
}

void CMfc_CounterDlg::OnThree() //按下了“3” 
{
// TODO: Add your control notification handler code here
OnCount('3');  //是按下那个数字
}

void CMfc_CounterDlg::OnTwo() //按下了“2” 
{
// TODO: Add your control notification handler code here
OnCount('2');  //是按下那个数字
}

void CMfc_CounterDlg::OnZero() //按下了“0” 
{
// TODO: Add your control notification handler code here
OnCount('0');  //是按下那个数字
}

void CMfc_CounterDlg::OnAmount() //计算结果
{
// TODO: Add your control notification handler code here


m_isBtnDown=TRUE;  //判断=号是否按下


m_secondNum=atof(m_number);  //字符串转double 


SetDlgItemText(IDC_EDIT1,m_number);

/*
计算结果
*/
if(m_countType=='+')//计算 和 
{
m_count.Format("%lf",m_firstNum+m_secondNum);    
}
else if(m_countType=='-')//计算 减
{
if(m_firstNum!=0)
{
m_count.Format("%lf",m_firstNum-m_secondNum);  
}
else
{m_firstNum=m_secondNum;
m_count.Format("%lf",m_firstNum);
}
}
else if(m_countType=='*') //计算 乘
{
if(m_firstNum==0)
{
m_firstNum=1;
}
m_count.Format("%lf",m_firstNum*m_secondNum);  

}
else if(m_countType=='/') //计算 除
{   if(m_firstNum==0)
{
m_firstNum=m_secondNum;
m_secondNum=1;
}
if(m_secondNum!=0)
{
m_count.Format("%lf",m_firstNum/m_secondNum);   
}
else
{
m_number="被除数不能为0,重新输入!";
UpdateData(FALSE);
}
}

UpdateData(FALSE);
SetDlgItemText(IDC_EDIT1,m_count);

}



1楼2008-04-05 21:16回复
    void CMfc_CounterDlg::OnCount(char ch) //是按下那个数字
    {


    if(m_isBtnDown==TRUE ) //如果用户按下了 + - * / 其中一个
    {
    UpdateData();
    m_firstNum=atof(m_number); //字符串转成double
    m_number.Empty();   //情空m_number
    m_number=m_number+ch; //加上按下的数字
    SetDlgItemText(IDC_EDIT1,m_number);
    UpdateData(FALSE);

    m_isBtnDown=FALSE;

    }
    else
    {   
    UpdateData();

    m_number=m_number+ch; //加上按下的数字
    m_secondNum=atof(m_number); //字符串转成double
    SetDlgItemText(IDC_EDIT1,m_number);

    UpdateData(FALSE);

    }
    }

    int CMfc_CounterDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here
    m_secondNum =0; //初始化
    m_firstNum =0; //初始化

    return 0;
    }

    void CMfc_CounterDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
    {
    CDialog::OnShowWindow(bShow, nStatus);

    ((CEdit*)GetDlgItem(IDC_EDIT1))->SetLimitText(12);

    SetWindowText("计算器");
    // TODO: Add your message handler code here

    }

    void CMfc_CounterDlg::OnPoint() //增加小数点
    {
    // TODO: Add your control notification handler code here

    CString str;
    GetDlgItemText(IDC_EDIT1,str);
     
    m_isPoint=m_number.Find('.'); //查找是否有小数点
    if( m_isPoint==-1 && (str.Find('.')==-1) ) //如果没有的话
    {
    m_number=m_number+'.'; //加上小数点
    UpdateData(FALSE);
    }

    }

    void CMfc_CounterDlg::OnChu2() 
    {
    // TODO: Add your control notification handler code here
    m_number="";
    UpdateData(FALSE);
    m_firstNum=m_secondNum=0;
    }


    void CMfc_CounterDlg::OnClose() 
    {
    // TODO: Add your message handler code here and/or call default
    CDialog::OnClose();
    }

    void CMfc_CounterDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here

    }


    2楼2008-04-05 21:16
    回复


      3楼2011-03-16 09:14
      回复