MFC计算器项目——普通计算模块

 

MFC计算器项目——普通计算模块

标签: mfc 计算器开发 VC++
201人阅读  评论(0)  收藏  举报
  分类:
项目实践(3) 

普通计算模块:(运行效果图参见计算器项目综述

该模块开发文档截图


这一模块是该项目的核心。考虑核心代码的实现:由于运算符只有这六种(+、-、*、/、平方、开方)所以就可以将表达式中的运算符存在一个char数组中,然后先对该数组中平方和开方这两个一元运算符进行处理,这样表达式中就只剩下另外四种运算符了。而这四种运算符只有两个优先级,利用堆栈很容易实现。遇到+、-就将数字(或其相反数)push(),遇到*、/就进行pop(),这样最后将栈中的数字加和即可。

该模块完整代码如下:

[cpp]  view plain  copy
  1. // 简化调试Dlg.cpp : implementation file  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "简化调试.h"  
  6. #include "简化调试Dlg.h"  
  7. #include "cmath"  
  8. #include "stack"  
  9. #include "MyDlg_1.h"  
  10. #include "MyDlg_2.h"  
  11. #include "MyDlg_3.h"  
  12. using namespace std;  
  13. #ifdef _DEBUG  
  14. #define new DEBUG_NEW  
  15. #undef THIS_FILE  
  16. static char THIS_FILE[] = __FILE__;  
  17. #endif  
  18.   
  19. CString m_num("");  
  20. CString m_sig("");  
  21. double xiaoshu,jieguo,answer;  
  22. int zhengshu,index;  
  23. int i,j,num_lenth=0;  
  24. double num[50];  
  25. stack <double> result;  
  26. /////////////////////////////////////////////////////////////////////////////  
  27. // CAboutDlg dialog used for App About  
  28.   
  29. class CAboutDlg : public CDialog  
  30. {  
  31. public:  
  32.     CAboutDlg();  
  33.   
  34. // Dialog Data  
  35.     //{{AFX_DATA(CAboutDlg)  
  36.     enum { IDD = IDD_ABOUTBOX };  
  37.     //}}AFX_DATA  
  38.   
  39.     // ClassWizard generated virtual function overrides  
  40.     //{{AFX_VIRTUAL(CAboutDlg)  
  41.     protected:  
  42.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support  
  43.     //}}AFX_VIRTUAL  
  44.   
  45. // Implementation  
  46. protected:  
  47.     //{{AFX_MSG(CAboutDlg)  
  48.     //}}AFX_MSG  
  49.     DECLARE_MESSAGE_MAP()  
  50. };  
  51.   
  52. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)  
  53. {  
  54.     //{{AFX_DATA_INIT(CAboutDlg)  
  55.     //}}AFX_DATA_INIT  
  56. }  
  57.   
  58. void CAboutDlg::DoDataExchange(CDataExchange* pDX)  
  59. {  
  60.     CDialog::DoDataExchange(pDX);  
  61.     //{{AFX_DATA_MAP(CAboutDlg)  
  62.     //}}AFX_DATA_MAP  
  63. }  
  64.   
  65. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)  
  66.     //{{AFX_MSG_MAP(CAboutDlg)  
  67.         // No message handlers  
  68.     //}}AFX_MSG_MAP  
  69. END_MESSAGE_MAP()  
  70.   
  71. /////////////////////////////////////////////////////////////////////////////  
  72. // CMyDlg dialog  
  73.   
  74. CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)  
  75.     : CDialog(CMyDlg::IDD, pParent)  
  76. {  
  77.     //{{AFX_DATA_INIT(CMyDlg)  
  78.     m_str = _T("");  
  79.     //}}AFX_DATA_INIT  
  80.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32  
  81.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);  
  82. }  
  83.   
  84. void CMyDlg::DoDataExchange(CDataExchange* pDX)  
  85. {  
  86.     CDialog::DoDataExchange(pDX);  
  87.     //{{AFX_DATA_MAP(CMyDlg)  
  88.     DDX_Text(pDX, IDC_EDIT1, m_str);  
  89.     //}}AFX_DATA_MAP  
  90. }  
  91.   
  92. BEGIN_MESSAGE_MAP(CMyDlg, CDialog)  
  93.     //{{AFX_MSG_MAP(CMyDlg)  
  94.     ON_WM_SYSCOMMAND()  
  95.     ON_WM_PAINT()  
  96.     ON_WM_QUERYDRAGICON()  
  97.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)  
  98.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)  
  99.     ON_BN_CLICKED(IDC_BUTTON3, OnButton3)  
  100.     ON_BN_CLICKED(IDC_BUTTON4, OnButton4)  
  101.     ON_BN_CLICKED(IDC_BUTTON5, OnButton5)  
  102.     ON_BN_CLICKED(IDC_BUTTON10, OnButton10)  
  103.     ON_BN_CLICKED(IDC_BUTTON11, OnButton11)  
  104.     ON_BN_CLICKED(IDC_BUTTON12, OnButton12)  
  105.     ON_BN_CLICKED(IDC_BUTTON13, OnButton13)  
  106.     ON_BN_CLICKED(IDC_BUTTON14, OnButton14)  
  107.     ON_BN_CLICKED(IDC_BUTTON6, OnButton6)  
  108.     ON_BN_CLICKED(IDC_BUTTON7, OnButton7)  
  109.     ON_BN_CLICKED(IDC_BUTTON8, OnButton8)  
  110.     ON_BN_CLICKED(IDC_BUTTON9, OnButton9)  
  111.     ON_BN_CLICKED(IDC_BUTTON0, OnButton0)  
  112.     ON_BN_CLICKED(IDC_BUTTON15, OnButton15)  
  113.     ON_BN_CLICKED(IDC_BUTTON16, OnButton16)  
  114.     ON_BN_CLICKED(IDC_BUTTON17, OnButton17)  
  115.     ON_BN_CLICKED(IDC_BUTTON18, OnButton18)  
  116.     ON_BN_CLICKED(IDC_BUTTON19, OnButton19)  
  117.     ON_COMMAND(ID_MENUITEM0, OnMenuitem0)  
  118.     ON_COMMAND(ID_MENUITEM1, OnMenuitem1)  
  119.     ON_COMMAND(ID_MENUITEM2, OnMenuitem2)  
  120.     ON_COMMAND(ID_MENUITEM3, OnMenuitem3)  
  121.     //}}AFX_MSG_MAP  
  122. END_MESSAGE_MAP()  
  123.   
  124. /////////////////////////////////////////////////////////////////////////////  
  125. // CMyDlg message handlers  
  126.   
  127. BOOL CMyDlg::OnInitDialog()  
  128. {  
  129.     CDialog::OnInitDialog();  
  130.   
  131.     // Add "About..." menu item to system menu.  
  132.   
  133.     // IDM_ABOUTBOX must be in the system command range.  
  134.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
  135.     ASSERT(IDM_ABOUTBOX < 0xF000);  
  136.   
  137.     CMenu* pSysMenu = GetSystemMenu(FALSE);  
  138.     if (pSysMenu != NULL)  
  139.     {  
  140.         CString strAboutMenu;  
  141.         strAboutMenu.LoadString(IDS_ABOUTBOX);  
  142.         if (!strAboutMenu.IsEmpty())  
  143.         {  
  144.             pSysMenu->AppendMenu(MF_SEPARATOR);  
  145.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);  
  146.         }  
  147.     }  
  148.   
  149.     // Set the icon for this dialog.  The framework does this automatically  
  150.     //  when the application's main window is not a dialog  
  151.     SetIcon(m_hIcon, TRUE);         // Set big icon  
  152.     SetIcon(m_hIcon, FALSE);        // Set small icon  
  153.       
  154.     // TODO: Add extra initialization here  
  155.       
  156.     return TRUE;  // return TRUE  unless you set the focus to a control  
  157. }  
  158.   
  159. void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)  
  160. {  
  161.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)  
  162.     {  
  163.         CAboutDlg dlgAbout;  
  164.         dlgAbout.DoModal();  
  165.     }  
  166.     else  
  167.     {  
  168.         CDialog::OnSysCommand(nID, lParam);  
  169.     }  
  170. }  
  171.   
  172. // If you add a minimize button to your dialog, you will need the code below  
  173. //  to draw the icon.  For MFC applications using the document/view model,  
  174. //  this is automatically done for you by the framework.  
  175.   
  176. void CMyDlg::OnPaint()   
  177. {  
  178.     if (IsIconic())  
  179.     {  
  180.         CPaintDC dc(this); // device context for painting  
  181.   
  182.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);  
  183.   
  184.         // Center icon in client rectangle  
  185.         int cxIcon = GetSystemMetrics(SM_CXICON);  
  186.         int cyIcon = GetSystemMetrics(SM_CYICON);  
  187.         CRect rect;  
  188.         GetClientRect(&rect);  
  189.         int x = (rect.Width() - cxIcon + 1) / 2;  
  190.         int y = (rect.Height() - cyIcon + 1) / 2;  
  191.   
  192.         // Draw the icon  
  193.         dc.DrawIcon(x, y, m_hIcon);  
  194.     }  
  195.     else  
  196.     {  
  197.         CDialog::OnPaint();  
  198.     }  
  199. }  
  200.   
  201. // The system calls this to obtain the cursor to display while the user drags  
  202. //  the minimized window.  
  203. HCURSOR CMyDlg::OnQueryDragIcon()  
  204. {  
  205.     return (HCURSOR) m_hIcon;  
  206. }  
  207.   
  208. void CMyDlg::OnButton1()   
  209. {  
  210.     // TODO: Add your control notification handler code here  
  211.     m_num = m_num + "1";  
  212.     m_str = m_str + "1";  
  213.     UpdateData(FALSE);  
  214. }  
  215.   
  216. void CMyDlg::OnButton2()   
  217. {  
  218.     // TODO: Add your control notification handler code here  
  219.     m_num = m_num + "2";  
  220.     m_str = m_str + "2";  
  221.     UpdateData(FALSE);  
  222. }  
  223.   
  224. void CMyDlg::OnButton3()   
  225. {  
  226.     // TODO: Add your control notification handler code here  
  227.     m_num = m_num + "3";  
  228.     m_str = m_str + "3";  
  229.     UpdateData(FALSE);  
  230. }  
  231.   
  232. void CMyDlg::OnButton4()   
  233. {  
  234.     // TODO: Add your control notification handler code here  
  235.     m_num = m_num + "4";  
  236.     m_str = m_str + "4";  
  237.     UpdateData(FALSE);  
  238. }  
  239.   
  240. void CMyDlg::OnButton5()   
  241. {  
  242.     // TODO: Add your control notification handler code here  
  243.     m_num = m_num + "5";  
  244.     m_str = m_str + "5";  
  245.     UpdateData(FALSE);  
  246. }  
  247.   
  248. void CMyDlg::OnButton10()   
  249. {  
  250.     // TODO: Add your control notification handler code here  
  251.     UpdateData();  
  252.     m_str = m_str + "+";  
  253.     zhengshu = 0;  
  254.     xiaoshu =0.0;  
  255.     index = 1;  
  256.     char cnum[50];  
  257.     strncpy(cnum,m_num,sizeof(cnum));  
  258.     for(i = 0;i < strlen(cnum);i++)  
  259.     {  
  260.         if(cnum[i] != '.')  
  261.             zhengshu = zhengshu * 10 + (cnum[i] - '0');  
  262.         else  
  263.         {  
  264.             for(j = i+1;j < strlen(cnum);j++,index++)  
  265.             {  
  266.                 xiaoshu += (cnum[j] - '0') / pow(10,index);  
  267.             }  
  268.             break;  
  269.         }  
  270.     }  
  271.     jieguo = zhengshu + xiaoshu;  
  272.     num[num_lenth++] = jieguo;  
  273.     m_sig = m_sig + "+";  
  274.     UpdateData(FALSE);  
  275.     m_num = "";  
  276. }  
  277.   
  278. void CMyDlg::OnButton11()   
  279. {  
  280.     // TODO: Add your control notification handler code here  
  281.     UpdateData();  
  282.     m_str = m_str + "-";  
  283.     zhengshu = 0;  
  284.     xiaoshu =0.0;  
  285.     index = 1;  
  286.     char cnum[50];  
  287.     strncpy(cnum,m_num,sizeof(cnum));  
  288.     for(i = 0;i < strlen(cnum);i++)  
  289.     {  
  290.         if(cnum[i] != '.')  
  291.             zhengshu = zhengshu * 10 + (cnum[i] - '0');  
  292.         else  
  293.         {  
  294.             for(j = i+1;j < strlen(cnum);j++,index++)  
  295.             {  
  296.                 xiaoshu += (cnum[j] - '0') / pow(10,index);  
  297.             }  
  298.             break;  
  299.         }  
  300.     }  
  301.     jieguo = zhengshu + xiaoshu;  
  302.     num[num_lenth++] = jieguo;  
  303.     m_sig = m_sig + "-";  
  304.     UpdateData(FALSE);  
  305.     m_num = "";  
  306. }  
  307.   
  308. void CMyDlg::OnButton12()   
  309. {  
  310.     // TODO: Add your control notification handler code here  
  311.     UpdateData();  
  312.     m_str = m_str + "×";  
  313.     zhengshu = 0;  
  314.     xiaoshu =0.0;  
  315.     index = 1;  
  316.     char cnum[50];  
  317.     strncpy(cnum,m_num,sizeof(cnum));  
  318.     for(i = 0;i < strlen(cnum);i++)  
  319.     {  
  320.         if(cnum[i] != '.')  
  321.             zhengshu = zhengshu * 10 + (cnum[i] - '0');  
  322.         else  
  323.         {  
  324.             for(j = i+1;j < strlen(cnum);j++,index++)  
  325.             {  
  326.                 xiaoshu += (cnum[j] - '0') / pow(10,index);  
  327.             }  
  328.             break;  
  329.         }  
  330.     }  
  331.     jieguo = zhengshu + xiaoshu;  
  332.     num[num_lenth++] = jieguo;  
  333.     m_sig = m_sig + "*";  
  334.     UpdateData(FALSE);  
  335.     m_num = "";  
  336. }  
  337.   
  338. void CMyDlg::OnButton13()   
  339. {  
  340.     // TODO: Add your control notification handler code here  
  341.     UpdateData();  
  342.     m_str = m_str + "÷";  
  343.     zhengshu = 0;  
  344.     xiaoshu =0.0;  
  345.     index = 1;  
  346.     char cnum[50];  
  347.     strncpy(cnum,m_num,sizeof(cnum));  
  348.     for(i = 0;i < strlen(cnum);i++)  
  349.     {  
  350.         if(cnum[i] != '.')  
  351.             zhengshu = zhengshu * 10 + (cnum[i] - '0');  
  352.         else  
  353.         {  
  354.             for(j = i+1;j < strlen(cnum);j++,index++)  
  355.             {  
  356.                 xiaoshu += (cnum[j] - '0') / pow(10,index);  
  357.             }  
  358.             break;  
  359.         }  
  360.     }  
  361.     jieguo = zhengshu + xiaoshu;  
  362.     num[num_lenth++] = jieguo;  
  363.     m_sig = m_sig + "/";  
  364.     UpdateData(FALSE);  
  365.     m_num = "";  
  366. }  
  367.   
  368. void CMyDlg::OnButton14()   
  369. {  
  370.     // TODO: Add your control notification handler code here  
  371.     UpdateData(TRUE);  
  372.     zhengshu = 0;  
  373.     xiaoshu =0.0;  
  374.     index = 1;  
  375.     char cnum[50];  
  376.     strncpy(cnum,m_num,sizeof(cnum));  
  377.     for(i = 0;i < strlen(cnum);i++)  
  378.     {  
  379.         if(cnum[i] != '.')  
  380.             zhengshu = zhengshu * 10 + (cnum[i] - '0');  
  381.         else  
  382.         {  
  383.             for(j = i+1;j < strlen(cnum);j++,index++)  
  384.             {  
  385.                 xiaoshu += (cnum[j] - '0') / pow(10,index);  
  386.             }  
  387.             break;  
  388.         }  
  389.     }  
  390.     jieguo = zhengshu + xiaoshu;  
  391.     num[num_lenth++] = jieguo;  
  392.     answer = 0;  
  393.     char csig[50];  
  394.     strncpy(csig,m_sig,sizeof(csig));  
  395.     for(i = 0;i < strlen(csig);i++)  
  396.     {  
  397.         if(csig[i] == '#' || csig[i] == '@')  
  398.         {  
  399.             if(csig[i] == '#')  
  400.                 num[i] = num[i] * num[i];  
  401.             else  
  402.                 num[i] = sqrt(num[i]);  
  403.             for(j = i;j<strlen(csig)-1;j++)  
  404.                 csig[j] = csig[j+1];  
  405.             csig[strlen(csig)-1] = '\0';  
  406.         }  
  407.     }  
  408.     result.push(num[0]);  
  409.     int m =-1;  
  410.     double b;  
  411.     while(1)  
  412.     {  
  413.         m++;  
  414.         if(csig[m] != '+' && csig[m] != '-' && csig[m] != '*' && csig[m] != '/')  
  415.             break;  
  416.         if(csig[m]=='+'||csig[m]=='-')  
  417.         {  
  418.             if(csig[m]=='+') result.push(num[m+1]);  
  419.             else result.push(-num[m+1]);  
  420.         }  
  421.         else              
  422.         {  
  423.             b=result.top();  
  424.             result.pop();  
  425.             if(csig[m]=='*') result.push(b*num[m+1]);  
  426.             else   
  427.             {  
  428.                 if(num[m+1]==0)  
  429.                 {  
  430.                     MessageBox("除以0,你是逗比吗?!\n重新计算吧。");  
  431.                     return ;  
  432.                 }  
  433.                 result.push(b/num[m+1]);  
  434.             }  
  435.         }  
  436.     }  
  437.     while(!result.empty())  
  438.     {  
  439.         answer+=result.top();  
  440.         result.pop();  
  441.     }  
  442.     m_str.Format("%f", answer);  
  443.     UpdateData(FALSE);  
  444. }  
  445.   
  446. void CMyDlg::OnButton16()   
  447. {  
  448.     // TODO: Add your control notification handler code here  
  449.     UpdateData();  
  450.     m_str = m_str + "^2";  
  451.     zhengshu = 0;  
  452.     xiaoshu =0.0;  
  453.     index = 1;  
  454.     char cnum[50];  
  455.     strncpy(cnum,m_num,sizeof(cnum));  
  456.     for(i = 0;i < strlen(cnum);i++)  
  457.     {  
  458.         if(cnum[i] != '.')  
  459.             zhengshu = zhengshu * 10 + (cnum[i] - '0');  
  460.         else  
  461.         {  
  462.             for(j = i+1;j < strlen(cnum);j++,index++)  
  463.             {  
  464.                 xiaoshu += (cnum[j] - '0') / pow(10,index);  
  465.             }  
  466.             break;  
  467.         }  
  468.     }  
  469.     jieguo = zhengshu + xiaoshu;  
  470.     num[num_lenth++] = jieguo;  
  471.     m_sig = m_sig + "#";  
  472.     UpdateData(FALSE);  
  473.     m_num = "";  
  474. }  
  475.   
  476. void CMyDlg::OnButton6()   
  477. {  
  478.     // TODO: Add your control notification handler code here  
  479.     m_num = m_num + "6";  
  480.     m_str = m_str + "6";  
  481.     UpdateData(FALSE);  
  482. }  
  483.   
  484. void CMyDlg::OnButton7()   
  485. {  
  486.     // TODO: Add your control notification handler code here  
  487.     m_num = m_num + "7";  
  488.     m_str = m_str + "7";  
  489.     UpdateData(FALSE);  
  490. }  
  491.   
  492. void CMyDlg::OnButton8()   
  493. {  
  494.     // TODO: Add your control notification handler code here  
  495.     m_num = m_num + "8";  
  496.     m_str = m_str + "8";  
  497.     UpdateData(FALSE);  
  498. }  
  499.   
  500. void CMyDlg::OnButton9()   
  501. {  
  502.     // TODO: Add your control notification handler code here  
  503.     m_num = m_num + "9";  
  504.     m_str = m_str + "9";  
  505.     UpdateData(FALSE);  
  506. }  
  507.   
  508. void CMyDlg::OnButton0()   
  509. {  
  510.     // TODO: Add your control notification handler code here  
  511.     m_num = m_num + "0";  
  512.     m_str = m_str + "0";  
  513.     UpdateData(FALSE);  
  514. }  
  515.   
  516. void CMyDlg::OnButton15()   
  517. {  
  518.     // TODO: Add your control notification handler code here  
  519.     if(m_num.GetAt(m_num.GetLength()-1)=='.')  
  520.         return ;  
  521.     m_num = m_num + ".";  
  522.     m_str = m_str + ".";  
  523.     UpdateData(FALSE);  
  524. }  
  525.   
  526. void CMyDlg::OnButton17()   
  527. {  
  528.     // TODO: Add your control notification handler code here  
  529.     UpdateData();  
  530.     m_str = m_str + "^0.5";  
  531.     zhengshu = 0;  
  532.     xiaoshu =0.0;  
  533.     index = 1;  
  534.     char cnum[50];  
  535.     strncpy(cnum,m_num,sizeof(cnum));  
  536.     for(i = 0;i < strlen(cnum);i++)  
  537.     {  
  538.         if(cnum[i] != '.')  
  539.             zhengshu = zhengshu * 10 + (cnum[i] - '0');  
  540.         else  
  541.         {  
  542.             for(j = i+1;j < strlen(cnum);j++,index++)  
  543.             {  
  544.                 xiaoshu += (cnum[j] - '0') / pow(10,index);  
  545.             }  
  546.             break;  
  547.         }  
  548.     }  
  549.     jieguo = zhengshu + xiaoshu;  
  550.     num[num_lenth++] = jieguo;  
  551.     m_sig = m_sig + "@";  
  552.     UpdateData(FALSE);  
  553.     m_num  = "";  
  554. }  
  555.   
  556. void CMyDlg::OnButton18()   
  557. {  
  558.     // TODO: Add your control notification handler code here  
  559.     MessageBox("1.该计算器可以进行混合运算\n\n2.每次重新计算之前请按CE","温馨提示");  
  560. }  
  561.   
  562. void CMyDlg::OnButton19()   
  563. {  
  564.     // TODO: Add your control notification handler code here  
  565.     UpdateData();  
  566.     m_num = "";  
  567.     m_sig = "";  
  568.     num_lenth = 0;  
  569.     m_str =  "";  
  570.     UpdateData(FALSE);  
  571. }  
  572.   
  573. void CMyDlg::OnMenuitem1()   
  574. {  
  575.     // TODO: Add your command handler code here  
  576.     CMyDlg::OnCancel();  
  577.     CMyDlg_1 dlg;  
  578.     dlg.DoModal();  
  579. }  
  580.   
  581. void CMyDlg::OnMenuitem2()   
  582. {  
  583.     // TODO: Add your command handler code here  
  584.     CMyDlg::OnCancel();  
  585.     CMyDlg_2 dlg;  
  586.     dlg.DoModal();  
  587. }  
  588.   
  589. void CMyDlg::OnMenuitem0()   
  590. {  
  591.     // TODO: Add your command handler code here  
  592.     MessageBox("您已处于普通计算模式!");  
  593. }  
  594.   
  595. void CMyDlg::OnMenuitem3()   
  596. {  
  597.     // TODO: Add your command handler code here  
  598.     CMyDlg::OnCancel();  
  599.     CMyDlg_3 dlg;  
  600.     dlg.DoModal();  
  601. }  

猜你喜欢

转载自blog.csdn.net/weixin_40236507/article/details/79799260