Evaluation and validation of suffix expressions

Source: Internet
Author: User
Tags class definition integer integer numbers


This program is a complete suffix expression calculation, mainly with the operation of the stack, this program encapsulates the Cstack class to implement the operation of the stack, the most characteristic of this procedure is the use of dynamic monitoring expression of the algorithm to the expression of data validation, all the legal expressions to calculate, check out any illegal expression and hint.

Keywords: suffix expression, checksum

Title: Suffix expression evaluation.

Requirement: input suffix expression, input as Integer and arithmetic, output calculation result.

For example:

Input: 2 3 * 1-

Output: 5

Analysis: 2*3-1=5

Input: 1 2 + 5 4 * 3-* 6-

Output: 45

Analysis: (1+2) * (5*4-3) -6=45

Algorithm Analysis:

Suffix expressions, in contrast to ordinary expressions, have the advantage of not needing parentheses. So the convenience of computer processing, usually for the suffix expression to use the stack data results to achieve, scan the expression from left to right, if you encounter a number, then put the number into the stack, if you encounter the operation symbol, then extract the stack of 2 elements to calculate, say the calculation results of the stack, if the last stack only a number, This is the result of the suffix expression's calculation. On this basis, considering the verification of the suffix expression, according to the characteristics of the suffix expression calculation, some functions have been written to verify the suffix expression, which should be able to check out any error suffix expressions and prompt for errors, set some states in the program, and stop the calculation process once the error state occurs. Correct the error immediately.

Overall design:

The programming environment is VC6.0, new MFC Project, choose as dialog box, because the expression is entered through a text box, so in the program, there must be a global function to convert character numbers to integer numbers. The most important function is the computation of that function, specifically the following oncalculate () function or source code. This program uses the data structure of the stack, so customize a cstack class, class definition, and implementation part of the code can see the following code. In order to complete the verification, another write a Function.h, in which the check function is defined, verify that the input expression contains illegal data, Trimblank function, remove the extra space in the expression, getop function, get the number of operation symbols in the expression, see the source code. In the main function, the test of the idea is to first judge the expression in the time contains illegal characters, if not, then further determine whether the number of numbers in the expression and the number of symbols to meet the "number of numbers -1= symbol number", if satisfied, In the calculation process, monitoring in the encounter with the operation of symbols in the stack of elements less than 2, this step is very important, every time the calculation is checked whether the conditions can be calculated, once the error, immediately break, so that even if there is a wrong input, Windows will not complain and cause the program to end abnormally, That is, the dynamic implementation of the tracking expression of the computer, and finally, check the number of pressure stacks and the number of stacks to meet a formula (specifically I forget, push myself) can determine the validity of the expression.

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.