Four arithmetic operations, simple arithmetic questions (for example, 1*2 + 3/3*4 + 8)

Source: Internet
Author: User

1. Simple arithmeticAlgorithm: For example, 1X2 + 3/3x4 + 8

The arithmetic above is relatively simple, with no parentheses. It is just a general operation. Now the only thing we need to do is to figure out the priority. In addition to the data structure, we can implement the stack assistant.

 

The basic algorithm is as follows: there must be two stacks, one for data storage, and the other for operators. If there are numbers, they should be placed in the data stack. If they are operators, the priority needs to be compared, if the priority is lower than that of the operator at the top of the current stack, It is pushed into the stack. If the priority is higher than that of the operator at the top of the stack, the data on both sides of the symbol must be calculated to obtain the result, place the result in the data stack, and the top symbol of the operator remains unchanged. For example, if the operator is lower than the top operator of the stack, You need to calculate the two numbers at the end of the data stack, in this way, the stack priority in the final operator is the same, and the data stack is directly read in sequence. The operator stack can be computed from the beginning.

 

2. complicated arithmetic questions 1*(2 + 3)/(3*4 + 8)

The basic principle is the same as above. In sequence, the values in the brackets are calculated and pushed to the data stack.

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.