Everyone has their own programming language hobby, each language has its own advantages also have their own shortcomings, can be sure that each language is only to learn the depth of the language after the proficiency of the operation, relatively more like C + +. This article is a calculator based on C + + and MFC (Figure 1 below), and it's also important to use a convenient and efficient IDE development environment, Now a lot of books, including our learning time is to use VC6.0, do not deny that VC6.0 is very classic, but after using VS2010 believe you will not be disappointed, in addition to recommend a plug-in visual Assist X, she automatically completes the function is really let people fondle admiringly.
(Figure 1)
MFC for the first time to write the program, the beginning of the time did not really think, looking at this based on the dialog style MFC program, do not know how to deal with those buttons and controls. The information on the Internet is very rich, looking for a few examples, but they did not give the idea to write and function is not comprehensive, so it is very easy to let people can not really grasp the idea of writing, so, in reference to the predecessors, based on their own methods to give out:
Objective:
Let's talk about your own tools. Visual Studio Ultimate English (with Visual Assist X). Here's a quick way to install visual Assist x select "Tools" in the menu bar, then Extension Manager, select online Gallery in the left column of Extension Manager, and then wait to load information from the network. You can see visual Assist x by dragging the scroll bar, or you can search for visual Assist x directly, and download the installation.
This program is based on MFC application Warzid, choose Dialog Style Dialog mode, select Dialog baesd when choosing Application type in the second step, and then advanced One of the features is support restart Manager, which allows you to maximize the state of an application when it suddenly terminates, a feature we can feel in applications such as word, when a Word document crashes, Again, the operation of the document will be resumed, which is optional or optional, does not affect the main functions of this program; Other details can be selected according to the Reading Wizard. Project name is Calcex, and the following is the automatically generated Class View:
Describe the members of the added Ccalcexdlg class:
CString M_show:edit Control is a member variable that is used with display input and results. Add by: Right click on Edit control, select Add Variable ...
CString M_NUM1:CCALCEXDLG member variable, for storing the first argument
CString M_NUM2:CCALCEXDLG member variable, for storing the first argument
The member variable of int M_operta:ccalcexdlg, which holds the operator, defaults to 0, addition to 1, subtraction to 2 etc.
The member variable of the int M_opertatemp:ccalcexdlg, which is used to store the previous operator and to compute the number of operands
Body:
1, we know that each button when you double-click it, will automatically jump to its corresponding processing function code, so if we want to press a button when we want to do some action, we simply add the appropriate code in its processing function. ----This shows how we should treat a Button/contrl: They are an object, they can have their own data members, or they can have their own member functions, you should take the object-oriented point of view in C + + to treat these buttons and controls.
2. Think about the process we use calculator: such as a+b=/a+b+c-d=. When only two numbers are added, we can take the binocular operator (+-*/) as the operator of the operands A and B, we need to get the operands first, and then give the result when we press =; This requires that each operator be pressed to check to see if the operand that was entered has been computed, and I used the following: Add a m_opertatemp variable to the Ccalcexdlg class and check if the m_opertatemp is 0 without the presence of = If 0 is explained before the operation has been carried out, otherwise the first execution of the operation, and then perform the following calculation, the code is as follows (for example):
This is the handler function for the number 1 button. void Ccalcexdlg::onbnclickedbutton11 () { Todo:add your control notification handler code here if (m_show!= ' 0 ') m_show+= ' 1 '; Else m_show= ' 1 '; UpdateData (FALSE); } |
This is the processing function for the + button void Ccalcexdlg::onbnclickedbutton1 () { Todo:add your control notification handler code here if (m_opertatemp)//check if has been proceeded { M_num2=m_show; Store current data to m_num2 to procedure Algrithm (m_opertatemp); Perform arithmetic } M_operator=1; M_opertatemp=m_operator; M_num1=m_show; m_show= ' 0 '; } |
3, how to display the input data and calculation results
The CString class is a useful class that we can use _tstof (CString) to convert a string to a double, or a double data to a string using Cstring::format ():
void Ccalcexdlg::algrithm (int operta)
{
Double TEMP1,TEMP2;
Double temp=0;
Temp1=_tstof (M_NUM1);
Temp2=_tstof (M_NUM2);
Switch (Operta)
{
Case 1:
TEMP=TEMP1+TEMP2;
Break
Case 2:
TEMP=TEMP1-TEMP2;
Break
Case 3:
TEMP=TEMP1*TEMP2;
Break
Case 4:
TEMP=TEMP1/TEMP2;
Break
Case 5:
Temp=cos (TEMP1);
Break
Case 6:
Temp=sin (TEMP1);
Break
Case 7:
Temp=tan (TEMP1);
Break
Case 8:
Temp=log (TEMP1); Log with E
Break
Case 9:
TEMP=LOG10 (TEMP1); Log with 10
Break
Case 10:
temp=1.0;
for (; temp2>0.0;--temp2)
{
TEMP*=TEMP1;
}
Break
Case 11:
Temp=1;
for (; temp1>0;--temp1)
{
TEMP*=TEMP1;
}
Default:break;
}
if (temp-(int) temp==0)
M_show. Format (_t ("%.0f"), temp);
Else
M_show. Format (_t ("%.6f"), temp);
UpdateData (FALSE);
M_opertatemp=0; Reset the m_opertatemp for the next calculate
}
4, the realization of the BACKSPACE: Cstring::left ()
void Ccalcexdlg::onbnclickedbutton22 ()
{
Todo:add your control notification handler code here
M_show=m_show. Left (m_show. GetLength ()-1); Extract the value on the left and subtract its length by 1
if (m_show== "")
{
m_show= ' 0 ';
}
UpdateData (FALSE);
}
5, the other is UpdateData (false) and UpdateData ()
UpdateData (false) does not display the data you entered into edit control, showing only the value of the data member m_show of edit control;
UpdateData () =updatedata (TRUE) shows the data you entered into edit control, not the value of the data member of the edit control itself.
6, the program generation
Sometimes we run the program we write on our own machine to run normally, but as soon as we get someone on the machine, we will be prompted that a DLL file is missing, because the application we generated is the use of MFC in a Shared DLL, we just change the option to MFC in a Static Library "on the line." The specific method is: Menu Project---> Select this Item property---->configuration Properties--->general right use the MFC option.
All right, so here's the key, and there's a better way to share it with you:
Source code Download (release version of):
http://download.csdn.net/source/2733548