Experience summary of simple calculator using ivx

Today I want to talk about using ivx to make a calculator that can perform simple addition, subtraction, multiplication and division. The design idea is to save the entire input expression into a text variable, and use the eval () direct method to calculate the value of the expression in the text variable. , Let me talk about specific steps.
Experience summary of simple calculator using ivx
1. The upper part of the counter interface is used to display the input calculation formulas and calculation results. The following uses the text component to display 16 keys. These 16 keys can be divided into four categories: numeric keys, operator keys, equal keys, and clear keys. The button keys and operator keys are similar to the operation of their similar keys. We can write it as an action group. When the button is clicked, the action group is called and the text component of the key is passed to the action group.
Experience summary of simple calculator using ivx
2. Numeric keys When you
click on the numeric keys, you only need to add the content of the key to the end of the calculation formula by using a concatenated string, but because the calculation formula "09 + 08" cannot be calculated in the eval () method, so We want to avoid the situation where the first digit of the value is 0. In order to make a mark, we add a Boolean variable "last operator" in the demo. When the calculator is in the initial state, that is, the value of the calculation formula is null, we make it true. In the calculation, when the calculation is the last one False when the digit is a number, true when the last digit is an operator. In this way, when the "last operator" is true, it means that we are inputting the first digit of the value at this time. Only the input value other than 0 is added to the end of the calculation formula, and then the "last operator" is set to true. , Otherwise a prompt.
Experience summary of simple calculator using ivx
3. Operators
There are several situations to consider when entering operators. The first is the case where the calculation formula is not empty. At this time, if the "last operator" is true, it indicates that the end of the calculation formula is already an operator, and we will delete it and replace it with a new input operator. The practice is to intercept the calculation The part other than the last digit of the formula is spliced ​​with the newly entered operator; if the "last operator" is false, it means that the end of the calculation formula is a numeric value, and the newly entered operator is added directly at the end of the calculation formula and the "last digit" The operator can be set to true. In addition, the calculator is in the initial state. When the calculation formula is empty, it is meaningless to input the three operators "+", "*", and "/", so only add it when the input operator is "-" In the calculation formula, in other cases, the user is prompted.
Experience summary of simple calculator using ivx
4. Equal to key
When clicking equal to key, we use the eval () method to perform numerical calculation on the content of the calculation formula. However, it is necessary to consider the case where the last input content of the calculation formula is an operator. In this case, the value of the "end operator" will be true. We intercept the original calculation formula and discard the invalid operator of the last digit. In addition Also reset the "end operator" to false.
Experience summary of simple calculator using ivx
5. Clear key The
clear key event is relatively simple. Clear the content of the calculation formula, return the answer to 0, and reset the "last operator" to true.
Experience summary of simple calculator using ivx
Summary In the
demo, only simple addition, subtraction, multiplication, and division are realized. More complicated calculation logic must be considered to realize more situations, and more judgment must be made on the input content. However, the most important thing is to classify the buttons. Those who can use the same set of actions are implemented by action groups. After all, the write events of a button and a button will increase not only generate a lot of redundant code, but also easy to make mistakes.

Guess you like

Origin blog.51cto.com/14556317/2486055