计算器部分代码

  1 // CalculatorDemoDlg.cpp : implementation file
  2 
  3 
  4 #include "stdafx.h"
  5 #include "TestCalculatorDemo.h"
  6 #include "CalculatorDemoDlg.h"
  7 #include "math.h"
  8 #include "string"
  9 
 10 #ifdef _DEBUG
 11 #define new DEBUG_NEW
 12 #undef THIS_FILE
 13 static char THIS_FILE[] = __FILE__;
 14 #endif
 15 
 16 /////////////////////////////////////////////////////////////////////////////
 17 // CCalculatorDemoDlg dialog
 18 
 19 
 20 CCalculatorDemoDlg::CCalculatorDemoDlg(CWnd* pParent /*=NULL*/)
 21     : CDialog(CCalculatorDemoDlg::IDD, pParent)
 22 {
 23     //{{AFX_DATA_INIT(CCalculatorDemoDlg)
 24         // NOTE: the ClassWizard will add member initialization here
 25     //}}AFX_DATA_INIT
 26 }
 27 
 28 
 29 void CCalculatorDemoDlg::DoDataExchange(CDataExchange* pDX)
 30 {
 31     CDialog::DoDataExchange(pDX);
 32     //{{AFX_DATA_MAP(CCalculatorDemoDlg)
 33         // NOTE: the ClassWizard will add DDX and DDV calls here
 34     //}}AFX_DATA_MAP
 35 }
 36 
 37 
 38 BEGIN_MESSAGE_MAP(CCalculatorDemoDlg, CDialog)
 39     //{{AFX_MSG_MAP(CCalculatorDemoDlg)
 40     ON_BN_CLICKED(IDC_BTN_NO1, OnBtnNo1)
 41     ON_BN_CLICKED(IDC_BTN_NO2, OnBtnNo2)
 42     ON_BN_CLICKED(IDC_BTN_NO3, OnBtnNo3)
 43     ON_BN_CLICKED(IDC_BTN_NO4, OnBtnNo4)
 44     ON_BN_CLICKED(IDC_BTN_NO5, OnBtnNo5)
 45     ON_BN_CLICKED(IDC_BTN_NO6, OnBtnNo6)
 46     ON_BN_CLICKED(IDC_BTN_NO7, OnBtnNo7)
 47     ON_BN_CLICKED(IDC_BTN_NO8, OnBtnNo8)
 48     ON_BN_CLICKED(IDC_BTN_NO9, OnBtnNo9)
 49     ON_BN_CLICKED(IDC_BTN_NO0, OnBtnNo0)
 50     ON_BN_CLICKED(IDC_BTN_PORT, OnBtnPort)
 51     ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)
 52     ON_BN_CLICKED(IDC_BTN_SUB, OnBtnSub)
 53     ON_BN_CLICKED(IDC_BTN_MUL, OnBtnMul)
 54     ON_BN_CLICKED(IDC_BTN_DIV, OnBtnDiv)
 55     ON_BN_CLICKED(IDC_BTN_EQUEL, OnBtnEquel)
 56     ON_BN_CLICKED(IDC_BTN_PERCENT, OnBtnPercent)
 57     ON_BN_CLICKED(IDC_BTN_DOWN, OnBtnDown)
 58     ON_BN_CLICKED(IDC_BTN_NEQUA, OnBtnNequa)
 59     ON_BN_CLICKED(IDC_BTN_SPACE, OnBtnSpace)
 60     ON_BN_CLICKED(IDC_BTN_C, OnBtnC)
 61     ON_BN_CLICKED(IDC_BTN_SQRT, OnBtnSqrt)
 62     //}}AFX_MSG_MAP
 63 END_MESSAGE_MAP()
 64 
 65 /////////////////////////////////////////////////////////////////////////////
 66 // CCalculatorDemoDlg message handlers
 67 
 68 void CCalculatorDemoDlg::OnBtnNo1() 
 69 {
 70     // TODO: Add your control notification handler code here
 71     PutIntoNum(1);
 72 }
 73 
 74 void CCalculatorDemoDlg::OnBtnNo2() 
 75 {
 76     // TODO: Add your control notification handler code here
 77     PutIntoNum(2);
 78 }
 79 
 80 void CCalculatorDemoDlg::OnBtnNo3() 
 81 {
 82     // TODO: Add your control notification handler code here
 83     PutIntoNum(3);
 84 }
 85 
 86 void CCalculatorDemoDlg::OnBtnNo4() 
 87 {
 88     // TODO: Add your control notification handler code here
 89     PutIntoNum(4);
 90 }
 91 
 92 void CCalculatorDemoDlg::OnBtnNo5() 
 93 {
 94     // TODO: Add your control notification handler code here
 95     PutIntoNum(5);
 96 }
 97 
 98 void CCalculatorDemoDlg::OnBtnNo6() 
 99 {
100     // TODO: Add your control notification handler code here
101     PutIntoNum(6);
102 }
103 
104 void CCalculatorDemoDlg::OnBtnNo7() 
105 {
106     // TODO: Add your control notification handler code here
107     PutIntoNum(7);
108 }
109 
110 void CCalculatorDemoDlg::OnBtnNo8() 
111 {
112     // TODO: Add your control notification handler code here
113     PutIntoNum(8);
114 }
115 
116 void CCalculatorDemoDlg::OnBtnNo9() 
117 {
118     // TODO: Add your control notification handler code here
119     PutIntoNum(9);
120 }
121 
122 void CCalculatorDemoDlg::OnBtnNo0() 
123 {
124     // TODO: Add your control notification handler code here
125     // TODO: Add your control notification handler code here
126     //如果第一次输入数字
127     if (cs_Num[m_i]=="0") 
128     {
129         return ;
130     }
131     cs_Num[m_i]+="0";
132     SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]);
133     if (!b_Hasport)
134     {
135         CString temp;
136         GetDlgItemText(IDC_EDIT_SHOW,temp);
137         temp+=".";
138         SetDlgItemText(IDC_EDIT_SHOW,temp);
139     }
140 }
141 
142 void CCalculatorDemoDlg::OnBtnPort() 
143 {
144     //已有小数点时
145     if (b_Hasport)
146     {
147         return;
148     }
149     //无小数点
150     cs_Num[m_i]+=".";
151     b_Hasport=TRUE;
152     b_Start=TRUE;
153     SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]);
154 }
155 
156 
157 //四则运算符
158 
159 void CCalculatorDemoDlg::OnBtnAdd() 
160 {
161     if (!b_Start)
162     {
163         return;
164     }
165     if (en_LastSignl==en_none)
166     {
167         en_LastSignl=en_add;
168     }
169     RunOperation(en_LastSignl);
170     en_LastSignl=en_add;
171 }
172 
173 void CCalculatorDemoDlg::OnBtnSub() 
174 {
175     if (!b_Start)
176     {
177         return;
178     }
179     
180     // TODO: Add your control notification handler code here
181     if (en_LastSignl==en_none)
182     {
183         en_LastSignl=en_sub;
184     }
185     RunOperation(en_LastSignl);
186     en_LastSignl=en_sub;
187 }
188 
189 void CCalculatorDemoDlg::OnBtnMul() 
190 {
191     if (!b_Start)
192     {
193         return;
194     }
195     if (en_LastSignl==en_none)
196     {
197         cs_Num[1]="1";
198         en_LastSignl=en_mul;
199     }
200     
201     RunOperation(en_LastSignl);
202     en_LastSignl=en_mul;
203 }
204 
205 void CCalculatorDemoDlg::OnBtnDiv() 
206 {
207     if (!b_Start)
208     {
209         return;
210     }
211     if (en_LastSignl==en_none)
212     {
213         cs_Num[1]="1";
214         en_LastSignl=en_div;
215     }
216     RunOperation(en_LastSignl);
217     en_LastSignl=en_div;
218 }
219 
220 void CCalculatorDemoDlg::OnBtnEquel() 
221 {
222     // TODO: Add your control notification handler code here
223     RunOperation(en_LastSignl);
224     //****特殊****
225     en_LastSignl=en_none;
226     b_Start=TRUE;
227 }
228 
229 BOOL CCalculatorDemoDlg::OnInitDialog() 
230 {
231     CDialog::OnInitDialog();
232     
233     // TODO: Add extra initialization here
234     InitAllDate();
235     return TRUE;  // return TRUE unless you set the focus to a control
236                   // EXCEPTION: OCX Property Pages should return FALSE
237 }
238 
239 void CCalculatorDemoDlg::PutIntoNum(int n)
240 {
241     CString tem_i;
242     tem_i.Format("%d",n);
243     CString temp;
244     //第一次输入
245     if (!b_Start)
246     {
247         cs_Num[m_i]=tem_i;
248         b_Start=TRUE;
249         SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]);
250         if (!b_Hasport)
251         {
252             GetDlgItemText(IDC_EDIT_SHOW,temp);
253             temp+=".";
254             SetDlgItemText(IDC_EDIT_SHOW,temp);
255         }
256         return;
257     }
258     cs_Num[m_i]+=tem_i;
259     SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]);
260     if (!b_Hasport)
261     {
262         GetDlgItemText(IDC_EDIT_SHOW,temp);
263         temp+=".";
264         SetDlgItemText(IDC_EDIT_SHOW,temp);
265     }
266 }
267 
268 void CCalculatorDemoDlg::InitAllDate()
269 {
270     en_LastSignl=en_none;
271     b_Hasport=FALSE;
272     b_Start=FALSE;
273     b_Hasmul=FALSE;
274     b_Hasdiv=FALSE;
275     cs_Num[0]="0";
276     cs_Num[1]="0";
277     m_i=0;
278     SetDlgItemText(IDC_EDIT_SHOW,cs_Num[m_i]+".");
279 }
280 
281 void CCalculatorDemoDlg::RunOperation(eum_signl e_signl)
282 {
283     // TODO: Add your control notification handler code here
284     double sum;
285     double nAdd1;
286     double nAdd2;
287     nAdd1=atof(cs_Num[0]);
288     nAdd2=atof(cs_Num[1]);
289 
290     //sum=nAdd1+nAdd2;
291     switch(e_signl)
292     {
293     case en_add:
294         sum=nAdd1+nAdd2;
295         break;
296     case en_sub:
297         sum=nAdd1-nAdd2;
298         break;
299     case en_mul:
300         sum=nAdd1*nAdd2;
301         break; 
302     case en_div:
303         sum=nAdd1/nAdd2;
304         break;
305     }
306     
307     cs_Num[0].Format("%g",sum);
308     SetDlgItemText(IDC_EDIT_SHOW,cs_Num[0]);
309     if (!b_Hasport && -1==cs_Num[0].Find('.'))
310     {
311         CString temp;
312         GetDlgItemText(IDC_EDIT_SHOW,temp);
313         temp+=".";
314         SetDlgItemText(IDC_EDIT_SHOW,temp);
315     }
316     m_i = 1;
317     b_Hasport=FALSE;
318     b_Start=FALSE;
319     cs_Num[m_i]="0";
320 }
321 
322 //void CCalculatorDemoDlg::RunOperation(eum_signl en_signl)
323 //{
324 
325 //}
326 
327 void CCalculatorDemoDlg::OnBtnC() 
328 {
329     // TODO: Add your control notification handler code here
330     InitAllDate();
331 }
332 
333 // 根号
334 void CCalculatorDemoDlg::OnBtnSqrt() 
335 {
336     // TODO: Add your control notification handler code here
337     if (!b_Start)
338     {
339         return;
340     }
341 
342     double num;
343     CString temp;
344     GetDlgItemText(IDC_EDIT_SHOW,temp);
345     num=atof(temp);
346     num=sqrt(num);
347     temp.Format("%lf",num);
348     if (temp.Find('.')==-1)
349         SetDlgItemText(IDC_EDIT_SHOW,temp+".");
350     else
351         SetDlgItemText(IDC_EDIT_SHOW,temp);
352 }
353 
354 
355 // %
356 void CCalculatorDemoDlg::OnBtnPercent() 
357 {
358     // TODO: Add your control notification handler code here
359     
360 }
361 
362 // 1/x
363 void CCalculatorDemoDlg::OnBtnDown() 
364 {
365     // TODO: Add your control notification handler code here
366     if (!b_Start)
367     {
368         return;
369     }
370     double num;
371     CString temp;
372     GetDlgItemText(IDC_EDIT_SHOW,temp);
373     num=atof(temp);
374     num=1/num;
375     temp.Format("%lf",num);
376     if (temp.Find('.')==-1)
377         SetDlgItemText(IDC_EDIT_SHOW,temp+".");
378     else
379         SetDlgItemText(IDC_EDIT_SHOW,temp);
380 }
381 
382 // +-
383 void CCalculatorDemoDlg::OnBtnNequa() 
384 {
385     // TODO: Add your control notification handler code here
386     if (!b_Start)
387     {
388         return;
389     }
390     double num;
391     CString temp;
392     GetDlgItemText(IDC_EDIT_SHOW,temp);
393     num=atof(temp);
394     num=0-num;
395     SetDlgItemText(IDC_EDIT_SHOW , b_Hasport ? temp:temp+".");
396 }
397 
398 // 退格键
399 void CCalculatorDemoDlg::OnBtnSpace() 
400 {
401     // TODO: Add your control notification handler code here
402     if (!b_Start)
403     {
404         return;
405     }
406     double num;
407     CString temp;
408     int len;
409     CHAR temp_ch[32];
410     GetDlgItemText(IDC_EDIT_SHOW,temp);
411     //AfxMessageBox(temp);
412     num=atof(temp);
413     sprintf(temp_ch,"%g",num);
414     num=0;
415     //AfxMessageBox(temp_ch);
416     len=strlen(temp_ch);
417     int te_i=len-1;
418     if(temp_ch[te_i]=='.')
419         te_i-=1,b_Hasport=FALSE;
420     temp_ch[te_i]='\0';
421     //////////////////////////////////////////////////////////////////////////
422     //AfxMessageBox(temp_ch);
423     if (strlen(temp_ch)==0)
424     {
425         temp_ch[0]='0';
426         temp_ch[1]='\0';
427     }
428     //////////////////////////////////////////////////////////////////////////
429     sscanf(temp_ch,"%lf",&num);
430     temp.Format("%g",num);
431     //AfxMessageBox(temp);
432     SetDlgItemText(IDC_EDIT_SHOW,b_Hasport?temp:temp+".");
433 }
CalculatorDemoDlg.h
 1 #if !defined(AFX_CALCULATORDEMODLG_H__6033594F_8916_49DF_B7FE_B09720CC9632__INCLUDED_)
 2 #define AFX_CALCULATORDEMODLG_H__6033594F_8916_49DF_B7FE_B09720CC9632__INCLUDED_
 3 
 4 #if _MSC_VER > 1000
 5 #pragma once
 6 #endif // _MSC_VER > 1000
 7 // CalculatorDemoDlg.h : header file
 8 //
 9 
