mfcc计算器源码(mfc设计计算器具体步骤)
本文目录一览:
- 1、求用MFC编写的计算器的源代码,请发到我邮箱heyanhong6688@163.com,非常感谢
- 2、求助一个用MFC来编写的计算器程序代码
- 3、MFC 简单计算器代码
- 4、求MFC编写计算器的详细方法 步骤 代码
- 5、MFC编写计算器具体步骤 方法 代码,拜托高手急用!
求用MFC编写的计算器的源代码,请发到我邮箱heyanhong6688@163.com,非常感谢
在MFC
AppWized(exe)项目下做。界面自己做
1.在对话框的头文件CalculatorDlg.h中添加#includemath.h
2.为CCalculatorDlg类添加成员数据和成员函数
double
number1,number2;
int
NumberState,OperationState;
void
cal();
并在CCalculatorDlg类的构造函数中增加NumberState=1;
3.添加消息按钮afx_msg
void
OnNumberKey(UINT
nID);
afx_msg
void
OnOperationKey(UINT
nID);
4.在CalculatorDlg.cpp文件中BEGIN_MESSAGE_MAP(CMy1Dlg,
CDialog)和END_MESSAGE_MAP()间添加代码
ON_COMMAND_RANGE(IDC_NUMBER1,IDC_NUMBER10,OnNumberKey)
ON_COMMAND_RANGE(IDC_NUMBER11,IDC_NUMBER20,OnOperationKey)
5.为成员函数OnNumberKey和OnOperationKey添加代码
void
CCalculatorDlg::OnNumberKey(UINT
nID)
{
int
n=0;
switch(nID)
{
case
IDC_NUMBER1:n=1;break;
case
IDC_NUMBER2:n=2;break;
case
IDC_NUMBER3:n=3;break;
case
IDC_NUMBER4:n=4;break;
case
IDC_NUMBER5:n=5;break;
case
IDC_NUMBER6:n=6;break;
case
IDC_NUMBER7:n=7;break;
case
IDC_NUMBER8:n=8;break;
case
IDC_NUMBER9:n=9;break;
case
IDC_NUMBER10:n=0;break;
}
if(NumberState==1)
{
m_result=m_result*10+n;
number1=m_result;
UpdateData(FALSE);
//
更新编辑框中的值
}
else
{
m_result=m_result*10+n;
number2=m_result;
UpdateData(FALSE);
}
}
void
CCalculatorDlg::OnOperationKey(UINT
nID)
{
switch(nID)
{
case
IDC_NUMBER13:
//
"/"按钮
OperationState=1;
UpdateData(FALSE);
m_result=0;
NumberState=2;
break;
case
IDC_NUMBER14:
//
"*"按钮
OperationState=2;
UpdateData(FALSE);
m_result=0;
NumberState=2;
break;
case
IDC_NUMBER15:
//
"+"按钮
OperationState=3;
UpdateData(FALSE);
m_result=0;
NumberState=2;
break;
case
IDC_NUMBER16:
//
"-"按钮
OperationState=4;
UpdateData(FALSE);
m_result=0;
NumberState=2;
break;
case
IDC_NUMBER17:
//
"C"按钮,撤消用,不需要可以删除
number1=number2=m_result=0;
UpdateData(FALSE);
NumberState=1;
break;
case
IDC_NUMBER20:
//
"="按钮
cal();
//
调用cal成员函数
break;
}
}
6.为成员函数cal()添加代码
void
CCalculatorDlg::cal()
{
switch(OperationState)
{
case
1:
m_result=(double)number1/number2;
UpdateData(FALSE);
//
更新编辑框中的结果
number1=m_result;
//
把此次的运算结果作为下一次运算的第一个操作数
NumberState=2;
//
下次输入的数作为第二个操作数
break;
case
2:
m_result=number1*number2;
UpdateData(FALSE);
//
更新编辑框中的结果
number1=m_result;
NumberState=2;
break;
case
3:
m_result=number1+number2;
UpdateData(FALSE);
//
更新编辑框中的结果
number1=m_result;
NumberState=2;
break;
case
4:
m_result=number1-number2;
UpdateData(FALSE);
//
更新编辑框中的结果
number1=m_result;
NumberState=2;
break;
}
OperationState=0;
}
注意按钮的ID号要和程序中的ID号相同!!!!要不然会有的按钮点击没反应……
求助一个用MFC来编写的计算器程序代码
主代码如下。。这个是我自己做的。就怕你看不懂了。。。
不过敢上来问,应该是比较精通的了。。
#include
"stdafx.h"
#include
stdio.h
#include
stdlib.h
#include
windows.h
#include
windowsx.h
#include
"resource.h"
#include
"MainDlg.h"
#define
TURE
1
#define
FALSE
BOOL
WINAPI
Main_Proc(HWND
hWnd,
UINT
uMsg,
WPARAM
wParam,
LPARAM
lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd,
WM_INITDIALOG,
Main_OnInitDialog);
HANDLE_MSG(hWnd,
WM_COMMAND,
Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE,
Main_OnClose);
}
return
FALSE;
}
BOOL
Main_OnInitDialog(HWND
hwnd,
HWND
hwndFocus,
LPARAM
lParam)
{
return
TRUE;
}
BOOL
IsInt(TCHAR*
str)
{
int
i=atoi(str);
TCHAR
strtemp[256];
wsprintf(strtemp,"%i",i);
if(strcmp(str,strtemp)==0)
{
return
TURE;
}
else
{
return
FALSE;
}
}
TCHAR
str1[256],str2[256],str3[256];
int
i1,i2,i3;
void
Main_OnCommand(HWND
hwnd,
int
id,
HWND
hwndCtl,
UINT
codeNotify)
{
switch(id)
{
case
IDC_OK:
{
GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
int
i1,i2,i3;
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);i2=atoi(str2);i3=i1+i2;
wsprintf(str3,"%i",i3);
SetDlgItemText(hwnd,IDC_EDIT3,str3);
}
break;
case
IDC_OK2:
{
GetDlgItemText(hwnd,IDC_EDIT4,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT5,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
i2=atoi(str2);
i3=i1-i2;
wsprintf(str3,"%i",i3);
SetDlgItemText(hwnd,IDC_EDIT6,str3);
}
break;
case
IDC_OK3:
{
GetDlgItemText(hwnd,IDC_EDIT7,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT8,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);i2=atoi(str2);
i3=i1*i2;
itoa(i3,str3,10);
SetDlgItemText(hwnd,IDC_EDIT9,str3);
}
break;
case
IDC_OK4:
{
GetDlgItemText(hwnd,IDC_EDIT10,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT11,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
i2=atoi(str2);
i3=i1/i2;
itoa(i3,str3,10);
//else
str3="除不掉";这里仍然有个bug!
SetDlgItemText(hwnd,IDC_EDIT12,str3);
}
break;
case
IDC_OK5:
{
GetDlgItemText(hwnd,IDC_EDIT13,str1,sizeof(str1));
i1=atoi(str1);
itoa(i1,str2,2);
SetDlgItemText(hwnd,IDC_EDIT14,str2);
}
break;
case
IDC_OK6:
{
GetDlgItemText(hwnd,IDC_EDIT15,str1,sizeof(str1));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
itoa(i1,str2,16);
SetDlgItemText(hwnd,IDC_EDIT16,str2);
}
break;
default:
break;
}
}
void
Main_OnClose(HWND
hwnd)
{
EndDialog(hwnd,
0);
}
MFC 简单计算器代码
class CCalculatorDlg : public CDialog
{
// Construction
public:
void SaveValue1();
CCalculatorDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CCalculatorDlg)
enum { IDD = IDD_CALCULATOR_DIALOG };
CEdit m_edit;span style="white-space:pre" /span//显示使用一个编辑框
CString m_str;span style="white-space:pre" /span//编辑框内的字符串
//输入的2个值
double m_fNumber1;
double m_fNumber2;
//保存计算符号类型 0='+',1='-',2='*',3='/'
int m_iSign;
//计算
void Calculator();
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCalculatorDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CCalculatorDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnMyAbout();
afx_msg void OnButtonBack();
afx_msg void OnButtonClear();
afx_msg void OnButton1();
afx_msg void OnButton2();
afx_msg void OnButton3();
afx_msg void OnButton4();
afx_msg void OnButton5();
afx_msg void OnButton6();
afx_msg void OnButton7();
afx_msg void OnButton8();
afx_msg void OnButton9();
afx_msg void OnButton0();
afx_msg void OnButtonDian();
afx_msg void OnButtonJia();
afx_msg void OnButtonJian();
afx_msg void OnButtonCheng();
afx_msg void OnButtonChu();
afx_msg void OnButtonDeng();
afx_msg void OnButtonPingFang();
afx_msg void OnButtonKaiFang();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CalculatorDlg.cpp文件中:
[cpp] view plain copy
void CCalculatorDlg::OnButtonBack()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
//移除最右边一个字符
if (!m_str.IsEmpty())
{
m_str = m_str.Left(m_str.GetLength()-1);
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonClear()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = "";
m_fNumber1 = 0.0f;
m_fNumber2 = 0.0f;
m_iSign = 0;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "1";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton2()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "2";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton3()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "3";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton4()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "4";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton5()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "5";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton6()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "6";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton7()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "7";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton8()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "8";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton9()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "9";
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton0()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_str = m_str + "0";
UpdateData(FALSE);
}
//小数点按钮
void CCalculatorDlg::OnButtonDian()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
//如果没有小数点mfcc计算器源码,则加上一个小数点mfcc计算器源码,如果已有小数点就忽略此次的小数点mfcc计算器源码,保证最多只有1个
if (-1 == m_str.Find('.'))
{
m_str = m_str + ".";
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonJia()
{
// TODO: Add your control notification handler code here
SaveValue1();
m_iSign = 0;
}
void CCalculatorDlg::OnButtonJian()
{
// TODO: Add your control notification handler code here
SaveValue1();
m_iSign = 1;
}
void CCalculatorDlg::OnButtonCheng()
{
// TODO: Add your control notification handler code here
SaveValue1();
m_iSign = 2;
}
void CCalculatorDlg::OnButtonChu()
{
// TODO: Add your control notification handler code here
SaveValue1();
m_iSign = 3;
}
void CCalculatorDlg::OnButtonDeng()
{
// TODO: Add your control notification handler code here
Calculator();
}
void CCalculatorDlg::SaveValue1()
{
UpdateData(TRUE);
m_fNumber1 = atof(m_str);
m_str = "";
UpdateData(FALSE);
}
void CCalculatorDlg::Calculator()
{
UpdateData(TRUE);
m_fNumber2 = atof(m_str);
double f = 0.0f;
switch (m_iSign)
{
//加
case 0:
f = m_fNumber1 + m_fNumber2;
break;
//减
case 1:
f = m_fNumber1 - m_fNumber2;
break;
//乘
case 2:
f = m_fNumber1 * m_fNumber2;
break;
//除
case 3:
if (m_fNumber2 == 0.0f)
{
f = m_fNumber1;
}
else
{
f = m_fNumber1 / m_fNumber2;
}
break;
//平方
case 4:
f = m_fNumber2 * m_fNumber2;
break;
//开方
case 5:
f = sqrt(m_fNumber2);
break;
default:
break;
}
//如果浮点数其实是个整数
if(f - int(f) = 1e-5)
{
m_str.Format("%d",(int)f);
}
else
{
m_str.Format("%f",f);
}
UpdateData(FALSE);
m_fNumber1 = f;
m_fNumber2 = 0.0f;
}
void CCalculatorDlg::OnButtonPingFang()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_fNumber1 = atof(m_str);
double f = m_fNumber1 * m_fNumber1;
//如果浮点数其实是个整数
if(f - int(f) = 1e-5)
{
m_str.Format("%d",(int)f);
}
else
{
m_str.Format("%f",f);
}
UpdateData(FALSE);
m_fNumber1 = f;
m_fNumber2 = 0.0f;
m_iSign = 4;
}
void CCalculatorDlg::OnButtonKaiFang()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_fNumber1 = atof(m_str);
double f = sqrt(m_fNumber1);
//如果浮点数其实是个整数
if(f - int(f) = 1e-5)
{
m_str.Format("%d",(int)f);
}
else
{
m_str.Format("%f",f);
}
UpdateData(FALSE);
m_fNumber1 = f;
m_fNumber2 = 0.0f;
m_iSign = 5;
}
求MFC编写计算器的详细方法 步骤 代码
在MFC AppWized(exe)项目下做。界面自己做
1.在对话框的头文件CalculatorDlg.h中添加#includemath.h
2.为CCalculatorDlg类添加成员数据和成员函数
double number1,number2;
int NumberState,OperationState;
void cal();
并在CCalculatorDlg类的构造函数中增加NumberState=1;
3.添加消息按钮afx_msg void OnNumberKey(UINT nID);
afx_msg void OnOperationKey(UINT nID);
4.在CalculatorDlg.cpp文件中BEGIN_MESSAGE_MAP(CMy1Dlg, CDialog)和END_MESSAGE_MAP()间添加代码
ON_COMMAND_RANGE(IDC_NUMBER1,IDC_NUMBER10,OnNumberKey)
ON_COMMAND_RANGE(IDC_NUMBER11,IDC_NUMBER20,OnOperationKey)
5.为成员函数OnNumberKey和OnOperationKey添加代码
void CCalculatorDlg::OnNumberKey(UINT nID)
{
int n=0;
switch(nID)
{
case IDC_NUMBER1:n=1;break;
case IDC_NUMBER2:n=2;break;
case IDC_NUMBER3:n=3;break;
case IDC_NUMBER4:n=4;break;
case IDC_NUMBER5:n=5;break;
case IDC_NUMBER6:n=6;break;
case IDC_NUMBER7:n=7;break;
case IDC_NUMBER8:n=8;break;
case IDC_NUMBER9:n=9;break;
case IDC_NUMBER10:n=0;break;
}
if(NumberState==1)
{
m_result=m_result*10+n;
number1=m_result;
UpdateData(FALSE); // 更新编辑框中的值
}
else
{
m_result=m_result*10+n;
number2=m_result;
UpdateData(FALSE);
}
}
void CCalculatorDlg::OnOperationKey(UINT nID)
{
switch(nID)
{
case IDC_NUMBER13: // "/"按钮
OperationState=1;
UpdateData(FALSE);
m_result=0;
NumberState=2;
break;
case IDC_NUMBER14: // "*"按钮
OperationState=2;
UpdateData(FALSE);
m_result=0;
NumberState=2;
break;
case IDC_NUMBER15: // "+"按钮
OperationState=3;
UpdateData(FALSE);
m_result=0;
NumberState=2;
break;
case IDC_NUMBER16: // "-"按钮
OperationState=4;
UpdateData(FALSE);
m_result=0;
NumberState=2;
break;
case IDC_NUMBER17: // "C"按钮,撤消用,不需要可以删除
number1=number2=m_result=0;
UpdateData(FALSE);
NumberState=1;
break;
case IDC_NUMBER20: // "="按钮
cal(); // 调用cal成员函数
break;
}
}
6.为成员函数cal()添加代码
void CCalculatorDlg::cal()
{
switch(OperationState)
{
case 1:
m_result=(double)number1/number2;
UpdateData(FALSE); // 更新编辑框中的结果
number1=m_result; // 把此次的运算结果作为下一次运算的第一个操作数
NumberState=2; // 下次输入的数作为第二个操作数
break;
case 2:
m_result=number1*number2;
UpdateData(FALSE); // 更新编辑框中的结果
number1=m_result;
NumberState=2;
break;
case 3:
m_result=number1+number2;
UpdateData(FALSE); // 更新编辑框中的结果
number1=m_result;
NumberState=2;
break;
case 4:
m_result=number1-number2;
UpdateData(FALSE); // 更新编辑框中的结果
number1=m_result;
NumberState=2;
break;
}
OperationState=0;
}
注意按钮的ID号要和程序中的ID号相同!!!!
MFC编写计算器具体步骤 方法 代码,拜托高手急用!
1、首先打开VS2013,新建一个VC++ MFC项目,命名为 gongzi。
2、然后在程序类型中,选择“基于对话框”,点击“完成”按钮。
3、然后在弹出的对话框中,删除原先的Static Text,添加相应的控件到窗体上。
4、接着在应发工资编辑框右击添加变量。 变量名称是 m_editTotal, 变量类型是 double, 类别为 value,点击“完成”。同样扣税费用编辑框右击添加变量。 变量名称是 m_editTax, 变量类型是 double, 类别为 value,应扣税款标签右击添加变量。
5、然后双击“计算应扣税款”进入代码,添加代码。
6、最后点击运行,输入对应的数目,按下按钮,查看执行效果。