android calculator realizes addition subtraction multiplication and division of two digits

  • 2021-11-30 01:32:44
  • OfStack

In this paper, we share the specific code of addition, subtraction, multiplication and division of android calculator for your reference, the specific contents are as follows

Note: The following calculator only pays attention to the realization function, does not consider other BUG, only two-digit integer algorithm operation, suitable for novice

1. Realize ideas

The numerical value obtained from the keyboard is placed in a character array, and the operation symbol (+-/) is used as the dividing point to separate the two numerical values and carry out algorithm operation. *

2. Difficulties

How to judge whether it is a symbol? +-×/
Where is the record symbol?

3. Steps:

1. Get the value entered by the keyboard
2. Store the value in an array of 1 characters
3. Traverse every number in the array. If you find the algorithm symbol, record the position of the algorithm symbol. (Key points, starting at 0)
4. Place the number before the algorithm symbol in a defined int type number
5. Same
6. Judge which method is addition, subtraction, multiplication and division, and then perform simple operation.

4. Code

i: Layout:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:weightSum="1"
 >

 <TextView android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/etResult"
 android:layout_weight="0.05"
 android:textSize="25dp"
 android:paddingTop="10dp"
 android:gravity="bottom"
 android:hint="0.0"
 />
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:layout_weight="0.8">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:weightSum="1">

 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="C"
 android:textSize="25dp"
 android:background="@color/colorWhite"
 android:id="@+id/btnQingchu"
 android:layout_weight="0.5" />
 <Button
 android:layout_width="235dp"
 android:layout_height="wrap_content"
 android:text=" " "
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:id="@+id/btnHuishan"
 android:layout_weight="0.5"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn7"
 android:text="7"
 android:textSize="25dp"
 android:layout_weight="0.25"
 />
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn8"
 android:text="8"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn9"
 android:text="9"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnChu"
 android:text=" ÷ "
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn4"
 android:text="4"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn5"
 android:text="5"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn6"
 android:text="6"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnCheng"
 android:text=" × "
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn1"
 android:text="1"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn2"
 android:text="2"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn3"
 android:text="3"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnJian"
 android:text=" - "
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn0"
 android:text="0"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnDian"
 android:text="."
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnDengyu"
 android:text="="
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnJia"
 android:text=" + "
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:layout_weight="0.25"/>
 </LinearLayout>
 </LinearLayout>
</LinearLayout>

ii: Get keyboard value, write monitor


public void getButton(){
 // Get Button Component 
 btn0= (Button) findViewById(R.id.btn0);
 btn1= (Button) findViewById(R.id.btn1);
 btn2= (Button) findViewById(R.id.btn2);
 btn3= (Button) findViewById(R.id.btn3);
 btn4= (Button) findViewById(R.id.btn4);
 btn5= (Button) findViewById(R.id.btn5);
 btn6= (Button) findViewById(R.id.btn6);
 btn7= (Button) findViewById(R.id.btn7);
 btn8= (Button) findViewById(R.id.btn8);
 btn9= (Button) findViewById(R.id.btn9);

 btnJia= (Button) findViewById(R.id.btnJia);
 btnJian= (Button) findViewById(R.id.btnJian);
 btnCheng= (Button) findViewById(R.id.btnCheng);
 btnChu= (Button) findViewById(R.id.btnChu);

 btnDian= (Button) findViewById(R.id.btnDian);
 btnDengyu= (Button) findViewById(R.id.btnDengyu);
 btnQingchu= (Button) findViewById(R.id.btnQingchu);
 btnHuishan= (Button) findViewById(R.id.btnHuishan);

 etGet = (TextView) findViewById(R.id.etResult);
 // Binding monitoring 
 btn0.setOnClickListener(this);
 btn1.setOnClickListener(this);
 btn2.setOnClickListener(this);
 btn3.setOnClickListener(this);
 btn4.setOnClickListener(this);
 btn5.setOnClickListener(this);
 btn6.setOnClickListener(this);
 btn7.setOnClickListener(this);
 btn8.setOnClickListener(this);
 btn9.setOnClickListener(this);

 btnJia.setOnClickListener(this);
 btnJian.setOnClickListener(this);
 btnCheng.setOnClickListener(this);
 btnChu.setOnClickListener(this);

 btnDian.setOnClickListener(this);
 btnDengyu.setOnClickListener(this);
 btnQingchu.setOnClickListener(this);
 btnHuishan.setOnClickListener(this);
 }

iii: Bind Button


 @Override
 public void onClick(View v) {
 str = etGet.getText().toString();
 switch (v.getId()){
 // Digital button 
 case R.id.btn0:
 case R.id.btn1:
 case R.id.btn2:
 case R.id.btn3:
 case R.id.btn4:
 case R.id.btn5:
 case R.id.btn6:
 case R.id.btn7:
 case R.id.btn8:
 case R.id.btn9:
 /* if (b_Clean)
 {
  b_Clean =false;
  etGet.setText("");
 }*/
 etGet.setText(str+((Button)v).getText());
 break;
 // Operation button 
 case R.id.btnJia:
 case R.id.btnJian:
 case R.id.btnCheng:
 case R.id.btnChu:
 case R.id.btnDian:
 /* if (b_Clean)
 {
  b_Clean =false;
  etGet.setText("");
 }*/
 etGet.setText(str+((Button)v).getText());
 break;
 // Clear button 
 case R.id.btnQingchu:
 /* if (b_Clean)
 {
  b_Clean =false;
  etGet.setText("");
 }*/
 etGet.setText("");
 break;
 case R.id.btnDengyu:
 getResult();
 break;
 case R.id.btnHuishan:
 str=etGet.getText().toString();
 try {
  etGet.setText(str.substring(0,str.length()-1));
 }
 catch (Exception e){
  etGet.setText("");
 }

 break;

 }
 }

iV: Algorithm Function Implementation


public void getResult(){
 str = etGet.getText().toString();
 strArray = new String[str.length()]; // Place the resulting string in the 1 In an array of characters 
 //System.out.println("str"+str);
 int n=0;
 for(int i=0; i<strArray.length;i++){
 strArray[i] = str.substring(i, i+1); // Traversing each element of the array 
 //System.out.print(strArray[i]);
 if(strArray[i].equals(" + ")||strArray[i].equals(" - ") // Satisfy the condition 
  ||strArray[i].equals(" × ")||strArray[i].equals(" ÷ "))
 {
 n= i; // Record the location where the symbol exists 
 }
 }
 int num1 = Integer.parseInt(str.substring(0,n)); // Before getting 1 String number 
 String caculate = str.substring(n,n+1); // Get algorithm symbols, add, subtract, multiply and divide 
 int num2 = Integer.parseInt(str.substring(n+1)); // After getting 1 String number 

 if (caculate.equals(" + "))
 {
 Inputresult = num1+num2;
 }
 else if (caculate.equals(" - "))
 {
 Inputresult = num1-num2;
 }
 else if (caculate.equals(" × "))
 {
 Inputresult = num1*num2;
 }
 else if (caculate.equals(" ÷ "))
 {
 if (num2==0)
 {
 return ;
 }
 Inputresult = num1/num2;
 }
 etGet.setText(num1+caculate+num2+"="+Inputresult);
 }

For more calculator functions, please click on the topic: Calculator Function Summary to learn

About the Android calculator function realization, check the topic: Android calculator for learning.


Related articles: