1. Design tasks and requirements
(1) design a calculator based on Android system to realize the arithmetic of addition, subtraction, multiplication and division, and the operation of clearing and canceling. The interface design should be as simple and beautiful as possible, with good interaction, and the program should have good robustness;
(2) display the operation result in another line;
(3) in the whole process of input and calculation, first of all, the input data should be processed with fault tolerance, which is conducive to improving the user experience. For example, in the process of input, operators can't input continuously, and zero can't appear at the beginning of integer data. In the process of programming, we need to design data structure to store numbers, operators and fault-tolerant tags;
(4) for continuous input of multiple groups of data, such as 3 + 8 / 2-98 or (1 + 2) 3 + 5-4 / 2, it should be able to distinguish the numbers and operators, and operate according to the priority.
2. Design principle and structure diagram
The expression string is processed, the numbers and operators are stored in two stacks respectively, and the design method is used to process the two stacks to get the corresponding calculation results. The operator design priority, multiplication and division priority is set to 2, and the addition and subtraction priority is set to 1.
(1) pop up two operators from the operator stack, assuming that they are symbol1 and symbol2, and compare the priority of the two operations. If the priority of symbol1 is greater than or equal to that of symbol2, execute two, otherwise execute three.
(2) pop up two numbers from the number stack, calculate the operation results of these two numbers under the operator symbol 1, press the results into the number stack, press the symbol 2 into the operator stack, and finally return one.
(3) pop up three numbers from the number stack, calculate the operation results of the last two numbers under the operator symbol 2, press the operation results into the stack after the end, press the remaining first number into the stack, press the operator symbol 1 into the stack, and finally return one.
The structure block diagram is as follows:
Figure 1 structure diagram of simple calculator software
Figure 1 functional structure diagram of simple calculator
3. Detailed design
The input module mainly describes the calculator keyboard and keyboard monitoring, that is, it is mainly responsible for reading the user's keyboard input and responding to the touch-screen keys. When the user clicks the key or the screen, the listener will call the corresponding function keys, which are reset, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, *, /, =, @, left bracket, right bracket, etc. The display module mainly describes the display area of the calculator, which is used to display the data input by the user, the final calculation results and some other information.
According to the different input polynomials (such as input 3 + 8 / 2-9 * 8), it is necessary to divide the polynomials, find out the numbers and symbols and save them respectively, and then calculate them according to the priority of operators.
(1) Initialization part 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; //Clear data input area Button btn_del; //Delete one Button btn_plus; // + Button btn_minus; // - Button btn_multiply ; // * Button btn_divide; // / Button btn_equal; // = Button btn_left; //Left parenthesis Button btn_right; //Right bracket private TextView et_input; //Data input area private StringBuilder pending = new StringBuilder(); //Data input area string 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); //Set key monitoring btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); ...... }
(2) Button key value judgment and execution function code
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) Priority judgment function
Use the set to define the operation priority of the symbol. If there are parentheses, first calculate the formula in parentheses, and then calculate in the order of first multiplication and division, then addition and subtraction.
4. Test and analysis
Test process:
(1) install relevant software and JDK installation configuration
(2) install the JDK of Java to the default path D:\Program Files\Java \. Generally, 300M space is required. If the hard disk space is sufficient, all components are installed. Then complete the configuration of environment variables.
(3) configure the Eclipse development environment, set the font size of the code, adjust to 12 (or 14), display the line number, format the code, change the maximum number of characters that a line can hold, set the intelligent prompt, run eclipse.exe to complete the path setting, and select the menu Help Install New Software
Configure SDK path:
(1) install the JDK developed by java to the local machine, and configure the environment variables.
(2) extract Android SDK and Eclipse, and configure the SDK path of Eclipse.
The default of the system is to install new SDK components online, select Use existing SDKs, and then click next to complete the configuration of one part. After the configuration is completed, find the Android SDK Manager in Windows to run, and check whether it can be started normally. After the startup, select the domestic image website for the next operation.
! configured Eclipse Configured Eclipse
Summary: you need to install JDK first, then the configuration environment changes, and then complete the decompression of SDK. Extract Eclipse with ADT, and finally reconfigure the SDK path in Eclipse. It's ready to use.
So far, the Android development environment has been basically built.
The test is as follows:
5. References
[1] Sun update, bin Sheng, sun hailun. Java ME mobile phone application development Daquan. Beijing: Science Press, 2008.6
[2] Guo Kehua. Java ME mobile development case elaboration. Beijing: Tsinghua University Press, 2010.1