Welcome to my recent blog post where I shared a simple calculator based on C++.
Github - 832101313BOYhttps://github.com/832101313BOY/832101313zhangboyi/commit/4a0e171fe06d32e8ebfdd1090852efc20e56d715?diff=unified
The link of my class | https://bbs.csdn.net/forums/ssynkqtd-04 |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617332156 |
The Aim of This Assignment | Create a calculator with a visual interface |
MU STU ID and FZU STU ID | MU:21125571 FZU:832101313 |
1. Introduction
This project uses the MFC application module in Visual Studio to realize the basic calculator function in the local side MFC interface window based on C++ language.
This simple calculator can perform the basic operations of addition, subtraction, multiplication and division.
2. PSP(Personal Software Process) Form
Personal Software Process Stages | Estimated Time (minutes) | Actual Time (minutes) |
Planning | 30 | 50 |
Estimate | 30 | 50 |
Development | 410 | 500 |
Analysis | 30 | 40 |
Design Spec | 15 | 25 |
Design Review | 25 | 30 |
Coding Standard | 15 | 15 |
Design | 50 | 55 |
Coding | 170 | 180 |
Code Review | 70 | 80 |
Test | 60 | 80 |
Reporting | 80 | 80 |
Test Repor | 20 | 20 |
Size Measurement | 20 | 20 |
Postmortem & Process Improvement Plan | 20 | 20 |
Sum | 1045 | 1245 |
3. Description of Problem-Solving Ideas
3.1 Initial Approach to Problem-Solving Ideas
First I consider the basic functions of a simple calculator. I decided to create a visual page to show the calculator, including addition, subtraction, multiplication, and division.
I decided to use the MFC application in visual studio to implement the project, because it just happens to be a good connection between the locale and the page, and it is easier to create.
3.2 Access to Information and Research
To better understand the MFC application development process, I read about MFC and looked at sample code and tutorials to better understand how to build visual pages and implement their functionality.
4. Design and Implementation Process
First of all, the size of each button on the interface should be designed to be reasonable. Then through C++ language, we assign the corresponding code for each button to display its number or symbol in the interface, and complete the operation and display the result under C++'s own operation logic. Writing multiple function keys at the same time, such as delete key, return to zero key, etc., realizes the function and demand of basic calculator.
Flow chart:
5. Code Description
* CcalculaterDlg dialog creation and message processing
CcalculaterDlg::CcalculaterDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_CALCULATER_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CcalculaterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CcalculaterDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON17, &CcalculaterDlg::OnBnClickedButton17)
ON_BN_CLICKED(IDC_BUTTON1, &CcalculaterDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON13, &CcalculaterDlg::OnBnClickedButton13)
ON_BN_CLICKED(IDC_BUTTON14, &CcalculaterDlg::OnBnClickedButton14)
ON_BN_CLICKED(IDC_BUTTON15, &CcalculaterDlg::OnBnClickedButton15)
ON_BN_CLICKED(IDC_BUTTON16, &CcalculaterDlg::OnBnClickedButton16)
ON_BN_CLICKED(IDC_BUTTON12, &CcalculaterDlg::OnBnClickedButton12)
ON_BN_CLICKED(IDC_BUTTON2, &CcalculaterDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CcalculaterDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, &CcalculaterDlg::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, &CcalculaterDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, &CcalculaterDlg::OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON7, &CcalculaterDlg::OnBnClickedButton7)
ON_BN_CLICKED(IDC_BUTTON8, &CcalculaterDlg::OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON9, &CcalculaterDlg::OnBnClickedButton9)
ON_BN_CLICKED(IDC_BUTTON10, &CcalculaterDlg::OnBnClickedButton10)
ON_BN_CLICKED(IDC_BUTTON11, &CcalculaterDlg::OnBnClickedButton11)
END_MESSAGE_MAP()
BOOL CcalculaterDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
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);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
* Draw charts and minimize page Windows
void CcalculaterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
int 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;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
HCURSOR CcalculaterDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
* Define numbers and symbols and enter functions
void CcalculaterDlg::input(char* a)
{
GetDlgItemText(IDC_EDIT2, str2);
if (str2 == _T(""))
{
str1 += LPCTSTR(a);
number1 = _ttof(str1);
SetDlgItemText(IDC_EDIT1, str1);
}
else
{
str3 += LPCTSTR(a);
number2 = _ttof(str3);
SetDlgItemText(IDC_EDIT3, str3);
}
}
void CcalculaterDlg::OnBnClickedButton17()
{
GetDlgItemText(IDC_EDIT1, str1);
GetDlgItemText(IDC_EDIT2, str2);
GetDlgItemText(IDC_EDIT3, str3);
number1 = _ttof(str1);
number2 = _ttof(str3);
if (str2 != _T(""))
{
if (str2 == _T("+"))
{
result = number1 + number2;
}
else if (str2 == _T("-"))
{
result = number1 - number2;
}
else if (str2 == _T("*"))
{
result = number1 * number2;
}
else if (str2 == _T("/"))
{
result = number1 / number2;
}
else
{
MessageBox(_T("运算符输入有误!"));
}
str.Format(_T("%g"), result);
SetDlgItemText(IDC_EDIT4, str);
void CcalculaterDlg::OnBnClickedButton13()
{
str2 = _T("+");
SetDlgItemText(IDC_EDIT2, str2);
}
void CcalculaterDlg::OnBnClickedButton14()
{
str2 = _T("-");
SetDlgItemText(IDC_EDIT2, str2);
}
void CcalculaterDlg::OnBnClickedButton15()
str2 = _T("*");
SetDlgItemText(IDC_EDIT2, str2)
}
void CcalculaterDlg::OnBnClickedButton16()
{
str2 = _T("/");
SetDlgItemText(IDC_EDIT2, str2);
}
void CcalculaterDlg::OnBnClickedButton12()
{
str1 = _T("");
str2 = _T("");
str3 = _T("");
str = _T("");
SetDlgItemText(IDC_EDIT1, str1);
SetDlgItemText(IDC_EDIT2, str2);
SetDlgItemText(IDC_EDIT3, str3);
SetDlgItemText(IDC_EDIT4, str);
}
void CcalculaterDlg::OnBnClickedButton1()
{
input("1");
}
void CcalculaterDlg::OnBnClickedButton2()
{
input("2");
}
void CcalculaterDlg::OnBnClickedButton3()
{
input("3");
}
void CcalculaterDlg::OnBnClickedButton4()
{
input("4");
}
void CcalculaterDlg::OnBnClickedButton5()
{
input("5");
}
void CcalculaterDlg::OnBnClickedButton6()
{
input("6");
}
void CcalculaterDlg::OnBnClickedButton7()
{
input("7");
void CcalculaterDlg::OnBnClickedButton8()
{
input("8");
}
void CcalculaterDlg::OnBnClickedButton9()
{
input("9");
}
void CcalculaterDlg::OnBnClickedButton10()
{
input("0");
}
void CcalculaterDlg::OnBnClickedButton11()
{
input(".");
}
6. Displaying Result
Calculator
7. Summary
Through the production of this basic visual calculator, I understand the specific process of how to make a complete software and understand the basic knowledge required for software development. However, the calculator I made for the first time still had many shortcomings. For example, I can design a scientific calculator with more complete functions, which can be gradually improved in the subsequent learning process. I learned how to create a user interface and exercised my coding skills.