(MFC) 계산기 프로그램 작성 I
Calculator Program 1
[MFC] 1
- 최초 작성일: 2022년 2월 25일 (금)
##
사칙 연산과 CString 함수의 형 변환을 이용하여 계산기 프로그램을 작성한다.
###
- 프로젝트명: ‘Calc’
- 응용 프로그램 종류: ‘대화 상자 기반’
###
###
아래와 같이, [클래스 뷰]-[Calc]-[CCalcDlg]에서 [추가]-[변수 추가]를 클릭하고 멤버 변수를 추가한다.
그리고 클래스 마법사를 실행시키고, [멤버 변수] 탭에서 아래와 같이 멤버 변수들을 추가한다.
또한, 클래스 마법사에서 다음과 같이 버튼 클릭시에 대한 멤버 함수를 추가하고, 코드를 작성하자.
###
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
void CCalcDlg::OnClickedButtonAdd()
{
// TODO: Add your control notification handler code here
m_nOption = 1;
}
void CCalcDlg::OnClickedButtonSub()
{
// TODO: Add your control notification handler code here
m_nOption = 2;
}
void CCalcDlg::OnClickedButtonMul()
{
// TODO: Add your control notification handler code here
m_nOption = 3;
}
void CCalcDlg::OnClickedButtonDiv()
{
// TODO: Add your control notification handler code here
m_nOption = 4;
}
void CCalcDlg::OnClickedButtonEqu()
{
// TODO: Add your control notification handler code here
char temp[10];
double tmpResult;
UpdateData(TRUE);
switch (m_nOption)
{
case 1:
tmpResult = atof(m_nNum1) + atof(m_nNum2); // [프로젝트 속성]-[구성 속성]-[고급]-[문자 집합]-[멀티바이트 문자 집합 사용] 안하면 에러뜸.
break;
case 2:
tmpResult = atof(m_nNum1) - atof(m_nNum2);
break;
case 3:
tmpResult = atof(m_nNum1) * atof(m_nNum2);
break;
case 4:
tmpResult = atof(m_nNum1) / atof(m_nNum2);
break;
default:
tmpResult = 0.00;
break;
}
sprintf_s(temp, "%2.f", tmpResult);
m_nResult = temp;
UpdateData(FALSE);
}
void CCalcDlg::OnClickedButtonClear()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_nNum1 = L"";
m_nNum2 = _T("");
m_nResult = _T("");
UpdateData(FALSE);
}
void CCalcDlg::OnClickedButtonExit()
{
// TODO: Add your control notification handler code here
PostQuitMessage(0);
}
###
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.










