开发工具:
文件大小: 251kb
下载次数: 0
上传时间: 2012-05-03
详细说明: 一个用VC++编写的计算器小程序 // caculaterDlg.cpp : implementation file // #include "stdafx.h" #include "caculater.h" #include "caculaterDlg.h" #include "math.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBO X }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_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_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCaculaterDlg dialog CCaculaterDlg::CCaculaterDlg(CWnd* pParent /*=NULL*/) : CDialog(CCaculaterDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCaculaterDlg) m_strItem = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m=n=0; m_string=""; m_strItem=""; } void CCaculaterDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCaculaterDlg) DDX_Control(pDX, IDC_EDIT1, m_Edit); DDX_Text(pDX, IDC_EDIT1, m_strItem); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCaculaterDlg, CDialog) //{{AFX_MSG_MAP(CCaculaterDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON16, OnButton16) ON_BN_CLICKED(IDC_BUTTON18, OnButton18) ON_BN_CLICKED(IDC_C, OnC) ON_BN_CLICKED(IDC_0, On0) ON_BN_CLICKED(IDC_1, On1) ON_BN_CLICKED(IDC_2, On2) ON_BN_CLICKED(IDC_3, On3) ON_BN_CLICKED(IDC_4, On4) ON_BN_CLICKED(IDC_5, On5) ON_BN_CLICKED(IDC_6, On6) ON_BN_CLICKED(IDC_7, On7) ON_BN_CLICKED(IDC_8, On8) ON_BN_CLICKED(IDC_9, On9) ON_BN_CLICKED(IDC_BUTTON15, OnButton15) ON_BN_CLICKED(IDC_BUTTON10, OnButton10) ON_BN_CLICKED(IDC_BUTTON11, OnButton11) ON_BN_CLICKED(IDC_BUTTON12, OnButton12) ON_BN_CLICKED(IDC_BUTTON14, OnButton14) ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCaculaterDlg message handlers BOOL CCaculaterDlg::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); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); 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 dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CCaculaterDlg::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 CCaculaterDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(▭); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CCaculaterDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CCaculaterDlg::OnButton16() //(加号) { // TODO: Add your control notification handler code here if(!n) { m_string=m_strItem; if(m_string==""){m_string="";return;} } else { double num1,num2; num1=atof(m_string); num2=atof(m_strItem); switch(m_sign) { case '+' : num1+=num2;break; case '-' : num1-=num2;break; case '*' : num1*=num2;break; case '/' : if(!num2) AfxMessageBox("the divisor is 0!"); else num1/=num2;break; case '%':if(!num2) AfxMessageBox("the divisor is 0!"); else num1=(int)num2%(int)num1;break; default : break; } m_string.Format("%.6f",num1); } m_sign='+'; m_strItem=""; n++; m=0; m_Edit.SetWindowText(m_string); } void CCaculaterDlg::OnButton18() //(等号 ) { // TODO: Add your control notification handler code here UpdateData(); double num1,num2; num1=atof(m_string); num2=atof(m_strItem); switch(m_sign) { case '+':num1+=num2;break; case '-':num1-=num2;break; case '*':num1*=num2;break; case '/':if(!num2) AfxMessageBox("the divisor is 0!"); else num1/=num2;break; case '%':if(!num2) AfxMessageBox("the divisor is 0!"); else num1=(int)num2%(int)num1;break; default:break; } // m_strItem=(CString)num1; m_strItem.Format("%.6f",num1); m_Edit.SetWindowText(m_strItem); m_string=""; m_strItem=""; n=0; m=0; } void CCaculaterDlg::OnC() { // TODO: Add your control notification handler code here UpdateData(); m_strItem=""; UpdateData(false); } void CCaculaterDlg::On0() { // TODO: Add your control notification handler code here //UpdateData(); if(m_strItem!="0") { CString str="0"; m_strItem+=str; UpdateData(false);} } void CCaculaterDlg::On1() { // TODO: Add your control notification handler code here //UpdateData(); CString str="1"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::On2() { // TODO: Add your control notification handler code here //UpdateData(); CString str="2"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::On3() { // TODO: Add your control notification handler code here //UpdateData(); CString str="3"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::On4() { // TODO: Add your control notification handler code here //UpdateData(); CString str="4"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::On5() { // TODO: Add your control notification handler code here // UpdateData(); CString str="5"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::On6() { // TODO: Add your control notification handler code here // UpdateData(); CString str="6"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::On7() { // TODO: Add your control notification handler code here // UpdateData(); CString str="7"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::On8() { // TODO: Add your control notification handler code here // UpdateData(); CString str="8"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::On9() { // TODO: Add your control notification handler code here // UpdateData(); CString str="9"; m_strItem+=str; UpdateData(false); } void CCaculaterDlg::OnButton15() { // TODO: Add your control notification handler code here UpdateData(); if(m_strItem!="") {CString str; if(!m) { str=".";m++;} else str=""; m_strItem+=str; UpdateData(false); } } void CCaculaterDlg::OnButton10() //除 { // TODO: Add your control notification handler code here if(!n) { m_string=m_strItem; if(m_string==""){m_string="";return;} } else { double num1,num2; num1=atof(m_string); num2=atof(m_strItem); switch(m_sign) { case '+' : num1+=num2;break; case '-' : num1-=num2;break; case '*' : num1*=num2;break; case '/' : if(!num2) AfxMessageBox("the divisor is 0!"); else num1/=num2;break; case '%':if(!num2) AfxMessageBox("the divisor is 0!"); else num1=(int)num2%(int)num1;break; default : break; } m_string.Format("%.6f",num1); } m_sign='/'; m_strItem=""; n++; m=0; m_Edit.SetWindowText(m_string); } void CCaculaterDlg::OnButton11() //乘 { // TODO: Add your control notification handler code here if(!n) { m_string=m_strItem; if(m_string==""){m_string="";return;} } else { double num1,num2; num1=atof(m_string); num2=atof(m_strItem); switch(m_sign) { case '+' : num1+=num2;break; case '-' : num1-=num2;break; case '*' : num1*=num2;break; case '/' : if(!num2) AfxMessageBox("the divisor is 0!"); else num1/=num2;break; case '%':if(!num2) AfxMessageBox("the divisor is 0!"); else num1=(int)num2%(int)num1;break; default : break; } m_string.Format("%.6f",num1); } m_sign='*'; m_strItem=""; n++; m=0; m_Edit.SetWindowText(m_string); } void CCaculaterDlg::OnButton12() //减 { // TODO: Add your control notification handler code here if(!n) { m_string=m_strItem; if(m_string==""){m_string="";return;} } else { double num1,num2; num1=atof(m_string); num2=atof(m_strItem); switch(m_sign) { case '+' : num1+=num2;break; case '-' : num1-=num2;break; case '*' : num1*=num2;break; case '/' : if(!num2) AfxMessageBox("the divisor is 0!"); else num1/=num2;break; case '%':if(!num2) AfxMessageBox("the divisor is 0!"); else num1=(int)num2%(int)num1;break; default : break; } m_string.Format("%.6f",num1); } m_sign='-'; m_strItem=""; n++; m=0; m_Edit.SetWindowText(m_string); } void CCaculaterDlg::OnButton14() { // TODO: Add your control notification handler code here // CString str; // str="-"; // m_strItem=str+m_strItem; UpdateData(); if(m_strItem!="") { double num; num=atof(m_strItem); num=-num; m_strItem.Format("%f",num); UpdateData(false); } } void CCaculaterDlg::OnBackspace() { // TODO: Add your control notification handler code here m_Edit.GetWindowText(m_strItem); if(m_strItem.GetLength()) { CString str; str=m_strItem; m_strItem=str.Left(m_strItem.GetLength()-1); m_Edit.SetWindowText(m_strItem); } } BOOL CCaculaterDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if (pMsg-> message == WM_KEYDOWN) { if(pMsg-> wParam==VK_NUMPAD0) On0(); if(pMsg-> wParam==VK_NUMPAD0) On0(); if(pMsg-> wParam==VK_NUMPAD1) On1(); if(pMsg-> wParam==VK_NUMPAD2) On2(); if(pMsg-> wParam==VK_NUMPAD3) On3(); if(pMsg-> wParam==VK_NUMPAD4) On4(); if(pMsg-> wParam==VK_NUMPAD5) On5(); if(pMsg-> wParam==VK_NUMPAD6) On6(); if(pMsg-> wParam==VK_NUMPAD7) On7(); if(pMsg-> wParam==VK_NUMPAD8) On8(); if(pMsg-> wParam==VK_NUMPAD9) On9(); if(pMsg-> wParam==VK_MULTIPLY) OnButton11(); if(pMsg-> wParam==VK_DIVIDE) OnButton10(); if(pMsg-> wParam==VK_ADD) OnButton16(); if(pMsg-> wParam==VK_SUBTRACT) OnButton12(); if(pMsg-> wParam==VK_DECIMAL) OnButton15(); if(pMsg-> wParam==187) OnButton18(); if(pMsg-> wParam==8) OnBackspace(); } return CDialog::PreTranslateMessage(pMsg); } void CCaculaterDlg::OnButton1() { // TODO: Add your control notification handler code here UpdateData(); double num2; num2=atof(m_strItem); num2=sqrt(num2); m_strItem.Format("o",num2); m_Edit.SetWindowText(m_strItem); } void CCaculaterDlg::OnButton2() { // TODO: Add your control notification handler code here if(!n) { m_string=m_strItem; if(m_string==""){m_string="";return;} } else { double num1,num2; num1=atof(m_string); num2=atof(m_strItem); switch(m_sign) { case '+' : num1+=num2;break; case '-' : num1-=num2;break; case '*' : num1*=num2;break; case '/' : if(!num2) AfxMessageBox("the divisor is 0!"); else num1/=num2;break; case '%':if(!num2) AfxMessageBox("the divisor is 0!"); else num1=(int)num1%(int)num2;break; default : break; } m_string.Format("%d",num1); } m_sign='%'; m_strItem=""; n++; m=0; m_Edit.SetWindowText(m_string); } void CCaculaterDlg::OnButton3() { // TODO: Add your control notification handler code here double num1; num1=atof(m_strItem); num1=1/num1; m_strItem.Format("%.6f",num1); m_Edit.SetWindowText(m_strItem); } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.