The implementation algorithm of the calculator supporting arithmetic

Source: Internet
Author: User
Tags arithmetic

1. Implementation algorithm of arithmetic

A. For an input expression, find the position in the expression "*", "/", "+", "-" in the expression, respectively.

B. If "*", "/" symbol exists, then the position of the preceding operator is the currently selected operator.

C. If "*", "/" symbol does not exist, then the position of the previous "+" or "-" is the currently selected operator.

D. Traverse to the left of the selected operator, truncate the left operand, encounter the new operator or the first character of the expression, and the traversal ends. Get the left operand and note the start position of the left operand.

E. Similarly, get the right operand and note the end position of the right operand.

F. Insert the left and right operands into the result of the operation, inserting it back into the original expression (between the start position of the left operand and the end position of the closing operand)

G. Return, continue to be evaluated in the same order until the current expression does not contain the subtraction operator, and the operation is considered to be complete.

2. Input response of the operation symbol (+,-,*,/)

A. The last character of the current input string if it is not a number, the last character is replaced with the current character.

3. Input response to decimal point

A. Starting from the end of the current input string to obtain the current operating factor, if the current operation factor is empty, then the input string after the concatenation of "0.";

B. The current operation factor is not NULL, but the current factor already contains a decimal point, ignoring the current decimal point;

C. The current operating factor is not empty and does not contain a decimal point, then a decimal point is added after the input string.

4. Response of the Equals sign

A. Calculate the final result, not the number (decimal point or Operation symbol), remove the last character;

5. Response of Numeric keys

A. The last valid input is "=", the last calculation should be emptied first, and a new round of calculations should be started.

6. Javascript floating point calculation accuracy problem

A. 0.2 + 0.1 result not equal to 0.3. The reason is related to the computer's coding rules for floating-point numbers, the principle of computer composition refers to this problem, the specific details forget.

B. Workaround: First, the operand of the participating operation is promoted to an integer, the operation, and then the lower.

C. 0.2-> 2, 0.1-1, 3-0.3

Program Address: http://liujiangyi123.oschina.io/calculator-v2.0/

The implementation algorithm of the calculator supporting arithmetic

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.