Andriod design simple calculator

1. Design tasks and requirements

     (1) design a calculator in the Android-based system, addition, subtraction algorithms, as well as clear, undo. Interface design should be simple and as far as possible in appearance, has good interaction, should have the good robustness;
     (2) a separate row computation result;
     (3) the entire input and computation process, should first input fault tolerant data processing, this will help improve the user experience. Fault-tolerant design example: the operator can not continuously input during the input, the data can not appear at the beginning of the integer 0 and the like. In the programming process, the need to store digital design data structures, and fault-tolerant marker operators;
     (4) a plurality of sets of data for a continuous input, such as a 3 + 8 / 2-9 8 (2 + 1) 3 + 5- 4/2, should be able to distinguish between the various operators and numbers, and performs calculation according to the priority.

2. Design principle and structure diagram

     The string expression is processed, the numbers and operators are stored in two stacks, the two stacks to handle the design method to give the corresponding calculation result, the operator design priority, multiplication and division priority is set to 2, addition and subtraction priority is set to 1.
     (1) the operator is popped from the stack two operators, and assumed symbol1 symbol2, two compare operations pay priority if the priority of greater than or equal symbol1 symbol2 is performed two, three or executed.
     (2) is popped from the stack two digital numbers, these two numbers is calculated at the calculation result of symbol1 operator, after press-fitting the end result of the digital stack, the stack symbol2 pressed into the operator, a final return.
     (3) the stack is popped from the digital three figures, two figures at the calculation result of the calculation symbol2 operator, after the end of the operation result onto the stack, the remaining first number onto the stack, the operator symbol1 pushed onto the stack, and finally a return.
: Shows the block diagram as
Here Insert Picture Description
in FIG. 1 a block diagram showing the software configuration simple calculator
Here Insert Picture Description
functional block diagram showing a simple calculator FIG.

3. detailed design

     The input module describes the key listener that is responsible for reading the user's keyboard input and responsive touch screen calculator keyboard and the keyboard when the user clicks the button or the screen will go listeners to call the corresponding function keys, which are cleared , 0,1,2,3,4,5,6,7,8,9, +, -, *, /, =, @, left parenthesis, right parenthesis and so on. The display module describes the calculator display area, i.e., user input data and the final results and some other area for displaying the information.
(Such as input 3 + 8 / 2-9 * 8), needs to be divided depending on the input polynomial polynomial, numbers, and symbols to identify each stored separately and then be calculated according to the priority of the operator.

(1) initialization part of the code

Button btn_0;
Button btn_1;
Button btn_2;
Button btn_3;
Button btn_4;
Button btn_5;
Button btn_6;
Button btn_7;
Button btn_8;
Button btn_9;
Button btn_clear; 				//清除数据输入区
Button btn_del;   				//删除一位
Button btn_plus;				// +
Button btn_minus;				// -
Button btn_multiply	;			// *
Button btn_divide;				// /
Button btn_equal;				// =
Button btn_left;				//左括号
Button btn_right;				//右括号
private TextView et_input;		//数据输入区
private StringBuilder pending = new StringBuilder();	//数据输入区字符串

private void initView() {
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
  	......

        btn_0.setOnClickListener(this);	//设置按键监听
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
	......
    }

(2) key-button and execute the function code is determined

public void onClick(View v) {
        int last = 0;
        if(pending.length()!=0) {
            last = pending.codePointAt(pending.length()-1);
        }
        switch (v.getId()) {
            case R.id.btn_0:
                pending = pending.append("0");
                et_input.setText(pending);
                break;
            case R.id.btn_1:
                pending = pending.append("1");
                et_input.setText(pending);
                break;
            case R.id.btn_2:
                pending = pending.append("2");
                et_input.setText(pending);
                break;
            case R.id.btn_3:
                pending = pending.append("3");
                et_input.setText(pending);
                break;
            case R.id.btn_4:
                pending = pending.append("4");
                et_input.setText(pending);
                break;
            case R.id.btn_5:
                pending = pending.append("5");
                et_input.setText(pending);
                break;
            case R.id.btn_6:
                pending = pending.append("6");
                et_input.setText(pending);
                break;
            case R.id.btn_7:
                pending = pending.append("7");
                et_input.setText(pending);
                break;
            case R.id.btn_8:
                pending = pending.append("8");
                et_input.setText(pending);
                break;
            case R.id.btn_9:
                pending = pending.append("9");
                et_input.setText(pending);
                break;
        ......
        }
    }

(3) the priority judging function

     Defining a set of symbols using the priority of the operation, if there are brackets, the bracketed expression is first calculated, and then follow the first multiplication and division, the calculation order of addition and subtraction.

4. Test and Analysis

     The testing process:
     (1) install software, JDK installation configuration
     (2) of the Java JDK installation to the default path D: \ Program Files \ Java \ , generally requires 300M of space, plenty of hard disk space if all components have been installed. Then complete the configuration environment variable.
     (3) Configuration Eclipse development environment, the code set font size, is adjusted to 12 (or 14), a display line number, code formatting changes to accommodate the maximum number of characters of a line, the smart prompt provided, running path setting completion eclipse.exe select menu Help-install New Software ...
Here Insert Picture Description
     disposed SDK path:
     (1) mounted in java JDK to the machine, configure the environment variables.
     (2) extracting and Eclipse Android SDK, Eclipse is disposed SDK path.
The default is to install a new SDK components online, select Use existing SDKs, and then click Next to complete the configuration of a warp. Complete the configuration in Windows to find the Android SDK Manager to run, to detect whether it can start after start, select the country mirror sites to the next step.
! Eclipse configuration of the configuration of Eclipse
     Summary: you need to install the JDK configuration environment has changed, and then complete the decompression SDK, unpacked the Eclipse ADT, last reconfigured in the Eclipse SDK path. Ready to use.
At this point the basic Android development environment to build complete.
FIG test as follows:
4 code is compiled and run a simple calculator chart
5 demonstrates simple calculator to calculate the map

5. References

[1] Sun update, Bin Sheng, Sun seagoing .Java ME mobile application development Daquan Beijing: Science Press, 2008.6
[2] Guo Kehua .JavaME mobile development examples succinctly Beijing: Tsinghua University Press, 2010.1

Published 32 original articles · won praise 56 · views 60000 +

Guess you like

Origin blog.csdn.net/DengFengLai123/article/details/104072858