10 /////////////////////////////////////////////////////////////////////////////
11 // CCalculatorDemoDlg dialog
12 //加减乘除四则运算
13 enum eum_signl {en_none,en_add,en_sub,en_mul,en_div};
14 
15 class CCalculatorDemoDlg : public CDialog
16 {
17 // Construction
18 public:
19     eum_signl en_LastSignl;
20     BOOL b_Hasdiv;
21     BOOL b_Hasmul;
22     void RunOperation(eum_signl en_signl);
23     void InitAllDate();
24     void PutIntoNum(int n);
25     BOOL b_Hasport;  //是否输入了小数点
26     BOOL b_Start;    //是否开始输入一个数据
27     int m_i;         //记录当前使用哪个CString
28     //存两个数据
29     CString cs_Num[2];
30 
31     CCalculatorDemoDlg(CWnd* pParent = NULL);   // standard constructor
32 
33 // Dialog Data
34     //{{AFX_DATA(CCalculatorDemoDlg)
35     enum { IDD = IDD_CALC_SLP_DLG };
36         // NOTE: the ClassWizard will add data members here
37     //}}AFX_DATA
38 
39 
40 // Overrides
41     // ClassWizard generated virtual function overrides
42     //{{AFX_VIRTUAL(CCalculatorDemoDlg)
43     protected:
44     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
45     //}}AFX_VIRTUAL
46 
47 // Implementation
48 protected:
49 
50     // Generated message map functions
51     //{{AFX_MSG(CCalculatorDemoDlg)
52     afx_msg void OnBtnNo1();
53     afx_msg void OnBtnNo2();
54     afx_msg void OnBtnNo3();
55     afx_msg void OnBtnNo4();
56     afx_msg void OnBtnNo5();
57     afx_msg void OnBtnNo6();
58     afx_msg void OnBtnNo7();
59     afx_msg void OnBtnNo8();
60     afx_msg void OnBtnNo9();
61     afx_msg void OnBtnNo0();
62     afx_msg void OnBtnPort();
63     afx_msg void OnBtnAdd();
64     afx_msg void OnBtnSub();
65     afx_msg void OnBtnMul();
66     afx_msg void OnBtnDiv();
67     afx_msg void OnBtnEquel();
68     virtual BOOL OnInitDialog();
69     afx_msg void OnBtnPercent();
70     afx_msg void OnBtnDown();
71     afx_msg void OnBtnNequa();
72     afx_msg void OnBtnSpace();
73     afx_msg void OnBtnC();
74     afx_msg void OnBtnSqrt();
75     //}}AFX_MSG
76     DECLARE_MESSAGE_MAP()
77 };
78 
79 //{{AFX_INSERT_LOCATION}}
80 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
81 
82 #endif // !defined(AFX_CALCULATORDEMODLG_H__6033594F_8916_49DF_B7FE_B09720CC9632__INCLUDED_)
posted @ 2012-04-17 22:28  r3call  阅读(419)  评论(0编辑  收藏  举报