Calculator for Android Application Analysis-understanding of core computing and high-precision computing

Source: Internet
Author: User
Tags mathematical functions

Many people think Android calculators are very powerful, because they not only implement the basic computing implemented by General computers. The Calculation of expressions such as "(6 + 3) * 3/5-34 + (3-5)" is implemented, which enables many primary school students to perform multiplier arithmetic operations, you can use this calculator to get the result at a time, greatly reducing the time overhead of the job.

1. core computing of Android Calculator

Because the calculation result is carried out by pressing the = key on the interface. Processing the event is placed in eventlistener. java. In onclick () Click Event, we findCodePortal.

Eventlistener. Java:

Logic. Java:


The jar package of this Arity is stored in the caculator directory. Version 1.3.3:

Google Code address of Arity:

Http://code.google.com/p/arity/

2. Android calculator achieves high-precision expression Calculation

Because caculator currently does not support high-precision expression operations, it performs approximate processing for large numbers (including integers and decimals). If Android supports high-precision number operations, it will be more perfect. Based on this, add support for high-precision expression operations for caculator. One method is to download the source code package of Arity for modification and add support for high-precision rewrite operations. However, because the source code of Arity is not well understood and Its part of expression calculation is not found, Arity is suspected to hand over this part to a simple Virtual Machine for processing.

Another method is to implement the expression operation without using Arity. Considering that this part of implementation is not too complicated, you can decide to implement it by yourself.

High-precision calculation is mainly to implement two numbers (these two numbers are represented by arrays, and can also be represented by strings in Android or Java, because the string length can be determined by the memory, that is to say, it can be very long) manual addition, subtraction, multiplication, division operations. The support and calculation of "negative", "Parentheses", and "floating point number" must be taken into account.

After I implemented the addition, subtraction, and multiplication operations (for the high-precision operations I implemented, see the operation class ), I have no intention of discovering that Java actually provides bigdecimal classes for high-precision support (I have to say that this is Java's advantage, and a higher degree of large-scale encapsulation reduces a lotProgramWorkload. In most cases, C and C ++ have to be written by yourself ). It is easier. Our main task is to transfer to the operation part of the Implementation expression.

An important part of expression operations is to judge whether the expression is correct or not. This involves more complex syntax analysis of this method. Because it is not a compiler. Simply, we can solve the problem with exception handling.

The process of expression calculation depends on a stack data structure. The operation of expressions is completed through the entry and exit of operators and operators. Here, I did not directly use the data structure of the stack. Instead, recursion is used to hand over the process of warehouse receiving and warehouse receiving to the computer for management. The advantage is that the code can be written more easily and read better. Of course, recursion sacrifices a little time and space, but this is also very important for the current computer computing capabilities, better code readability and reusability.

Use the expression (1 + 2) * (3-4) as an example to illustrate the main steps of expression calculation:

(1 + 2) * (3-4) enter the recursive function:

Procedure

Expression

1. Remove unnecessary parentheses in the expression.

(1 + 2) * (3-4)

2. Find the operator with the lowest priority. Perform recursive operations on the left and right sides of the operator. Then, the operation result is operated according to the operator.

Here, the lowest priority symbol is *, because + and minus are in brackets, and the priority is higher.

3. If the recursive word expression does not contain symbols, it is a value. This value is returned directly. Otherwise, perform recursive operations on the left and right sides.

Including symbols, respectively (1 + 2) and (3-4) as expressions as new expressions and evaluate values using the same method respectively (the last left expression is divided into 1 and 2, the right expression is decomposed into 3 and 4 ). At the overall level, it is the expansion of a binary tree.

4. Operate the left and right expression values based on the lowest priority symbol.

The result of the Left expression (1 + 2) recursion is 3.

Right Expression (3 + 4) recursive Operation Result:-1

So (1 + 2) * (3-4) =-3

For detailed comments and code for implementation of specific expression operations, see expression. Java class.

3. Modify the caculator source code and less than 3.1 of the expression class code

Add the expression. Java class to calculator, and in logic. Java:

Use expression for calculation. Before calculation, you must specify the special android +-× Runtime (in Java, +/u2212/u00d7/u00f7, you can use native2ascii in Java for conversion) and replace it with +-*/In the expression class -*/.

Insufficient 3.2

In high-precision operations, only the addition, subtraction, multiplication, division, and parentheses are implemented. Other mathematical symbols are not implemented. For example, the realization of multiplication, log (), Ln (), and trigonometric functions involves too much mathematical knowledge in addition to multiplication. High-Precision operations are not implemented. Therefore, the modified calculator does not support addition, subtraction, multiplication, division, and parentheses due to incomplete reasons. If you need to develop an upstream database, you can use this calculator as a separate application. If you are interested, you can implement other mathematical functions. (The multiplication operator is easy to implement ^-^)

The full text is complete.

Attachment:

Http://download.csdn.net/source/2807815

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.