MFC-based calculators with four mixed operations

Source: Internet
Author: User
Tags bmp image

Today, inadvertently found Win7 system standard calculator Even the most basic four mixed operation did not do, just a good company gave me a job, is to use MFC to implement a calculator with four mixed operation .

I check the information on the Internet, found that most of the implementation of the basic subtraction operations, and four mixed operations are not able to speak clearly. So I searched the four hybrid algorithm, found that to realize the four hybrid operation, we should use the inverse Polish algorithm , and the inverse Polish algorithm, we must first convert the arithmetic from infix expression to the suffix expression .

The so-called infix expression is our usual arithmetic, for example: 1+2-3*4/5.

The suffix expression, after the operator is written to the operand, is prefixed with the following arithmetic: 12+34*5/-. How does it come about, let's look at the suffix expression algorithm:

1) first constructs an operator stack, which follows the principle that the higher the top priority is in the stack;

2) read a simple arithmetic expression denoted by infix, for convenience, set the right end of the simple arithmetic expression plus the special symbol "#" with the lowest priority;

3) scan the arithmetic expression from left to right, starting from the first character, if the character is a number, parse to the end of the number string and output the string directly;

4) If it is not a number, the character is an operator, at which point the precedence relationship should be compared as follows:

Compares this character to the precedence of the operator at the top of the operator stack. If the character precedence is higher than the operator at the top of this operator stack, the operator is put into the stack. If not, the top operator is popped out of the stack until the top of the stack operator is lower than the current operator and the character is placed on the stack.

5) Repeat the above operation (1)-(2) until the complete simple arithmetic expression is scanned, to make sure that all characters are handled correctly, we can convert the simple arithmetic expression of infix notation into a simple arithmetic expression of inverse polish representation.

Maybe a lot of people do not understand this process, and we will show it to you with 1+2-3*4/5 as an example.

We use vector capacity to install suffix expressions and build a stack to load operators. Traversing arithmetic, encountered the number "1", pressed into the vector, encountered "+", directly into the stack, encountered "2", pressed into the vector, encountered "-", "-" the priority is not higher than "+", "+" Out of the stack, pressed into the vector, "-" into the stack; , the priority of "*" is higher than "-", into the stack, encountered "4", pressed into the vector, encountered "/", "/" priority is not higher than "*", "*" Out of the stack, pressing into the vector, at this time the stack top for "-", "/" Priority than "-" high, into the stack, encountered "5", pushed into the vector. The traversal completes, at this time the vector is {12+34*}, the operator stack is {-/}, the contents of the operator stack are pressed into the vector, because the stack is last-first out, so the content of the vector after the input is {12+34*5/-}. This completes the suffix expression.

The inverse Polish algorithm is described below. Referring to the above example, the converted suffix expression is 12+34*5/-, constructs a new stack, iterates over the suffix expression, encounters the number directly into the stack, encounters the operator, puts the stack top 2 numbers out to perform the operation, and the final result is traversed. "1" "2" into the stack, encountered "+", "1" "2" to take out the + operation, the result is "3", put "3" into the stack, continue to traverse, "3" into the Stack, "4" into the stack, the stack is "3" "3" "4", encountered "*", "3" "4" to take out the * operation, the result is "12", put "12 "The Stack is" 3 "" 12 "in this case, continue to traverse," 5 "into the stack, encountered"/"," 12 "" 5 "out of the run/operation, the result is" 2.4 ", the" 2.4 "into the stack, encountered"-"," 3 "" 2.4 "out of the-operation, the result is" 0.6 ", the" 0.6 "into the stack, The traversal is completed and the final result is "0.6". This is the process of reversing the Polish algorithm.

Below, we formally use MFC to implement four mixed operation calculator.

To create a new project, select MFC, select an MFC application, enter a name, and click Next.

Click Next, select Based on the dialog box, always click Next, complete;

  

After the completion of the project, the interface is designed to look like a calculator;

  

Add variables to 2 edit boxes, one input arithmetic, one output, double click on each key, add input function, so the basic frame has been set and started to write the program.

First add header file # include <vector>, #include <stack>, and using namespace Std.

The operatorenable is initialized first, BOOL operatorenable = FALSE.

This is the program for my header file:

  

The number of keys to the program as follows, 0 to 9 of the program, the number of changes:

  

operator, like a program, changes the operator:

  

Clear the key program as follows:

  

The procedure for the equal sign key is as follows:

  

  

The procedure for converting a suffix expression is as follows:

  

  

  

The inverse Polish algorithm process procedure is as follows:

  

  

  

The procedure for judging operator precedence is as follows:

  

By this, the program has been completed. You can implement four blending operations. The following tests are performed:

Enter a random arithmetic, press the equal sign, output the results, and then perform calculations on the computer calculator to get the results compared.

    

  

Well, MFC-based calculators with four mixed operations have been completed, and this implementation process has made me more aware of MFC. Next, I will write an MFC-based monochrome BMP image production software.

  

  

MFC-based calculators with four mixed operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.