MFC计算器主要代码(C++)
MFC计算器介绍
MFC计算器介绍部门: xxx时间: xxx整理范文,仅供参考,可下载自行编辑基于对话框实验,写一个计算器,实验结果:工具:VS2018遇到的问题:编译遇到字符串不能转换为char *,在字符串前面加_T解决了。
1.首先新建一个MFC工程,在工具框里面拖出工具,摆好布局,修改caption和ID栏2.在头文件添加CString num1。
CString num2。
CStringm_result。
boolisresult。
int witch。
在初始化函数里面初始化// TODO: 在此添加额外的初始化代码num1=""。
num2=""。
isresult=false。
witch=0。
3.数字键代码voidCcalculatorDlg::OnBnClickednum1(>{// TODO: 在此添加控件通知处理程序代码if(isresult==false>{num1+="1"。
m_result=num1。
UpdateData(false>。
}if(isresult==true>{num2+="1"。
m_result=num2。
UpdateData(false>。
}}运算符键代码:voidCcalculatorDlg::OnBnClickedjia(>{// TODO: 在此添加控件通知处理程序代码isresult=true。
witch=1。
}等于键代码:voidCcalculatorDlg::OnBnClickeddengyu(> {// TODO: 在此添加控件通知处理程序代码UpdateData(true>。
double number1=_ttof(num1>。
double number2=_ttof(num2>。
double result=0.0。
switch(witch>{ case 1: result=number1+number2。
MFC简单计算器编程步骤
1,准备环境:Microsoft Visual C++6.0
2,打开Microsoft Visual C++6.0,选择【文件】,点击新建,再出现的对话框中选择MFC AppWizard(exe),然后设置工程名和位置,如下图所示,点击确定
3,在出现的对话框中选择(基本对话框),点击完成,如下图所示
4,再出现的对话框中添加编辑框和按钮,并进行布局如下图
注:点击按钮右键属性,即可设置按钮上的数字和运算符显示
5,然后单击编辑框,接着按组合键Ctrl+W,再出现的对话框内选择【Member Variable】,出现下图所示
6,然后选项按钮【Add Variable...】,再出现的对话框内进行设置,如下图中所示,接着点击【OK】,接着点击确定,返回最初的对话框
7,然后切换到ClassViewj界面,双击CCalculatorDlg,在public中定义如下变量8,然后进行数字设置,如双击【1】按钮,添加如下代码
9,然后进行等号设置,双击【=】按钮,添加如下代码
10,进行运算符设置,如双击【+】,添加如下代码11,进行CE设置,双击【CE】,添加如下代码
12,最后一步,调试运行,结果如下
因为时间和水品有限,只能做到这里了,希望对有兴趣的人有一定帮助
版权归孤独邪剑所有,尽请分享,哈哈。
MFC计算器实现步骤和代码
MFC计算机程序步骤:1. 在进入VC++的第一个界面下,选择MFC AppWizard(exe),并设置工程名机器所在位置。
2. 在MFC AppWizard—step1 中选中Dialog based 选项,建立一个基于对话框的程序框架。
3. 使用资源编辑器建立对话框4. 编辑其中的各个控件的属性5. 插入菜单资源并编辑各项属性各属性为:Caption + * / ClearID ID_ADD_MENU ID_DIFFERENCE _MENU ID_MULTIPL Y_MENU ID_DEVIDE_MENU ID_CLEAR_MENUAbout ExitID_ABOUT_MENU ID_EXIT_MENU6. 添加代码使用Class Wizard 给编辑框连接变量(1)实现基本的加、减、乘、除的代码的添加。
(2)添加与菜单相关联的代码(3)填添加与滚动条相关联的代码在MFC AppWized(exe)项目下做。
界面自己做1.在对话框的头文件CalculatorDlg.h中添加#include<math.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实现计算器的源代码
DDX_Control(pDX, IDC_BUTTON2, m_button2);
// DDX_CBIndex(pDX, IDC_COMBO1, m_combo1);
// DDX_CBIndex(pDX, IDC_COMBO3, m_combo3);
char a[10];//定义字符数组
for (int i = 0; i < strData.GetLength(); i++)
{
a[i] = strData.GetAt(i);
}//将从窗口获取的数据放入数组中
double data;//定义一个double数据
data = atof(a);//将数组中CString转换为double
DDX_Control(pDX, IDC_COMBO4, m_combo4);
// DDX_Text(pDX, IDC_EDIT12, m_edit);
DDX_Text(pDX, IDC_EDIT12, m_edit12);
}
BEGIN_MESSAGE_MAP(Ctest2Dlg, CDialogEx)
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
//如果向对话框添加最小化按钮,则需要下面的代码
//来绘制该图标。对于使用文档/视图模型的MFC应用程序,
//这将由框架自动完成。
void Ctest2Dlg::OnPaint()
{
m_edit12="Yes";
MFC计算器代码
// CalculatorDemoDlg.cpp : implementation file// Download by #include "stdafx.h"#include "TestCalculatorDemo.h"#include "CalculatorDemoDlg.h"#include "math.h"#include "string"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif///////////////////////////////////////////////////////////////////////////// // CCalculatorDemoDlg dialogCCalculatorDemoDlg::CCalculatorDemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CCalculatorDemoDlg::IDD, pParent){//{{AFX_DATA_INIT(CCalculatorDemoDlg)// NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT}void CCalculatorDemoDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CCalculatorDemoDlg)// NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CCalculatorDemoDlg, CDialog)//{{AFX_MSG_MAP(CCalculatorDemoDlg)ON_BN_CLICKED(IDC_BTN_NO1, OnBtnNo1)ON_BN_CLICKED(IDC_BTN_NO2, OnBtnNo2)ON_BN_CLICKED(IDC_BTN_NO3, OnBtnNo3)ON_BN_CLICKED(IDC_BTN_NO4, OnBtnNo4)ON_BN_CLICKED(IDC_BTN_NO5, OnBtnNo5)ON_BN_CLICKED(IDC_BTN_NO6, OnBtnNo6)ON_BN_CLICKED(IDC_BTN_NO7, OnBtnNo7)ON_BN_CLICKED(IDC_BTN_NO8, OnBtnNo8)ON_BN_CLICKED(IDC_BTN_NO9, OnBtnNo9)ON_BN_CLICKED(IDC_BTN_NO0, OnBtnNo0)ON_BN_CLICKED(IDC_BTN_PORT, OnBtnPort)ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)ON_BN_CLICKED(IDC_BTN_SUB, OnBtnSub)ON_BN_CLICKED(IDC_BTN_MUL, OnBtnMul)ON_BN_CLICKED(IDC_BTN_DIV, OnBtnDiv)ON_BN_CLICKED(IDC_BTN_EQUEL, OnBtnEquel)ON_BN_CLICKED(IDC_BTN_PERCENT, OnBtnPercent)ON_BN_CLICKED(IDC_BTN_DOWN, OnBtnDown)ON_BN_CLICKED(IDC_BTN_NEQUA, OnBtnNequa)ON_BN_CLICKED(IDC_BTN_SPACE, OnBtnSpace)ON_BN_CLICKED(IDC_BTN_C, OnBtnC)ON_BN_CLICKED(IDC_BTN_SQRT, OnBtnSqrt)//}}AFX_MSG_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CCalculatorDemoDlg message handlersvoid CCalculatorDemoDlg::OnBtnNo1(){// TODO: Add your control notification handler code herePutIntoNum(1);}void CCalculatorDemoDlg::OnBtnNo2(){// TODO: Add your control notification handler code herePutIntoNum(2);}void CCalculatorDemoDlg::OnBtnNo3(){// TODO: Add your control notification handler code herePutIntoNum(3);}void CCalculatorDemoDlg::OnBtnNo4(){// TODO: Add your control notification handler code herePutIntoNum(4);}void CCalculatorDemoDlg::OnBtnNo5(){// TODO: Add your control notification handler code here PutIntoNum(5);}void CCalculatorDemoDlg::OnBtnNo6(){// TODO: Add your control notification handler code here PutIntoNum(6);}void CCalculatorDemoDlg::OnBtnNo7(){// TODO: Add your control notification handler code here PutIntoNum(7);}void CCalculatorDemoDlg::OnBtnNo8(){// TODO: Add your control notification handler code here PutIntoNum(8);}void CCalculatorDemoDlg::OnBtnNo9(){// TODO: Add your control notification handler code here PutIntoNum(9);}void CCalculatorDemoDlg::OnBtnNo0(){// TODO: Add your control notification handler code here // TODO: Add your control notification handler code here //如果第一次输入数字if (cs_Num[m_i]=="0"){return ;}cs_Num[m_i]+="0";SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]);if (!b_Hasport){CString temp;GetDlgItemText(IDC_EDIT_SHOW,temp);temp+=".";SetDlgItemText(IDC_EDIT_SHOW,temp);}}void CCalculatorDemoDlg::OnBtnPort(){//已有小数点时if (b_Hasport){return;}//无小数点cs_Num[m_i]+=".";b_Hasport=TRUE;b_Start=TRUE;SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]); }//四则运算符void CCalculatorDemoDlg::OnBtnAdd(){if (!b_Start){return;}if (en_LastSignl==en_none){en_LastSignl=en_add;}RunOperation(en_LastSignl);en_LastSignl=en_add;}void CCalculatorDemoDlg::OnBtnSub(){if (!b_Start){return;}// TODO: Add your control notification handler code here if (en_LastSignl==en_none){en_LastSignl=en_sub;}RunOperation(en_LastSignl);en_LastSignl=en_sub;}void CCalculatorDemoDlg::OnBtnMul(){if (!b_Start){return;}if (en_LastSignl==en_none){cs_Num[1]="1";en_LastSignl=en_mul;}RunOperation(en_LastSignl);en_LastSignl=en_mul;}void CCalculatorDemoDlg::OnBtnDiv(){if (!b_Start){return;}if (en_LastSignl==en_none){cs_Num[1]="1";en_LastSignl=en_div;}RunOperation(en_LastSignl);en_LastSignl=en_div;}void CCalculatorDemoDlg::OnBtnEquel(){// TODO: Add your control notification handler code hereRunOperation(en_LastSignl);//****特殊****en_LastSignl=en_none;b_Start=TRUE;}BOOL CCalculatorDemoDlg::OnInitDialog(){CDialog::OnInitDialog();// TODO: Add extra initialization hereInitAllDate();return TRUE; // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE }void CCalculatorDemoDlg::PutIntoNum(int n){CString tem_i;tem_i.Format("%d",n);CString temp;//第一次输入if (!b_Start){cs_Num[m_i]=tem_i;b_Start=TRUE;SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]);if (!b_Hasport){GetDlgItemText(IDC_EDIT_SHOW,temp);temp+=".";SetDlgItemText(IDC_EDIT_SHOW,temp);}return;}cs_Num[m_i]+=tem_i;SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]);if (!b_Hasport){GetDlgItemText(IDC_EDIT_SHOW,temp);temp+=".";SetDlgItemText(IDC_EDIT_SHOW,temp);}}void CCalculatorDemoDlg::InitAllDate(){en_LastSignl=en_none;b_Hasport=FALSE;b_Start=FALSE;b_Hasmul=FALSE;b_Hasdiv=FALSE;cs_Num[0]="0";cs_Num[1]="0";m_i=0;SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]+".");}void CCalculatorDemoDlg::RunOperation(eum_signl e_signl) {// TODO: Add your control notification handler code here double sum;double nAdd1;double nAdd2;nAdd1=atof(cs_Num[0]);nAdd2=atof(cs_Num[1]);//sum=nAdd1+nAdd2;switch(e_signl){case en_add:sum=nAdd1+nAdd2;break;case en_sub:sum=nAdd1-nAdd2;break;case en_mul:sum=nAdd1*nAdd2;break;case en_div:sum=nAdd1/nAdd2;break;}cs_Num[0].Format("%g",sum);SetDlgItemText(IDC_EDIT_SHOW,cs_Num[0]);if (!b_Hasport && -1==cs_Num[0].Find('.')){CString temp;GetDlgItemText(IDC_EDIT_SHOW,temp);temp+=".";SetDlgItemText(IDC_EDIT_SHOW,temp);}m_i = 1;b_Hasport=FALSE;b_Start=FALSE;cs_Num[m_i]="0";}//void CCalculatorDemoDlg::RunOperation(eum_signl en_signl) //{//}void CCalculatorDemoDlg::OnBtnC(){// TODO: Add your control notification handler code here InitAllDate();}// 根号void CCalculatorDemoDlg::OnBtnSqrt(){// TODO: Add your control notification handler code here if (!b_Start){return;}double num;CString temp;GetDlgItemText(IDC_EDIT_SHOW,temp);num=atof(temp);num=sqrt(num);temp.Format("%lf",num);if (temp.Find('.')==-1)SetDlgItemText(IDC_EDIT_SHOW,temp+".");elseSetDlgItemText(IDC_EDIT_SHOW,temp);}// %void CCalculatorDemoDlg::OnBtnPercent(){// TODO: Add your control notification handler code here}// 1/xvoid CCalculatorDemoDlg::OnBtnDown(){// TODO: Add your control notification handler code hereif (!b_Start){return;}double num;CString temp;GetDlgItemText(IDC_EDIT_SHOW,temp);num=atof(temp);num=1/num;temp.Format("%lf",num);if (temp.Find('.')==-1)SetDlgItemText(IDC_EDIT_SHOW,temp+".");elseSetDlgItemText(IDC_EDIT_SHOW,temp);}// +-void CCalculatorDemoDlg::OnBtnNequa(){// TODO: Add your control notification handler code hereif (!b_Start){return;}double num;CString temp;GetDlgItemText(IDC_EDIT_SHOW,temp);num=atof(temp);num=0-num;SetDlgItemText(IDC_EDIT_SHOW , b_Hasport ? temp:temp+".");// 退格键void CCalculatorDemoDlg::OnBtnSpace(){// TODO: Add your control notification handler code hereif (!b_Start){return;}double num;CString temp;int len;CHAR temp_ch[32];GetDlgItemText(IDC_EDIT_SHOW,temp);//AfxMessageBox(temp);num=atof(temp);sprintf(temp_ch,"%g",num);num=0;//AfxMessageBox(temp_ch);len=strlen(temp_ch);int te_i=len-1;if(temp_ch[te_i]=='.')te_i-=1,b_Hasport=FALSE;temp_ch[te_i]='\0';////////////////////////////////////////////////////////////////////////// //AfxMessageBox(temp_ch);if (strlen(temp_ch)==0){temp_ch[0]='0';temp_ch[1]='\0';}////////////////////////////////////////////////////////////////////////// sscanf(temp_ch,"%lf",&num);temp.Format("%g",num);//AfxMessageBox(temp);SetDlgItemText(IDC_EDIT_SHOW,b_Hasport?temp:temp+".");}。
MFC计算器代码
///D:/daima/calc/calcdlg.cpp// calcdlg.cpp : implementation file//// This is a part of the Microsoft Foundation Classes C++ library.// Copyright (C) 1992-1998 Microsoft Corporation// All rights reserved.//// This source code is only intended as a supplement to the// Microsoft Foundation Classes Reference and related// electronic documentation provided with the library.// See these sources for detailed information regarding the// Microsoft Foundation Classes product.#include "stdafx.h"#include "mfccalc.h"#include "calcdlg.h"#ifdef _DEBUG#undef THIS_FILEstatic char BASED_CODE THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// Implementationprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //{{AFX_MSG(CAboutDlg)virtual BOOL OnInitDialog();//}}AFX_MSGDECLARE_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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CAboutDlg message handlersBOOL CAboutDlg::OnInitDialog(){CDialog::OnInitDialog();// TODO: Add extra initialization herereturn TRUE;}/////////////////////////////////////////////////////////////////////////////// CCalcDlg dialogIMPLEMENT_DYNCREATE(CCalcDlg, CDialog)BEGIN_DISPATCH_MAP(CCalcDlg, CDialog)//{{AFX_DISPA TCH_MAP(CCalcDlg)DISP_PROPERTY_EX(CCalcDlg, "Accum", GetAccum, SetAccum, VT_I4)DISP_PROPERTY_EX(CCalcDlg, "Operand", GetOperand, SetOperand, VT_I4)DISP_PROPERTY_EX(CCalcDlg, "Operation", GetOperation, SetOperation, VT_I2) DISP_PROPERTY_EX(CCalcDlg, "Visible", GetVisible, SetVisible, VT_BOOL) DISP_FUNCTION(CCalcDlg, "Evaluate", Evaluate, VT_BOOL, VTS_NONE)DISP_FUNCTION(CCalcDlg, "Clear", Clear, VT_EMPTY, VTS_NONE)DISP_FUNCTION(CCalcDlg, "Display", Display, VT_EMPTY, VTS_NONE)DISP_FUNCTION(CCalcDlg, "Close", Close, VT_EMPTY, VTS_NONE)DISP_FUNCTION(CCalcDlg, "Button", Button, VT_BOOL, VTS_BSTR)//}}AFX_DISPA TCH_MAPEND_DISPATCH_MAP()#ifndef IMPLEMENT_OLECREATE_SINGLE// MFC will provide this macro in the future. For now, we define it.#define IMPLEMENT_OLECREATE_SINGLE(class_name, external_name, \ l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \AFX_DATADEF COleObjectFactory class_name::factory(class_name::guid, \ RUNTIME_CLASS(class_name), TRUE, _T(external_name)); \ const AFX_DATADEF GUID class_name::guid = \{ l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } };#endif// {62C4DD10-F45E-11cd-8C3D-00AA004BB3B7}IMPLEMENT_OLECREATE_SINGLE(CCalcDlg, "mfccalc.calculator",0x62c4dd10, 0xf45e, 0x11cd, 0x8c, 0x3d, 0x0, 0xaa, 0x0, 0x4b, 0xb3, 0xb7); CCalcDlg::CCalcDlg(CWnd* pParent /*=NULL*/): CDialog(CCalcDlg::IDD, pParent){m_bAutoDelete = TRUE; // default to auto-deletem_dwRegister = 0; // not registered as active by default//{{AFX_DATA_INIT(CCalcDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);// Note that LoadAccelerator does not require DestroyAcceleratorTablem_hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD));// clear the contents of the calculator and reset stateOnClickedClear();// enable this object for OLE automationEnableAutomation();}CCalcDlg::~CCalcDlg(){if (m_dwRegister != 0)RevokeActiveObject(m_dwRegister, NULL);}void CCalcDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CCalcDlg)// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP}/////////////////////////////////////////////////////////////////////////////// CCalcDlg implementationvoid CCalcDlg::PerformOperation(){if (m_errorState != ErrNone)return;if (m_bOperandAvail){if (m_operator == OpNone)m_accum = m_operand;else if (m_operator == OpMultiply)m_accum *= m_operand;else if (m_operator == OpDivide){if (m_operand == 0)m_errorState = ErrDivideByZero;elsem_accum /= m_operand;}else if (m_operator == OpAdd)m_accum += m_operand;else if (m_operator == OpSubtract)m_accum -= m_operand;}m_bOperandAvail = FALSE;UpdateDisplay();}void CCalcDlg::ClickedNumber(long l){if (m_errorState != ErrNone)return;if (!m_bOperandAvail)m_operand = 0L;SetOperand(m_operand*10+l);UpdateDisplay();}void CCalcDlg::UpdateDisplay(){if (GetSafeHwnd() == NULL)return;CString str;if (m_errorState != ErrNone)str.LoadString(IDS_ERROR);else{long lVal = (m_bOperandAvail) ? m_operand : m_accum;str.Format(_T("%ld"), lVal);}GetDlgItem(IDE_ACCUM)->SetWindowText(str);GetDlgItem(IDC_INVISIBLE_FOCUS)->SetFocus();}BEGIN_MESSAGE_MAP(CCalcDlg, CDialog)//{{AFX_MSG_MAP(CCalcDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_COMMAND_RANGE(IDB_0, IDB_9, OnClickedNumber) ON_BN_CLICKED(IDB_CLEAR, OnClickedClear)ON_BN_CLICKED(IDB_DIVIDE, OnClickedDivide)ON_BN_CLICKED(IDB_EQUAL, OnClickedEqual)ON_BN_CLICKED(IDB_MINUS, OnClickedMinus)ON_BN_CLICKED(IDB_PLUS, OnClickedPlus)ON_BN_CLICKED(IDB_TIMES, OnClickedTimes)ON_EN_SETFOCUS(IDE_ACCUM, OnSetFocusAccum)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CCalcDlg message handlersBOOL CCalcDlg::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);CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}pSysMenu->RemoveMenu(SC_MAXIMIZE, MF_BYCOMMAND);pSysMenu->RemoveMenu(SC_SIZE, MF_BYCOMMAND);// want focus to stay on the dialog itself (until a button is clicked)SetFocus();return FALSE;}void CCalcDlg::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 CCalcDlg::OnPaint(){if (!IsIconic()){CDialog::OnPaint();return;}CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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 icondc.DrawIcon(x, y, m_hIcon);}// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSOR CCalcDlg::OnQueryDragIcon(){return (HCURSOR)m_hIcon;}void CCalcDlg::OnClickedNumber(UINT nID){ASSERT(nID >= IDB_0 && nID <= IDB_9);ClickedNumber(nID - IDB_0);}void CCalcDlg::OnClickedClear(){m_operator = OpNone;m_operand = 0L;m_accum = 0L;m_bOperandAvail = FALSE;m_errorState = ErrNone;UpdateDisplay();}void CCalcDlg::OnClickedDivide(){PerformOperation();m_operator = OpDivide;}void CCalcDlg::OnClickedEqual(){PerformOperation();m_operator = OpNone;}void CCalcDlg::OnClickedMinus(){PerformOperation();m_operator = OpSubtract;}void CCalcDlg::OnClickedPlus(){PerformOperation();m_operator = OpAdd;}void CCalcDlg::OnClickedTimes(){PerformOperation();m_operator = OpMultiply;}BOOL CCalcDlg::PreTranslateMessage(MSG* pMsg){if (m_hAccel != NULL && TranslateAccelerator(m_hWnd, m_hAccel, pMsg)) return TRUE;return CDialog::PreTranslateMessage(pMsg);}void CCalcDlg::PostNcDestroy(){if (m_bAutoDelete)delete this;}void CCalcDlg::OnCancel(){DestroyWindow();}void CCalcDlg::OnOK(){}void CCalcDlg::OnSetFocusAccum(){GetDlgItem(IDC_INVISIBLE_FOCUS)->SetFocus();}/////////////////////////////////////////////////////////////////////////////// CCalcDlg automationBOOL CCalcDlg::RegisterActive(){// attempt to register as the active object for the CCalcDlg CLSID return RegisterActiveObject(GetInterface(&IID_IUnknown), CCalcDlg::guid, NULL, &m_dwRegister) == NOERROR; }long CCalcDlg::GetAccum(){return m_accum;}void CCalcDlg::SetAccum(long nNewValue){m_accum = nNewValue;}long CCalcDlg::GetOperand(){return m_operand;}void CCalcDlg::SetOperand(long nNewValue){m_operand = nNewValue;m_bOperandAvail = TRUE;}short CCalcDlg::GetOperation(){return m_operator;}void CCalcDlg::SetOperation(short nNewValue){m_operator = (Operator)nNewValue;}BOOL CCalcDlg::GetVisible(){return m_hWnd != NULL && (GetStyle() & WS_VISIBLE) != 0; }void CCalcDlg::SetVisible(BOOL bNewValue){if (bNewValue == GetVisible())return;if (bNewValue){// create it if necessaryif (m_hWnd == NULL && !Create(CCalcDlg::IDD)) return;// set up as the active window for the applicationif (AfxGetThread()->m_pMainWnd == NULL)AfxGetThread()->m_pMainWnd = this;// show itShowWindow(SW_SHOWNORMAL);}else{if (m_hWnd != NULL)ShowWindow(SW_HIDE);}}BOOL CCalcDlg::Evaluate(){OnClickedEqual();return m_errorState == ErrNone;}void CCalcDlg::Clear(){OnClickedClear();}void CCalcDlg::Display(){UpdateDisplay();}void CCalcDlg::Close(){if (m_hWnd == NULL){AfxPostQuitMessage(0);return;}BOOL bAutoDelete = m_bAutoDelete;m_bAutoDelete = FALSE;DestroyWindow();m_bAutoDelete = bAutoDelete;}BOOL CCalcDlg::Button(LPCTSTR szButton){switch (szButton[0]){case 'c':case 'C':OnClickedClear();break;case '/':OnClickedDivide();break;case '+':OnClickedPlus();break;case '-':OnClickedMinus();break;case '*':OnClickedTimes();break;case '=':OnClickedEqual();break;default:if (szButton[0] >= '0' && szButton[0] <= '9')ClickedNumber(szButton[0] - '0');elsereturn FALSE;break;}return TRUE;}///D:/daima/calc/calcdlg.h// calcdlg.h : header file//// This is a part of the Microsoft Foundation Classes C++ library.// Copyright (C) 1992-1998 Microsoft Corporation// All rights reserved.//// This source code is only intended as a supplement to the// Microsoft Foundation Classes Reference and related// electronic documentation provided with the library.// See these sources for detailed information regarding the// Microsoft Foundation Classes product./////////////////////////////////////////////////////////////////////////////// CCalcDlg dialogenum Operator { OpNone, OpAdd, OpSubtract, OpMultiply, OpDivide }; enum CalcError { ErrNone, ErrDivideByZero };class CCalcDlg : public CDialog{// Constructionpublic:CCalcDlg(CWnd* pParent = NULL); // standard constructor// OperationsBOOL RegisterActive();// Dialog Data//{{AFX_DATA(CCalcDlg)enum { IDD = IDD_MFCCALC_DIALOG };// NOTE: the ClassWizard will add data members here//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CCalcDlg)public:virtual BOOL PreTranslateMessage(MSG* pMsg);protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support virtual void PostNcDestroy();//}}AFX_VIRTUAL// Implementationprotected:virtual ~CCalcDlg();HICON m_hIcon;HACCEL m_hAccel;BOOL m_bAutoDelete; // delete in PostNcDestroyDWORD m_dwRegister; // active registration magic cookie// calculator statelong m_accum;long m_operand;Operator m_operator;CalcError m_errorState;BOOL m_bOperandAvail;// helper functionsvoid PerformOperation();void ClickedNumber(long lNum);void UpdateDisplay();public:// Generated message map functions//{{AFX_MSG(CCalcDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnClickedNumber(UINT nID);afx_msg void OnClickedClear();afx_msg void OnClickedDivide();afx_msg void OnClickedEqual();afx_msg void OnClickedMinus();afx_msg void OnClickedPlus();afx_msg void OnClickedTimes();virtual void OnCancel();virtual void OnOK();afx_msg void OnSetFocusAccum();//}}AFX_MSGDECLARE_MESSAGE_MAP()// to be OLE creatable, it must be DYNCREATE and OLECREATE DECLARE_DYNCREATE(CCalcDlg)DECLARE_OLECREA TE(CCalcDlg)public:// Generated OLE dispatch map functions//{{AFX_DISPA TCH(CCalcDlg)afx_msg long GetAccum();afx_msg void SetAccum(long nNewValue);afx_msg long GetOperand();afx_msg void SetOperand(long nNewValue);afx_msg short GetOperation();afx_msg void SetOperation(short nNewValue);afx_msg BOOL GetVisible();afx_msg void SetVisible(BOOL bNewValue);afx_msg BOOL Evaluate();afx_msg void Clear();afx_msg void Display();afx_msg void Close();afx_msg BOOL Button(LPCTSTR szButton);//}}AFX_DISPA TCHDECLARE_DISPATCH_MAP()};///D:/daima/calc/mfccalc.cpp// mfccalc.cpp : Defines the class behaviors for the application.//// This is a part of the Microsoft Foundation Classes C++ library.// Copyright (C) 1992-1998 Microsoft Corporation// All rights reserved.//// This source code is only intended as a supplement to the// Microsoft Foundation Classes Reference and related// electronic documentation provided with the library.// See these sources for detailed information regarding the// Microsoft Foundation Classes product.#include "stdafx.h"#include "mfccalc.h"#include "calcdlg.h"#ifdef _DEBUG#undef THIS_FILEstatic char BASED_CODE THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CCalcAppBEGIN_MESSAGE_MAP(CCalcApp, CWinApp)//{{AFX_MSG_MAP(CCalcApp)// NOTE - the ClassWizard will add and remove mapping macros here.// DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSGON_COMMAND(ID_HELP, CWinApp::OnHelp)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CCalcApp constructionCCalcApp::CCalcApp(){// TODO: add construction code here,// Place all significant initialization in InitInstance}/////////////////////////////////////////////////////////////////////////////// The one and only CCalcApp objectCCalcApp theApp;/////////////////////////////////////////////////////////////////////////////// CCalcApp initializationBOOL CCalcApp::InitInstance(){// Standard initialization// If you are not using these features and wish to reduce the size// of your final executable, you should remove from the following// the specific initialization routines you do not need.Enable3dControls();// Initialize OLE 2.0 librariesif (!AfxOleInit()){AfxMessageBox(IDP_OLE_INIT_FAILED);return FALSE;}// Parse the command line to see if launched as OLE serverif (RunEmbedded() || RunAutomated()){// Register all OLE server (factories) as running. This enables the// OLE 2.0 libraries to create objects from other applications.COleTemplateServer::RegisterAll();// Do not continue with rest of initialization. Wait for// client to create a CCalcDlg object instead.return TRUE;}// When a server application is launched stand-alone, it is a good idea // to update the system registry in case it has been damaged.COleObjectFactory::UpdateRegistryAll();// create a modeless dialog as the main window of the applicationCCalcDlg* pDlg = new CCalcDlg;pDlg->SetVisible(TRUE);if (!pDlg->GetVisible()){AfxMessageBox(IDP_UNABLE_TO_SHOW_CALC);return FALSE;}pDlg->RegisterActive();// We are using a modeless dialog so we can translate accelerator keys // against the dialog. In order to run the main message pump,// InitInstance must return TRUE even though this application is// a dialog-based application.return TRUE;}///D:/daima/calc/mfccalc.h// mfccalc.h : main header file for the MFCCALC application//// This is a part of the Microsoft Foundation Classes C++ library.// Copyright (C) 1992-1998 Microsoft Corporation// All rights reserved.//// This source code is only intended as a supplement to the// Microsoft Foundation Classes Reference and related// electronic documentation provided with the library.// See these sources for detailed information regarding the// Microsoft Foundation Classes product.#ifndef __AFXWIN_H__#error include 'stdafx.h' before including this file for PCH#endif#include "resource.h" // main symbols/////////////////////////////////////////////////////////////////////////////// CCalcApp:// See mfccalc.cpp for the implementation of this class//class CCalcApp : public CWinApp{public:CCalcApp();// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CCalcApp)public:virtual BOOL InitInstance();//}}AFX_VIRTUAL// Implementation//{{AFX_MSG(CCalcApp)// NOTE - the ClassWizard will add and remove member functions here.// DO NOT EDIT what you see in these blocks of generated code !//}}AFX_MSGDECLARE_MESSAGE_MAP()};////////////////////////////////////////////////////////////////////////////////D:/daima/calc/mfccalc.odl// mfccalc.odl : type library source for mfccalc.exe// This file will be processed by the Make Type Library (mktyplib) tool to// produce the type library (mfccalc.tlb).[ uuid(5627AF00-F44C-11CD-8C3D-00AA004BB3B7), version(1.0) ]library mfccalc{importlib("stdole32.tlb");// Primary dispatch interface for CCalcDlg[ uuid(990BA900-F45E-11cd-8C3D-00AA004BB3B7) ]dispinterface ICalcDlg{properties:// NOTE - ClassWizard will maintain property information here.// Use extreme caution when editing this section.//{{AFX_ODL_PROP(CCalcDlg)[id(1)] long Accum;[id(2)] long Operand;[id(3)] short Operation;[id(4)] boolean Visible;//}}AFX_ODL_PROPmethods:// NOTE - ClassWizard will maintain method information here.// Use extreme caution when editing this section.//{{AFX_ODL_METHOD(CCalcDlg)[id(5)] boolean Evaluate();[id(6)] void Clear();[id(7)] void Display();[id(8)] void Close();[id(9)] boolean Button(BSTR szButton);//}}AFX_ODL_METHOD};// Class information for CCCalcDlg[ uuid(62C4DD10-F45E-11cd-8C3D-00AA004BB3B7) ]coclass CCCalcDlg{[default] dispinterface ICalcDlg;};//{{AFX_APPEND_ODL}}};///D:/daima/calc/mfccalc.rc//Microsoft Developer Studio generated resource script.//#include "resource.h"#define APSTUDIO_READONL Y_SYMBOLS///////////////////////////////////////////////////////////////////////////////// Generated from the TEXTINCLUDE 2 resource.//#include "afxres.h"/////////////////////////////////////////////////////////////////////////////#undef APSTUDIO_READONL Y_SYMBOLS/////////////////////////////////////////////////////////////////////////////// English (U.S.) resources#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)#ifdef _WIN32LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US#pragma code_page(1252)#endif //_WIN32#ifdef APSTUDIO_INVOKED///////////////////////////////////////////////////////////////////////////////// TEXTINCLUDE//1 TEXTINCLUDE DISCARDABLEBEGIN"resource.h\0"END2 TEXTINCLUDE DISCARDABLEBEGIN"#include ""afxres.h""\r\n""\0"END3 TEXTINCLUDE DISCARDABLEBEGIN"#include ""res\\mfccalc.rc2"" // non-Microsoft Visual C++ edited resources\r\n""\r\n""#define _AFX_NO_SPLITTER_RESOURCES\r\n""#define _AFX_NO_OLE_RESOURCES\r\n""#define _AFX_NO_TRACKER_RESOURCES\r\n""#define _AFX_NO_PROPERTY_RESOURCES\r\n""#include ""afxres.rc"" // Standard components\r\n""\0"END#endif // APSTUDIO_INVOKED///////////////////////////////////////////////////////////////////////////////// Icon//IDR_MAINFRAME ICON DISCARDABLE "res\\mfccalc.ico" ///////////////////////////////////////////////////////////////////////////////// Dialog//IDD_ABOUTBOX DIALOG DISCARDABLE 34, 22, 217, 55STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "About mfccalc"FONT 8, "MS Sans Serif"BEGINICON IDR_MAINFRAME,IDC_STA TIC,11,17,18,20LTEXT "MFC/OLE Calc Version 1.0",IDC_STA TIC,40,10,119,8LTEXT "Copyright ?1994 - 1998 Microsoft Corporation",IDC_STATIC,40,25,167,8DEFPUSHBUTTON "OK",IDOK,176,6,32,14,WS_GROUPLTEXT "All Rights Reserved",IDC_STA TIC,40,36,148,8ENDIDD_MFCCALC_DIALOG DIALOG DISCARDABLE 1, 1, 93, 114STYLE DS_MODALFRAME | WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU CAPTION "MFC OLE Calc"FONT 8, "MS Sans Serif"BEGINPUSHBUTTON "0",IDB_0,9,90,15,15,NOT WS_TABSTOPPUSHBUTTON "1",IDB_1,9,70,15,15,NOT WS_TABSTOPPUSHBUTTON "2",IDB_2,29,70,15,15,NOT WS_TABSTOPPUSHBUTTON "3",IDB_3,49,70,15,15,NOT WS_TABSTOPPUSHBUTTON "4",IDB_4,9,50,15,15,NOT WS_TABSTOPPUSHBUTTON "5",IDB_5,29,50,15,15,NOT WS_TABSTOPPUSHBUTTON "6",IDB_6,49,50,15,15,NOT WS_TABSTOPPUSHBUTTON "7",IDB_7,9,30,15,15,NOT WS_TABSTOPPUSHBUTTON "8",IDB_8,29,30,15,15,NOT WS_TABSTOPPUSHBUTTON "9",IDB_9,49,30,15,15,NOT WS_TABSTOPPUSHBUTTON "=",IDB_EQUAL,49,90,15,15,NOT WS_TABSTOPPUSHBUTTON "+",IDB_PLUS,69,30,15,15,NOT WS_TABSTOPPUSHBUTTON "-",IDB_MINUS,69,50,15,15,NOT WS_TABSTOPPUSHBUTTON "*",IDB_TIMES,69,70,15,15,NOT WS_TABSTOPPUSHBUTTON "/",IDB_DIVIDE,69,90,15,15,NOT WS_TABSTOPPUSHBUTTON "C",IDB_CLEAR,29,90,15,15,NOT WS_TABSTOPEDITTEXT IDE_ACCUM,8,6,76,15,ES_RIGHT | ES_MULTILINE |ES_AUTOHSCROLL | ES_READONL Y | NOT WS_TABSTOP LTEXT "",IDC_INVISIBLE_FOCUS,8,106,77,8,WS_TABSTOPEND///////////////////////////////////////////////////////////////////////////////// Version//VS_VERSION_INFO VERSIONINFOFILEVERSION 1,0,0,1PRODUCTVERSION 1,0,0,1FILEFLAGSMASK 0x3fL#ifdef _DEBUGFILEFLAGS 0x1L#elseFILEFLAGS 0x0L#endifFILEOS 0x4LFILETYPE 0x1LFILESUBTYPE 0x0LBEGINBLOCK "StringFileInfo"BEGINBLOCK "040904b0"BEGINV ALUE "CompanyName", "\0"V ALUE "FileDescription", "MFCCALC MFC Application\0"V ALUE "FileVersion", "1, 0, 0, 1\0"V ALUE "InternalName", "MFCCALC\0"V ALUE "LegalCopyright", "Copyright ?1994 - 1998\0"V ALUE "OriginalFilename", "MFCCALC.EXE\0"V ALUE "ProductName", "MFCCALC Application\0"V ALUE "ProductV ersion", "1, 0, 0, 1\0"ENDENDBLOCK "VarFileInfo"BEGINV ALUE "Translation", 0x409, 1200ENDEND///////////////////////////////////////////////////////////////////////////////// Accelerator//IDD_MFCCALC_DIALOG ACCELERATORS DISCARDABLEBEGIN"*", IDB_TIMES, ASCII, NOINVERT"+", IDB_PLUS, ASCII, NOINVERT"-", IDB_MINUS, ASCII, NOINVERT"/", IDB_DIVIDE, ASCII, NOINVERT"0", IDB_0, ASCII, NOINVERT"1", IDB_1, ASCII, NOINVERT"2", IDB_2, ASCII, NOINVERT"3", IDB_3, ASCII, NOINVERT"4", IDB_4, ASCII, NOINVERT"5", IDB_5, ASCII, NOINVERT"6", IDB_6, ASCII, NOINVERT"7", IDB_7, ASCII, NOINVERT"8", IDB_8, ASCII, NOINVERT"9", IDB_9, ASCII, NOINVERT"=", IDB_EQUAL, ASCII, NOINVERT"C", IDB_CLEAR, ASCII, NOINVERTVK_LEFT, ID_NOTHING, VIRTKEY, NOINVERT VK_RIGHT, ID_NOTHING, VIRTKEY, NOINVERT VK_UP, ID_NOTHING, VIRTKEY, NOINVERT VK_DOWN, ID_NOTHING, VIRTKEY, NOINVERT VK_RETURN, IDB_EQUAL, VIRTKEY, NOINVERT "c", IDB_CLEAR, ASCII, NOINVERTEND///////////////////////////////////////////////////////////////////////////////// String Table//STRINGTABLE DISCARDABLEBEGINID_INDICATOR_EXT "EXT"ID_INDICATOR_CAPS "CAP"ID_INDICATOR_NUM "NUM"ID_INDICATOR_SCRL "SCRL"ID_INDICATOR_OVR "OVR"ID_INDICATOR_REC "REC"ENDSTRINGTABLE DISCARDABLEBEGINIDP_OLE_INIT_FAILED "Failed to initialized the OLE libraries!"IDS_ABOUTBOX "&About MFC/OLE Calc..."IDS_ERROR "ERROR!"IDP_UNABLE_TO_SHOW_CALC "Unable to create and show the calculator window!" ENDSTRINGTABLE DISCARDABLEBEGINAFX_IDS_APP_TITLE "MFC/OLE Calc"END#endif // English (U.S.) resources/////////////////////////////////////////////////////////////////////////////#ifndef APSTUDIO_INVOKED///////////////////////////////////////////////////////////////////////////////// Generated from the TEXTINCLUDE 3 resource.//#include "res\mfccalc.rc2" // non-Microsoft Visual C++ edited resources#define _AFX_NO_SPLITTER_RESOURCES#define _AFX_NO_OLE_RESOURCES#define _AFX_NO_TRACKER_RESOURCES#define _AFX_NO_PROPERTY_RESOURCES#include "afxres.rc" // Standard components/////////////////////////////////////////////////////////////////////////////#endif // not APSTUDIO_INVOKED///D:/daima/calc/mfccalc.regREGEDIT; This .REG file may be used by your SETUP program.。
visual 2015 mfc 计算器 代码
以下是一个简单的 Visual Studio 2015 MFC 计算器的示例代码。
它包括两个窗口,一个用于输入和显示数字,另一个用于显示结果。
```cpp// MainFrm.cpp : implementation of the CMainFrame class //#include "pch.h"#include "framework.h"#include "Calculator.h"#include "MainFrm.h"#include "afxdialogex.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CMainFrameIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)ON_WM_CREATE()ON_WM_DESTROY()ON_COMMAND(ID_APP_ABOUT,&CMainFrame::OnAppAbout)ON_COMMAND(ID_FILE_NEW, &CMainFrame::OnFileNew) ON_COMMAND(ID_FILE_EXIT, &CMainFrame::OnFileExit) ON_COMMAND(ID_EDIT_CLEAR,&CMainFrame::OnEditClear)ON_COMMAND(ID_EDIT_COPY,&CMainFrame::OnEditCopy)ON_COMMAND(ID_EDIT_PASTE,&CMainFrame::OnEditPaste)ON_COMMAND(ID_VIEW_SPLIT,&CMainFrame::OnViewSplit)ON_COMMAND(ID_VIEW_FULL,&CMainFrame::OnViewFull)ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR,&CMainFrame::OnUpdateEditClear)ON_UPDATE_COMMAND_UI(ID_EDIT_COPY,&CMainFrame::OnUpdateEditCopy)ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE,&CMainFrame::OnUpdateEditPaste)END_MESSAGE_MAP()static UINT indicators[] ={ID_SEPARATOR, // status line indicatorID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,};CMainFrame::CMainFrame() : CFrameWndEx(){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}CMainFrame::~CMainFrame(){}int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)return -1;// Create a view to host the client window. This object will be responsible for drawing the client area.// Set initial host view properties.CView* pView = new CView;if (!pView)return -1;pView->Create(_T("Calculator"), WS_CHILD | WS_VISIBLE | WS_BORDER | 500, CRect(0, 0, 200, 200), this);m_viewList.AddTail(pView);// m_pActiveView = pView;// m_bIsSplitterBar = TRUE;// m_bIsHorzSplit = TRUE;// m_bIsVerSplit = FALSE;// m_bIsDocTemplate = TRUE;// Create a toolbar control and initialize it.CToolBarCtrl mtbBarCtrl;// Create a command bar control and initialize it.CCommandBarCtrl commandBarCtrl; // Create a status bar control and initialize it. // Create a menu control and initialize it. CMenu menuBar;menuBar.CreateMenu();menuBar.AppendMenu(MF_POPUP, IDM_FILE, _T("&File"));menuBar.AppendMenu(MF_POPUP, IDM_EDIT, _T("&Edit")); menuBar.AppendMenu(MF_POPUP,IDM_VIEW, _T("&View")); menuBar.AppendMenu(MF_POPUP, IDM_INSERT, _T("&Insert")); menuBar.AppendMenu(MF_POPUP, IDM_FORMAT, _T("&Format")); menuBar.AppendMenu。
简易计算器MFC程序
1、编写一个对话框应用程序,添加三按钮,三文本编辑框,实现加、减、乘、除简单运算,并实现编辑框的文本复制、粘贴、清空;程序代码:实验4View.cppvoid C实验4View::OnModelDialog(){CMyDlg dlg;dlg.DoModal();}void C实验4View::OnModellessDialog(){CMyDlg *pDlg=new CMyDlg;pDlg->Create(IDD_DIALOG1,this);pDlg->ShowWindow(SW_SHOW);}MyDlg.hpublic:CEdit m_edit1;CEdit m_edit2;CEdit m_edit3;CEdit m_edit4;CEdit m_edit5;afx_msg void OnBnClickedAdd();afx_msg void OnBnClickedSubtact();afx_msg void OnBnClickedMultiply();afx_msg void OnBnClickedDivide();afx_msg void OnBnClickedClearwrite();afx_msg void OnBnClickedClearsave();afx_msg void OnBnClickedCopy();afx_msg void OnBnClickedPaste();afx_msg void OnEnSetfocusEdit4();afx_msg void OnEnSetfocusEdit5();};MyDlg.cppint nEDITtemp;//全局变量BEGIN_MESSAGE_MAP(CMyDlg, CDialog)ON_BN_CLICKED(IDM_ADD, &CMyDlg::OnBnClickedAdd)ON_BN_CLICKED(IDM_SUBTACT, &CMyDlg::OnBnClickedSubtact)ON_BN_CLICKED(IDM_MULTIPLY, &CMyDlg::OnBnClickedMultiply)ON_BN_CLICKED(IDM_DIVIDE, &CMyDlg::OnBnClickedDivide)ON_BN_CLICKED(IDM_CLEARWRITE, &CMyDlg::OnBnClickedClearwrite) ON_BN_CLICKED(IDM_CLEARSAVE, &CMyDlg::OnBnClickedClearsave) ON_BN_CLICKED(IDM_COPY, &CMyDlg::OnBnClickedCopy)ON_BN_CLICKED(IIDM_PASTE, &CMyDlg::OnBnClickedPaste)ON_EN_SETFOCUS(IDC_EDIT4, &CMyDlg::OnEnSetfocusEdit4)ON_EN_SETFOCUS(IDC_EDIT5, &CMyDlg::OnEnSetfocusEdit5)END_MESSAGE_MAP()// CMyDlg 消息处理程序void CMyDlg::OnBnClickedAdd()//加法{// TODO: 在此添加控件通知处理程序代码double num1, num2, num3;int i=0;CString ch1, ch2, ch3;m_edit1.GetWindowText(ch1);m_edit2.GetWindowText(ch2);num1 =_tstof(ch1);num2 =_tstof(ch2);num3 = num1 + num2;ch3.Format(_T("%g"), num3);m_edit3.SetWindowText(ch3);}void CMyDlg::OnBnClickedSubtact()//减法{// TODO: 在此添加控件通知处理程序代码double num1, num2, num3;CString ch1, ch2, ch3;m_edit1.GetWindowText(ch1);m_edit2.GetWindowText(ch2);num1 = _tstof(ch1);num2 = _tstof(ch2);num3 = num1 - num2;ch3.Format(_T("%g"), num3);m_edit3.SetWindowText(ch3);}void CMyDlg::OnBnClickedMultiply()//乘法// TODO: 在此添加控件通知处理程序代码double num1, num2, num3;CString ch1, ch2, ch3;m_edit1.GetWindowText(ch1);m_edit2.GetWindowText(ch2);num1 = _tstof(ch1);num2 = _tstof(ch2);num3 = num1 * num2;ch3.Format(_T("%g"), num3);m_edit3.SetWindowText(ch3);}void CMyDlg::OnBnClickedDivide()//除法{// TODO: 在此添加控件通知处理程序代码double num1, num2, num3;CString ch1, ch2, ch3;m_edit1.GetWindowText(ch1);m_edit2.GetWindowText(ch2);num1 = _tstof(ch1);num2 = _tstof(ch2);num3 = num1 / num2;ch3.Format(_T("%g"), num3);m_edit3.SetWindowText(ch3);}void CMyDlg::OnBnClickedClearwrite()//初始化编辑框数据{// TODO: 在此添加控件通知处理程序代码m_edit1.SetSel(0,-1);m_edit1.Clear();m_edit2.SetSel(0,-1);m_edit2.Clear();m_edit3.SetSel(0,-1);m_edit3.Clear();}void CMyDlg::OnBnClickedClearsave()//初始化保存框数据{// TODO: 在此添加控件通知处理程序代码m_edit4.SetSel(0,-1);m_edit4.Clear();m_edit5.SetSel(0,-1);m_edit5.Clear();}void CMyDlg::OnBnClickedCopy()//复制// TODO: 在此添加控件通知处理程序代码m_edit3.SetSel(0,-1);m_edit3.Copy();}void CMyDlg::OnBnClickedPaste()//粘贴{// TODO: 在此添加控件通知处理程序代码if (nEDITtemp==1)m_edit4.Paste();else if(nEDITtemp==2)m_edit5.Paste();}void CMyDlg::OnEnSetfocusEdit4()//判断光标是否在SAVEREASULT1指示框中{// TODO: 在此添加控件通知处理程序代码nEDITtemp=1;}void CMyDlg::OnEnSetfocusEdit5()//判断光标是否在SAVEREASULT2指示框中{// TODO: 在此添加控件通知处理程序代码nEDITtemp=2;}运行结果:。
MFC计算器
基于VC的MFC计算器案例步骤及源代码基于VC的MFC计算器案例详细步骤有图有代码,图中右边的各个函数也都能实现:清除,加,减,乘,除,倒数,e的x次方,以e为底x的对数,10的x次方,以10为底x的对数,余弦,反余弦,双曲余弦值,正弦,反正弦,双曲正弦值,正切,反正切,双曲正切值,x的y次方,2的x次方,n(n为整数)的阶乘。
如图:步骤:1.创建一个基于对话框的应用程序(这一步应该都会吧!),命名为dckCalculator;2.打开资源视图->点击“dckCalculator”左边的“+”->点击“dckCalculator.rc”左边的“+”->点击“Dialog”左边的“+”->双击“IDD_DCKCALCULATOR_DIALOG”->去除“确定”“取消”“TODO:在此放置对话框控件。
”几个组件(全选。
然后右击选择“删除”即可):3.按照我们想要的效果给对话框添加组件->编辑各个组件的显示名字->改变其ID属性4.将编辑框的属性中的Align Text设置为right,如图:5.设置相关属性(每次改变ID都要保存一下):6.将编辑框中属性的“read only”设置为ture!7.在类视图中给CdckCalculatorDlg类添加成员变量:int m_duType;初始值为0,用来作为判定弧度还是角度的变准;double m_first;//存储一次运算的第一个操作数及一次运算的结果double m_second;//存储一次运算的第二个操作数CString m_operator;//存储运算符double m_coff;//存储小数点的系数权值8.给编辑框添加关联变量:(右击编辑框->添加变量)CString m_display;//编辑框IDC_DISPLAY的关联变量,显示计算结果(注意最右边选择“value”)9.双击“弧度”给其添加事件响应(等价于右击然后添加事件处理器);同理双击“角度”添加代码://弧度处理函数void CdckCalculatorDlg::OnBnClickedHudu(){m_duType=0;//系统默认m_duType为,这里为刚好可以为计算器默认为弧度计算}//角度处理函数void CdckCalculatorDlg::OnBnClickedJiaodu(){m_duType=1;//当选择角度处理函数的时候,m_duType为;这哥主要是作为以后函数算法的判断标准}10.在对话框类的构造函数中,初始化成员变量:// CdckCalculatorDlg 对话框的构造函数CdckCalculatorDlg::CdckCalculatorDlg(CWnd* pParent /*=NULL*/): CDialog(CdckCalculatorDlg::IDD, pParent), m_duType(0), m_first(0.0), m_second(0.0), m_operator(_T("+")), m_coff(0), m_display(_T("0.0"))······11.类视图里手动为对话框添加2个函数:void UpdateDisplay(double dck)——用于编辑框显示数据Void Calculate(void)——用于计算结果代码如下(由于在代码中要用到fabs,要在CdckCalculatorDlg.cpp 里添加一个库:#include"math.h")://在编辑框中显示数据void CdckCalculatorDlg::UpdateDisplay(double dck){m_display.Format(_T("%f"),dck);int i=m_display.GetLength();while(m_display.GetAt(i-1)=='0') //格式化输出,将输出结果后的零截去{ m_display.Delete(i-1,1); i--; }UpdateData(false);//更新编辑框变量m_display}//计算结果void CdckCalculatorDlg::Calculate(void){//将前一次数据与当前数据进行运算,作为下次的第一操作数,并在编辑框显示。
MFC计算器主要代码(C++)
// Calculator_17483Dlg.cpp : implementation file//#include"stdafx.h"#include"Calculator_17483.h"#include"Calculator_17483Dlg.h"#include"afxdialogex.h"#ifdef _DEBUG#definenew DEBUG_NEW#endif// CAboutDlg dialog used for App AboutclassCAboutDlg : publicCDialogEx{public:CAboutDlg();// Dialog Dataenum { IDD = IDD_ABOUTBOX };protected:virtualvoid DoDataExchange(CDataExchange* pDX); // DDX/DDV support// Implementationprotected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){}voidCAboutDlg::DoDataExchange(CDataExchange* pDX) {CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()// CCalculator_17483Dlg dialogCCalculator_17483Dlg::CCalculator_17483Dlg(CWnd*pParent/*=NULL*/): CDialogEx(CCalculator_17483Dlg::IDD, pParent){num1=0;num2=0;operation=0;point=0;nump_1=0;nump_2=0;m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}voidCCalculator_17483Dlg::DoDataExchange(CDataExchange* pDX) {CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CCalculator_17483Dlg, CDialogEx) ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1,&CCalculator_17483Dlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON2,&CCalculator_17483Dlg::OnBnClickedButton2)ON_BN_CLICKED(IDC_BUTTON3,&CCalculator_17483Dlg::OnBnClickedButton3)ON_BN_CLICKED(IDC_BUTTON4,&CCalculator_17483Dlg::OnBnClickedButton4)ON_BN_CLICKED(IDC_BUTTON5,&CCalculator_17483Dlg::OnBnClickedButton5)ON_BN_CLICKED(IDC_BUTTON6,&CCalculator_17483Dlg::OnBnClickedButton6)ON_BN_CLICKED(IDC_BUTTON7,&CCalculator_17483Dlg::OnBnClickedButton7)ON_BN_CLICKED(IDC_BUTTON8,&CCalculator_17483Dlg::OnBnClickedButton8)ON_BN_CLICKED(IDC_BUTTON9,&CCalculator_17483Dlg::OnBnClickedButton9)ON_BN_CLICKED(IDC_BUTTON10,&CCalculator_17483Dlg::OnBnClickedButton10)ON_BN_CLICKED(IDC_BUTTON11,&CCalculator_17483Dlg::OnBnClickedButton11)ON_BN_CLICKED(IDC_BUTTON12,&CCalculator_17483Dlg::OnBnClickedButton12)ON_BN_CLICKED(IDC_BUTTON13,&CCalculator_17483Dlg::OnBnClickedButton13)ON_BN_CLICKED(IDC_BUTTON14,&CCalculator_17483Dlg::OnBnClickedButton14)ON_BN_CLICKED(IDC_BUTTON15,&CCalculator_17483Dlg::OnBnClickedButton15)ON_BN_CLICKED(IDC_BUTTON16,&CCalculator_17483Dlg::OnBnClickedButton16)ON_BN_CLICKED(IDC_BUTTON17,&CCalculator_17483Dlg::OnBnClickedButton17)END_MESSAGE_MAP()// CCalculator_17483Dlg message handlersBOOLCCalculator_17483Dlg::OnInitDialog(){CDialogEx::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){BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}voidCCalculator_17483Dlg::OnSysCommand(UINT nID,LPARAM lParam){if ((nID& 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialogEx::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. voidCCalculator_17483Dlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND,reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// Center icon in client rectangleint 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 icondc.DrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}}// The system calls this function to obtain the cursor to display while the user drags// the minimized window.HCURSORCCalculator_17483Dlg::OnQueryDragIcon(){returnstatic_cast<HCURSOR>(m_hIcon);voidCCalculator_17483Dlg::OnBnClickedButton13()//0{if(operation==0){num1=num1*10+0;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+0;string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,string_num2);}// TODO: Add your control notification handler code here}//0voidCCalculator_17483Dlg::OnBnClickedButton9()//1{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+1;//string_num1.Format(_T("%d"),num1);string_num1+="1";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+1;//string_num2.Format(_T("%d"),num2);string_num2+="1";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="1";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="1";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//1voidCCalculator_17483Dlg::OnBnClickedButton10()//2{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+2;//string_num1.Format(_T("%d"),num1);string_num1+="2";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+2;//string_num2.Format(_T("%d"),num2);string_num2+="2";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="2";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="2";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//2voidCCalculator_17483Dlg::OnBnClickedButton11()//3{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+3;//string_num1.Format(_T("%d"),num1);string_num1+="3";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+3;//string_num2.Format(_T("%d"),num2);string_num2+="3";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="3";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="3";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//3voidCCalculator_17483Dlg::OnBnClickedButton5()//4{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+4;//string_num1.Format(_T("%d"),num1);string_num1+="4";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+4;//string_num2.Format(_T("%d"),num2);string_num2+="4";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="4";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="4";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//4voidCCalculator_17483Dlg::OnBnClickedButton6()//5{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+5;//string_num1.Format(_T("%d"),num1);string_num1+="5";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+5;//string_num2.Format(_T("%d"),num2);string_num2+="5";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="5";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="5";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//5voidCCalculator_17483Dlg::OnBnClickedButton7()//6{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+6;//string_num1.Format(_T("%d"),num1);string_num1+="6";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+6;//string_num2.Format(_T("%d"),num2);string_num2+="6";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="6";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="6";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//6voidCCalculator_17483Dlg::OnBnClickedButton1()//7{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+7;//string_num1.Format(_T("%d"),num1);string_num1+="7";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="7";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="7";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="7";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here }//7voidCCalculator_17483Dlg::OnBnClickedButton2()//8{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+8;//string_num1.Format(_T("%d"),num1);string_num1+="8";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+8;//string_num2.Format(_T("%d"),num2);string_num2+="8";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="8";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="8";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//8voidCCalculator_17483Dlg::OnBnClickedButton3()//9{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+9;//string_num1.Format(_T("%d"),num1);string_num1+="9";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+9;//string_num2.Format(_T("%d"),num2);string_num2+="9";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="9";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="9";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//9voidCCalculator_17483Dlg::OnBnClickedButton4()//+{if(point==0){if(operation==0){SetDlgItemTextW(IDC_EDIT4,_T("+"));operation=1;}else{switch(operation){case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;}num2=0;operation=1;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("+"));}}else{operation=1;point=0;// char*p1=(LPSTR)(LPCTSTR)string_num1;// char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,string_num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("+"));//int len=string_num1.GetLength();//intnbyte=WideCharToMultiByte(CP_ACP,0,string_num1,len,NULL,0,NU LL,NULL);//char *p=new char[nbyte+1];//memset(p,0,len+1);//WideCharToMultiByte(CP_OEMCP,0,string_num1,len,p,nbyte,N ULL,NULL);//p[nbyte]=0;//nump_1=atof(p);}// TODO: Add your control notification handler code here}//+voidCCalculator_17483Dlg::OnBnClickedButton8()//-{if(point==0){if(operation==0){SetDlgItemTextW(IDC_EDIT4,_T("-"));operation=2;}else{switch(operation){case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;}num2=0;operation=2;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("-"));}}else{operation=2;point=0;// char*p1=(LPSTR)(LPCTSTR)string_num1;// char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,string_num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("-"));}// TODO: Add your control notification handler code here }//-voidCCalculator_17483Dlg::OnBnClickedButton12()//*{if(point==0){if(operation==0){SetDlgItemTextW(IDC_EDIT4,_T("*"));operation=3;}else{switch(operation){case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;}num2=0;operation=3;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("*"));}}else{operation=3;switch(operation){case 1:resultp=nump_1+nump_2;break;case 2:resultp=nump_1-nump_2;break;case 3:resultp=nump_1*nump_2;break;case 4:resultp=nump_1/nump_2;break;}point=0;// char*p1=(LPSTR)(LPCTSTR)string_num1;//char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("*"));}// TODO: Add your control notification handler code here }//*voidCCalculator_17483Dlg::OnBnClickedButton16()///{if(point==0){if(operation==0){SetDlgItemTextW(IDC_EDIT4,_T("/"));operation=4;}else{switch(operation){case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;}num2=0;operation=4;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("/"));}}else{operation=4;point=0;// char*p1=(LPSTR)(LPCTSTR)string_num1;//char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("/"));}// TODO: Add your control notification handler code here }///voidCCalculator_17483Dlg::OnBnClickedButton14()//.{point_2=1;if(point==0){point=1;if(operation==0){string_num1+=".";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{string_num2+=".";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here }//.voidCCalculator_17483Dlg::OnBnClickedButton15()//={if(point_2==0){switch(operation){case 1:result=num1+num2;break;case 2:result=num1-num2;break;case 3:result=num1*num2;break;case 4:result=num1/num2;break;}string_result.Format(_T("%d"),result);SetDlgItemTextW(IDC_EDIT3,string_result);}else{//char*p1=(LPSTR)(LPCTSTR)string_num1;//char*p2=(LPSTR)(LPCTSTR)string_num2;//char str1=(string_num1.GetBuffer());wchar_t*den=NULL;nump_1=wcstod(string_num1.GetBuffer(),&den);nump_2=wcstod(string_num2.GetBuffer(),&den);switch(operation){case 1:resultp=nump_1+nump_2;break;case 2:resultp=nump_1-nump_2;break;case 3:resultp=nump_1*nump_2;break;case 4:resultp=nump_1/nump_2;break;}string_result.Format(_T("%f"),resultp);SetDlgItemTextW(IDC_EDIT3,string_result);}num1=0;num2=0;result=0;operation=0;point=0;// TODO: Add your control notification handler code here }//=voidCCalculator_17483Dlg::OnBnClickedButton17()//reset {num1=0;num2=0;result=0;operation=0;point=0;string_num1="";SetDlgItemTextW(IDC_EDIT1,_T(""));string_num2="";SetDlgItemTextW(IDC_EDIT2,_T(""));string_result="";SetDlgItemTextW(IDC_EDIT3,_T(""));SetDlgItemTextW(IDC_EDIT4,_T(""));// TODO: Add your control notification handler code here }//reset。
MFC计算器的实现
MFC计算器的实现MFC是Microsoft Foundation Classes的缩写,是一个框架,用于开发Windows平台上的应用程序。
在MFC框架下,我们可以很方便地实现计算器应用程序。
以下是一个基于MFC框架的简单计算器应用程序的实现。
1.首先,创建一个新的MFC应用程序项目,并选择创建一个对话框为基础的应用程序。
4. 创建一个新的类(例如名为CalculatorDlg)作为对话框的类派生自CDialog。
在类头文件中声明需要的成员变量和函数。
```cppclass CalculatorDlg : public CDialogpublic:CalculatorDlg(CWnd* pParent = nullptr);enum { IDD = IDD_DLG_CALC };protected:virtual void DoDataExchange(CDataExchange* pDX);protected:afx_msg void OnBnClickedButtonNumber(UINT nID); // 数字按钮点击事件处理函数afx_msg void OnBnClickedButtonOperator(UINT nID); // 运算符按钮点击事件处理函数afx_msg void OnBnClickedButtonClear(; // 清零按钮点击事件处理函数afx_msg void OnBnClickedButtonEqual(; //等于按钮点击事件处理函数DECLARE_MESSAGE_MAP};```5. 在类的.cpp文件中实现需要的成员函数和消息映射。
```cppIMPLEMENT_DYNAMIC(CalculatorDlg, CDialog)CalculatorDlg::CalculatorDlg(CWnd* pParent) :CDialog(IDD_DLG_CALC, pParent)//初始化成员变量m_hResultEdit = nullptr;void CalculatorDlg::DoDataExchange(CDataExchange* pDX)CDialog::DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CalculatorDlg, CDialog)ON_BN_CLICKED(IDC_BUTTON_0,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_1,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_2,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_3,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_4,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_5,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_6,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_7,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_8,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_9,&CalculatorDlg::OnBnClickedButtonNumber) ON_BN_CLICKED(IDC_BUTTON_ADD,&CalculatorDlg::OnBnClickedButtonOperator)ON_BN_CLICKED(IDC_BUTTON_SUBTRACT,&CalculatorDlg::OnBnClickedButtonOperator)ON_BN_CLICKED(IDC_BUTTON_MULTIPLY,&CalculatorDlg::OnBnClickedButtonOperator)ON_BN_CLICKED(IDC_BUTTON_DIVIDE,&CalculatorDlg::OnBnClickedButtonOperator)ON_BN_CLICKED(IDC_BUTTON_CLEAR,&CalculatorDlg::OnBnClickedButtonClear)ON_BN_CLICKED(IDC_BUTTON_EQUAL,&CalculatorDlg::OnBnClickedButtonEqual)END_MESSAGE_MAP``````cppvoid CalculatorDlg::OnBnClickedButtonNumber(UINT nID)//获取按钮上的数字CString strNumber;strNumber.Format(_T("%d"), nID - IDC_BUTTON_0);::SetWindowText(m_hResultEdit, strNumber);void CalculatorDlg::OnBnClickedButtonOperator(UINT nID) //获取按钮上的运算符TCHAR chOperator = '+';switch (nID)case IDC_BUTTON_ADD:chOperator = '+';break;case IDC_BUTTON_SUBTRACT:chOperator = '-';break;case IDC_BUTTON_MULTIPLY:chOperator = '*';break;case IDC_BUTTON_DIVIDE:chOperator = '/';break;}CString strOperator;strOperator.Format(_T("%c"), chOperator); ::SetWindowText(m_hResultEdit, strOperator); void CalculatorDlg::OnBnClickedButtonClear::SetWindowText(m_hResultEdit, _T(""));void CalculatorDlg::OnBnClickedButtonEqual//执行运算CString strExpression;::GetWindowText(m_hResultEdit, strExpression);//2.使用字符串解析和计算函数,计算结果//...//3.显示结果CString strResult;strResult.Format(_T("%f"), 123.456); // 替换为实际的计算结果::SetWindowText(m_hResultEdit, strResult);```请注意,在实际的计算过程中,您需要实现字符串解析和数学计算部分的代码。
VC++_mfc编计算器+源代码
实用标准文档用C++编写计算器程序搞要本课程设计是在基于对话框的应用程序中模拟一个计算器,本计算器可以进行十进制下的四则运算(加、减、乘、除)和四则混合运算,可以把十进制转化为二进制或十六进制,可以进行一些常用的函数运算(比如sin、cos、tan、cot、sqrt、ln等),还可以支持带“(”,“)”符号的表达式的计算。
系统开发平台为Windows XP,程序设计设计语言采用Visual C++6.0,程序运行平台为Windows 98/2000/XP。
程序通过调试运行,初步实现了设计目标。
关键词程序设计;计算器;C++;1 引言在现代社会中,计算器已经进入了每一个家庭,人们在生活和学习中经常需要使用到计算器,它的出现大大减少了人们在计算方面的工作量,可以说它在人们生活和学习中是不可缺少的。
1.1C++介绍C++语言的主要特点表现在两个方面,一是全面兼容C语言,二是支持面向对象的程序设计方法[1]。
(1) C++是一个更好的C,它保持了C语言的优点,大多数的C程序代码略作修改或不作修改就可在C++的集成环境下调试和运行。
这对于继承和开发当前已在广泛的软件是非常重要的,可以节省大量的人力和物力。
(2) C++是一种面向对象的程序设计语言它使得程序的各个模块的独立性更强,程序的可读性和可移植性更强,程序代码的结构更加合理,程序的扩充性更强。
这对于设计、编制和调试一些大型的软件尤为重要。
(3) C++集成环境不仅支持C++程序的编译和调试,而且也支持C程序的编译和调试。
通常,C++程序环境约定:当源程序文件的扩展名为c.时,则为C程序;而当源程序文件的扩展名为cpp.时,则为C++程序。
(4) C++语句非常简练,对语法限制比较宽松,因此C++语法非常灵活。
其优点是给用户编程带来书写上的方便。
其缺点是由于编译时对语法限制比较宽松,许多逻辑上的错误不容易发现,给用户编程增加了难度。
1.2计算器的介绍(1)在运行程序后,系统会弹出一个基于对话框的计算器界面,如下图所示:图1.1 计算器界面(2)在计算器程序中,主要通过一个编辑框来获取表达式和显示计算结果,表达式可以通过键盘和单击按钮2种方式输入,输入后的结果如下图所示:图1.2 输入表达式后的界面(3)在输入完表达式后,单击“=”后,开始对表达式进行计算,计算完成后,在编辑框中显示计算的结果。
C语言MFC基础之计算器详解
C语⾔MFC基础之计算器详解⽬录基于MFC计算器(基础点)需求:把计算器运算的过程呈现出来。
基础点总结基于MFC计算器(基础点)主要是在实现MFC计算器的过程中碰到的⼀些问题,和补充的⼀些知识。
需求:把计算器运算的过程呈现出来。
想法:利⽤两个队列,⼀个存储输⼊的字符串(表达式),⼀个存放结果。
分别命名为queue1,和myqueue,让字符串从左到右依次⼊队列。
但之后按照⾃⼰的思路去推演,发现如果进⼊队列的是1+23,如果下⼀个字符⼊队的是“+”,那么先计算23的结果⼊队myqueue中,⽽queue1中还储存1,但下⼀个“+”⼊ queue1之后,此时queue1中包含1+,最后把myqueue中的元素和queue1结合,最终在编辑框⽂本中显⽰的是:1+6;如果下⼀个运算符是“*”,那么直接输出队列中的字符串。
在实现的过程中会涉及到类型转换,⽐如CString转换成浮点型。
⽬前还没有实现,主要原因在于对基础的语法不熟悉,还不太会使⽤先总结前⼀段时间碰到的⼀些问题,和解决⽅案。
基础点stack,queue,⽽map函数映射库是char到int的映射,实现了优先级的定义。
C++中的compare():涌来进⾏字符串以及⼦串之间的⽐较。
getline():读整⾏,包括前导和嵌⼊的空格并将其储存在字符串对象中。
length=strlen():遍历字符串strlen():可以做⼀个计数器,从内存的某个位置扫描,直到碰到第⼀个字符串结束符‘\0'为⽌,返回计算器值bool类型做判断:true,falsef分别是:对错,是⾮,正反atof():把字符转化为浮点数ofstream():写操作ofstream fout:是对C++SIT中对⽂件操作的合集,包含了常⽤的所有⽂件操作:插⼊器,析取器fout.open(“txt”,ios::out):打开⽂件流ifstream():读操作,从硬盘到内存fstream():同时进⾏读写ios:in,⽂件以输⼊的⽅式打开。
mfc入门教程之实现一个简单的计算器
mfc⼊门教程之实现⼀个简单的计算器mfc学习之前的了解什么是mfc?MFC是微软基础类的缩写(Microsoft Foundation Classes),是⼀个庞⼤的类库,可以理解为⼀种在Windows上开发软件的架构,是微软专为Visual C++定制的。
该类库提供⼀组通⽤的可重⽤的类库供开发⼈员使⽤。
没有MFC之前,Windows上⽤Win32 API进⾏编程,之后MFC出现,在⼀定程度上提⾼了软件开发效率,它是对win32 API的封装,所以易⽤性好,不过性能会⽐win32开发低⼀些,⼆者各有所长。
在Windows上开发界⾯程序以前⽤MFC最合适,能兼顾效率和性能。
简单来说mfc就是⽅便windows程序开发的框架,不单单是界⾯,更有很多类库。
mfc能⼲什么?在学习mfc之前想必⼤家都会关系这个问题,很多⼈学习了c++,或者是学习了c和c++之后发现还是只能⿊窗⼝下⾯做做数学题,写写算法,做不出什么实际的东西(这⾥不是说c/++做不出实际的东西,⽽是⼤多数的⼈做不出)。
问这个问题就像问⼀把剪⼑能⼲什么,剪⼑什么也⼲不了,我们可以拿着剪⼑剪东西,剪的好坏更多的是在于使⽤者⽽不是⼯具本⾝。
当使⽤⼀件⼯具熟练之后去使⽤同类的⼯具你会发现很轻松就能上⼿,因为它们看似有区别实则万变不离其宗。
mfc和qt的选择很多⼈会去推荐学习qt说mfc过时了,如果你想要跨平台那么qt⾮你莫属,如果只是在windows上,那么mfc⽆可替代。
⽆论是qt还是mfc在windows平台上内部的实现原理都是⼀样的,只是封装上有些区别。
mfc制作简单计算器-界⾯设计1.新建mfc项⽬启动vs2013,点击⽂件->新建->项⽬->vc++->mfc应⽤程序点击确定进⼊mfc应⽤程序向导下⼀步选择基于对话框,点击完成。
这时候mfc为我们⽣成了默认的界⾯,⼀个静态⽂本框,两个按钮。
2.计算器界⾯设计⾸先删除三个默认创建的控件,选中之后右键,删除或者直接按下delete键。
手把手教你MFC编程计算器.docx
STEP 1 (页面设计部分与控件添加部分)首先打开VC,选择MFC AppWizard[exe],设定好路径和工程名(这里我设置工程名为为“My')。
这里要注意的是在创建向导步骤1的时候,我们选择基本对话框”。
之后我们可以点击完成便看到以下界面此时我们把当前页面上原配的控件按 Delete 全部清除(如下图)口工悴(D Atit'U Sfi MLkd.工Sb 粗煙也 m®a )工冋(D 旨□世i 耳邑*Q0 H ■:・—!<<endl_*jMICUYOlg 巳 HN_CUCK£IJQ I K3置逛曲门圉m 中ui 血a' :Win3?PchugIp I ■+ 厚 MY cigs sesI V I I 14 P 4 II Fl I' ■ I I I)(| r :h 切丼斤胎耳i,吒它IT 丰習養看孔中咅滇專哗圖.>.沁 叱吟网/ \ *N|]粤|肄轉韓姑 I 曰 $ m 暮口r*ia;_ ?M \. I C sn v uGEKW (遞试l 在文律L 中暨復\在艾弊沖Ji 找二詁舉\佩曲吨皿/1 *JJ「]斑臥酚書0.0r 320x200接着我们按照 MFC 自带的控件选项进行我们本次计算器的控件添加(以下是控件)岂建辟①涌陽IT 弧扎⑴工程®狙連眠齐馬3二总© 稱勘⑩ _ SCMYI )I <] 【[悩| 日・理 mci!ilw 「T 「・CMVDI|t * FH ▼;□阖宙喰<<rndl・1 - 1 w0 tH n ©a II S(£] 13击注阖M&屯占ul 4 I , +| Win 議Dc^U. — 討 徐88爲:?旬曲[L4. ill JI ai ('a I ■■!■■ J B I L BI *ltadbdll ・li ・l ・・ii ・・ lltulijll列毎邂為湎口 口区宀枣圍陰C □日西我们在原先清空的界面中依次用上图控件画出一个基本的计算器页面(如下图)。
MFC入门教程之简易计算器制作
MFC入门之简易计算器制作最近自学MFC,入门的时候走了不少弯路,所以特做此教程,希望能帮助一些人能够快速入门,少走一些弯路。
这个教程就是通过一个简易计算器的制作让大家了解MFC界面编程的基本步骤和几个常用控件的使用方法。
使用软件Visual C++ 6.0.首先,新建一个MFC基于对话框的工程。
点击文件-新建,在工程选项卡里选择MFC AppWizard[exe],给工程命名Jisuan。
点击确定。
在接下来的向导里选择基于对话框,其他选项使用默认即可,之间点击完成。
先给大家介绍一下Visual C++ 6.0的界面。
左边框起来的是文件资源控制区,默认有三个选项卡,分别是ClassView、ResourceView和FileView。
分别用来显示类、资源和文件。
中间这个主要操作区呢,就是我们用来设计界面和编程的窗口了。
现在这个是IDD_JISUAN_DIALOG资源,如图所示。
右边那个是控件工具箱,如果没有显示的话可以右击菜单栏空白区域,把控件前打上对号即可。
然后给大家介绍一下今天需要用到的控件。
如左图所示,今天主要需要用到四个控件。
第一个是静态文本控件,就是可以往对话框上放一些静态的文字。
第二个是编辑框,它是可以往里面输入文字的。
第三个是组合框,它是把用来给对话框分区,使界面更加美观明了。
第四个是按钮控件,是用来点一下产生某个操作的。
上面那个是编辑框,下面这个是按钮控件。
点一下控件,拖到对话框上,即可使用。
下面我们可以这样布局:上面两个编辑框里输入数字,中间四个按钮分别是+ - * /,然后下面编辑框里显示数字。
下面开始给大家介绍第一个最重要的操作,就是属性操作。
我们想让这些控件的显示文字改变,就需要修改它的属性。
选定某个控件,右击,选择属性,即可来改变它们的属性了。
我们挨个控件介绍。
首先是组合框。
组合框的属性,我们看到有一个ID,一个标题。
首先说明一点,每个控件都必须有一个ID值,而且一般每个控件的ID值不能相同。
MFC简单计算器 C++
MFC计算器实现(VS2008/vs2010)随心无语第二篇,MFC初步学习用MFC写个计算器练习下.1:文件->新建->项目->MFC应用程序=>命名工程名(就叫CalculatorBata1吧!)MFC应用程序向导:应用程序类型:基于对话框(D)->完成//到此为止准备工作完成了一般用户界面->自己看吧,随便都可以!2:把面板上的“确定”,“取消”等删掉->工具箱:Button和EditControl控件移动到面板上,吧Button的Caption改为对应数字、符号->属性->杂项:修改ID为自己容易理解的方式(也可以不修改,这一步要在编译前完成,否则后面因ID无法识别而编译失败)3:右击面板上的EditControl编辑框->添加成员变量->控件ID:确定是EditControl的->类别:value(改了类别才能改类型)->类型:double //这个就是要出现在编辑框的数据,你也可以改为CString来显示汉字等->你喜欢啥就叫啥,比如叫result_14:在头文件CalculatorBata1Dlg.h的对话框类最后一行将出现(public)你刚刚添加的变量:double result_1;这时你可以自己其后面添加一些自己需要的其它数据,比如运算符标志operation,数据缓存temp等等/*到此为止,所有准备工作完成了!接下来看看现象吧*/5、返回到:资源视图,打开面板,双击Button控件(如Caption为数字1的那个)->进入CalculatorBata1Dlg.cpp中对应控件Button函数然后键入代码:result=123;UpdateData(flase);//刷新编辑框中显示数字,具体去问MSDN,问百度也是可以的6、编译F7->运行F5->单机对话框中的(Button)"1",这是编辑框将出现"123"。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// Calculator_17483Dlg.cpp : implementation file//#include"stdafx.h"#include"Calculator_17483.h"#include"Calculator_17483Dlg.h"#include"afxdialogex.h"#ifdef _DEBUG#definenew DEBUG_NEW#endif// CAboutDlg dialog used for App AboutclassCAboutDlg : publicCDialogEx{public:CAboutDlg();// Dialog Dataenum { IDD = IDD_ABOUTBOX };protected:virtualvoid DoDataExchange(CDataExchange* pDX); // DDX/DDV support// Implementationprotected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){}voidCAboutDlg::DoDataExchange(CDataExchange* pDX) {CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()// CCalculator_17483Dlg dialogCCalculator_17483Dlg::CCalculator_17483Dlg(CWnd*pParent/*=NULL*/): CDialogEx(CCalculator_17483Dlg::IDD, pParent){num1=0;num2=0;operation=0;point=0;nump_1=0;nump_2=0;m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}voidCCalculator_17483Dlg::DoDataExchange(CDataExchange* pDX) {CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CCalculator_17483Dlg, CDialogEx) ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1,&CCalculator_17483Dlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON2,&CCalculator_17483Dlg::OnBnClickedButton2)ON_BN_CLICKED(IDC_BUTTON3,&CCalculator_17483Dlg::OnBnClickedButton3)ON_BN_CLICKED(IDC_BUTTON4,&CCalculator_17483Dlg::OnBnClickedButton4)ON_BN_CLICKED(IDC_BUTTON5,&CCalculator_17483Dlg::OnBnClickedButton5)ON_BN_CLICKED(IDC_BUTTON6,&CCalculator_17483Dlg::OnBnClickedButton6)ON_BN_CLICKED(IDC_BUTTON7,&CCalculator_17483Dlg::OnBnClickedButton7)ON_BN_CLICKED(IDC_BUTTON8,&CCalculator_17483Dlg::OnBnClickedButton8)ON_BN_CLICKED(IDC_BUTTON9,&CCalculator_17483Dlg::OnBnClickedButton9)ON_BN_CLICKED(IDC_BUTTON10,&CCalculator_17483Dlg::OnBnClickedButton10)ON_BN_CLICKED(IDC_BUTTON11,&CCalculator_17483Dlg::OnBnClickedButton11)ON_BN_CLICKED(IDC_BUTTON12,&CCalculator_17483Dlg::OnBnClickedButton12)ON_BN_CLICKED(IDC_BUTTON13,&CCalculator_17483Dlg::OnBnClickedButton13)ON_BN_CLICKED(IDC_BUTTON14,&CCalculator_17483Dlg::OnBnClickedButton14)ON_BN_CLICKED(IDC_BUTTON15,&CCalculator_17483Dlg::OnBnClickedButton15)ON_BN_CLICKED(IDC_BUTTON16,&CCalculator_17483Dlg::OnBnClickedButton16)ON_BN_CLICKED(IDC_BUTTON17,&CCalculator_17483Dlg::OnBnClickedButton17)END_MESSAGE_MAP()// CCalculator_17483Dlg message handlersBOOLCCalculator_17483Dlg::OnInitDialog(){CDialogEx::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){BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}voidCCalculator_17483Dlg::OnSysCommand(UINT nID,LPARAM lParam){if ((nID& 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialogEx::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. voidCCalculator_17483Dlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND,reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// Center icon in client rectangleint 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 icondc.DrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}}// The system calls this function to obtain the cursor to display while the user drags// the minimized window.HCURSORCCalculator_17483Dlg::OnQueryDragIcon(){returnstatic_cast<HCURSOR>(m_hIcon);voidCCalculator_17483Dlg::OnBnClickedButton13()//0{if(operation==0){num1=num1*10+0;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+0;string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,string_num2);}// TODO: Add your control notification handler code here}//0voidCCalculator_17483Dlg::OnBnClickedButton9()//1{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+1;//string_num1.Format(_T("%d"),num1);string_num1+="1";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+1;//string_num2.Format(_T("%d"),num2);string_num2+="1";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="1";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="1";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//1voidCCalculator_17483Dlg::OnBnClickedButton10()//2{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+2;//string_num1.Format(_T("%d"),num1);string_num1+="2";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+2;//string_num2.Format(_T("%d"),num2);string_num2+="2";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="2";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="2";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//2voidCCalculator_17483Dlg::OnBnClickedButton11()//3{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+3;//string_num1.Format(_T("%d"),num1);string_num1+="3";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+3;//string_num2.Format(_T("%d"),num2);string_num2+="3";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="3";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="3";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//3voidCCalculator_17483Dlg::OnBnClickedButton5()//4{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+4;//string_num1.Format(_T("%d"),num1);string_num1+="4";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+4;//string_num2.Format(_T("%d"),num2);string_num2+="4";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="4";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="4";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//4voidCCalculator_17483Dlg::OnBnClickedButton6()//5{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+5;//string_num1.Format(_T("%d"),num1);string_num1+="5";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+5;//string_num2.Format(_T("%d"),num2);string_num2+="5";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="5";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="5";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//5voidCCalculator_17483Dlg::OnBnClickedButton7()//6{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+6;//string_num1.Format(_T("%d"),num1);string_num1+="6";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+6;//string_num2.Format(_T("%d"),num2);string_num2+="6";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="6";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="6";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//6voidCCalculator_17483Dlg::OnBnClickedButton1()//7{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+7;//string_num1.Format(_T("%d"),num1);string_num1+="7";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="7";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="7";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="7";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here }//7voidCCalculator_17483Dlg::OnBnClickedButton2()//8{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+8;//string_num1.Format(_T("%d"),num1);string_num1+="8";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+8;//string_num2.Format(_T("%d"),num2);string_num2+="8";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="8";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="8";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//8voidCCalculator_17483Dlg::OnBnClickedButton3()//9{if(point==0){ //无T小?数ºy点Ì?if(operation==0){ //num1num1=num1*10+9;//string_num1.Format(_T("%d"),num1);string_num1+="9";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{num2=num2*10+9;//string_num2.Format(_T("%d"),num2);string_num2+="9";SetDlgItemTextW(IDC_EDIT2,string_num2);}}else{if(operation==0){float i;nump_1=num1;// for(i=0.7;nump_1>(int)nump_1;nump_1*10,i/10){}// nump_1=nump_1+i;string_num1+="9";// string_num1.Format(_T("%f"),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);}else{// num2=num2*10+7;//string_num2.Format(_T("%d"),num2);string_num2+="9";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here}//9voidCCalculator_17483Dlg::OnBnClickedButton4()//+{if(point==0){if(operation==0){SetDlgItemTextW(IDC_EDIT4,_T("+"));operation=1;}else{switch(operation){case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;}num2=0;operation=1;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("+"));}}else{operation=1;point=0;// char*p1=(LPSTR)(LPCTSTR)string_num1;// char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,string_num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("+"));//int len=string_num1.GetLength();//intnbyte=WideCharToMultiByte(CP_ACP,0,string_num1,len,NULL,0,NU LL,NULL);//char *p=new char[nbyte+1];//memset(p,0,len+1);//WideCharToMultiByte(CP_OEMCP,0,string_num1,len,p,nbyte,N ULL,NULL);//p[nbyte]=0;//nump_1=atof(p);}// TODO: Add your control notification handler code here}//+voidCCalculator_17483Dlg::OnBnClickedButton8()//-{if(point==0){if(operation==0){SetDlgItemTextW(IDC_EDIT4,_T("-"));operation=2;}else{switch(operation){case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;}num2=0;operation=2;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("-"));}}else{operation=2;point=0;// char*p1=(LPSTR)(LPCTSTR)string_num1;// char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,string_num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("-"));}// TODO: Add your control notification handler code here }//-voidCCalculator_17483Dlg::OnBnClickedButton12()//*{if(point==0){if(operation==0){SetDlgItemTextW(IDC_EDIT4,_T("*"));operation=3;}else{switch(operation){case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;}num2=0;operation=3;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("*"));}}else{operation=3;switch(operation){case 1:resultp=nump_1+nump_2;break;case 2:resultp=nump_1-nump_2;break;case 3:resultp=nump_1*nump_2;break;case 4:resultp=nump_1/nump_2;break;}point=0;// char*p1=(LPSTR)(LPCTSTR)string_num1;//char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("*"));}// TODO: Add your control notification handler code here }//*voidCCalculator_17483Dlg::OnBnClickedButton16()///{if(point==0){if(operation==0){SetDlgItemTextW(IDC_EDIT4,_T("/"));operation=4;}else{switch(operation){case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;}num2=0;operation=4;string_num1.Format(_T("%d"),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T("%d"),num2);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("/"));}}else{operation=4;point=0;// char*p1=(LPSTR)(LPCTSTR)string_num1;//char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,_T(""));SetDlgItemTextW(IDC_EDIT4,_T("/"));}// TODO: Add your control notification handler code here }///voidCCalculator_17483Dlg::OnBnClickedButton14()//.{point_2=1;if(point==0){point=1;if(operation==0){string_num1+=".";SetDlgItemTextW(IDC_EDIT1,string_num1);}else{string_num2+=".";SetDlgItemTextW(IDC_EDIT2,string_num2);}}// TODO: Add your control notification handler code here }//.voidCCalculator_17483Dlg::OnBnClickedButton15()//={if(point_2==0){switch(operation){case 1:result=num1+num2;break;case 2:result=num1-num2;break;case 3:result=num1*num2;break;case 4:result=num1/num2;break;}string_result.Format(_T("%d"),result);SetDlgItemTextW(IDC_EDIT3,string_result);}else{//char*p1=(LPSTR)(LPCTSTR)string_num1;//char*p2=(LPSTR)(LPCTSTR)string_num2;//char str1=(string_num1.GetBuffer());wchar_t*den=NULL;nump_1=wcstod(string_num1.GetBuffer(),&den);nump_2=wcstod(string_num2.GetBuffer(),&den);switch(operation){case 1:resultp=nump_1+nump_2;break;case 2:resultp=nump_1-nump_2;break;case 3:resultp=nump_1*nump_2;break;case 4:resultp=nump_1/nump_2;break;}string_result.Format(_T("%f"),resultp);SetDlgItemTextW(IDC_EDIT3,string_result);}num1=0;num2=0;result=0;operation=0;point=0;// TODO: Add your control notification handler code here }//=voidCCalculator_17483Dlg::OnBnClickedButton17()//reset {num1=0;num2=0;result=0;operation=0;point=0;string_num1="";SetDlgItemTextW(IDC_EDIT1,_T(""));string_num2="";SetDlgItemTextW(IDC_EDIT2,_T(""));string_result="";SetDlgItemTextW(IDC_EDIT3,_T(""));SetDlgItemTextW(IDC_EDIT4,_T(""));// TODO: Add your control notification handler code here }//reset。