Simple calculator based on MFC application in Visual Studio

 Welcome to my recent blog post where I shared a simple calculator based on C++.

Github - 832101313BOYicon-default.png?t=N7T8https://github.com/832101313BOY/832101313zhangboyi/commit/4a0e171fe06d32e8ebfdd1090852efc20e56d715?diff=unified

The link of my classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentCreate a calculator with a visual interface
MU STU ID and FZU STU IDMU: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)

Planning3050
Estimate3050
Development410500
Analysis3040
Design Spec1525
Design Review2530
Coding Standard1515
Design5055
Coding170180
Code Review7080
Test6080
Reporting8080
Test Repor2020
Size Measurement2020
Postmortem & Process Improvement Plan2020
Sum10451245

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值