C # calculator--1 for Imitation Query Analyzer. Preliminary analysis

Source: Internet
Author: User

Calculator is a lot of programming beginners have done, starting from the simplest console program, input values, input operators, and then enter a value, to get a result. The interface is basically modeled on the Windows operating system calculator, through the button input values and operators, and then give the results of the operation. Can you directly enter an expression, or you can calculate several expressions, or select a part of an expression to calculate, if the input error can indicate the wrong position in the expression? Such requirements are similar to MS SQL's Query Analyzer, and this series of articles describes how to implement such a calculator. (These content has been involved in the compiler principle, but I am not a computer professional graduate, just a little look at the compilation of the principle of information, professional vocabulary such as finite state machine and so on do not mention, we are not very pro, hehe. )

First we give an expression 5*3+12/4 and we get this expression and we get the following analysis:

1. Multiplication Priority high after the addition, first calculate multiplication, multiplication on both sides of the number is 5 and 3, perform multiplication 5*3=15

2. The priority of addition is not the following division priority high, first Calculate division, division on both sides of the number is 12 and 4, perform Division 12/4=3

3. Multiplication and division after doing the addition, this time the addition does not need to know how to calculate on both sides, just need to know the value on both sides of the calculation can be, the multiplication of the value is 15, the value is 3, the implementation of addition 15+3=18

Add one more step after this expression: 5*3+12/4-8, it's just one more step.

4. After adding to do subtraction, the minus sign does not need to know how to calculate on both sides, to the two sides of the request value, the value of the addition is 18, the other side is a number 8, do not need to calculate the direct to it, the implementation of subtraction 18-8=10

Change it a little bit: 5*3+12/4-8/2, the computational process is

4. Do addition preparation to do subtraction, but the next Division priority high, first do Division, division on both sides of the number is 8 and 2, to perform division to get 4

5. Then perform subtraction, subtraction on the left is performed after the addition of the value 18, the right is the division after the execution of the value of 4, perform subtraction 18-4=14

It will be found that each operator can be interpreted as a numeric storage unit, and that the unit can be calculated according to the values on both sides of it to get its own value. It only cares about the values on both sides, and does not need to worry about what algorithm the value is derived from. This will have the taste of two fork tree, a node can have two sub nodes below. So, no matter how complicated the expression is, there is only one result, and the result is that the last operator calculates the value on both sides of it, and the last operator is understood as the root node. Below, the expression is decomposed according to the shape of the tree:

Subtraction here is the root node, which requests a value and calculates it. Its subordinates also use the same method to request a value down, the request to the value of the execution of their own calculations, the results of the calculation stored in their own storage unit, and then let the upper level call their own values. Finally, the root node calculates the value of its subordinate's worth to the entire expression. The final computed tree structure is as follows:

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.