Android implements the calculator (and calculates the decimal point and parentheses)

  • 2021-12-09 09:55:06
  • OfStack

In this paper, we share the specific code of Android calculator for your reference, the specific contents are as follows

Layout code:


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:stretchColumns="4"
 android:focusable="true"
 android:focusableInTouchMode="true"
 tools:context=".MainActivity">
 
 <TableRow
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#aaaaaa">
 
 <LinearLayout
 android:layout_width="0dp"
 android:layout_height="200dp"
 android:layout_column="0"
 android:gravity="right"
 android:layout_weight="1">
 
 <EditText
 android:id="@+id/content_box"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="bottom|right"
 android:background="@null"
 android:hint="0"
 android:textSize="20pt" />
 </LinearLayout>
 </TableRow> <!-- Evaluate expression input box -->
 
 <TableRow>
 
 <Button
 android:id="@+id/btn_c"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="C"
 android:textColor="#ff0000"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_ce"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="CE"
 android:textColor="#ff0000"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_aliquot"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="2"
 android:layout_weight="1"
 android:text="%"
 android:textColor="#ff0000"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_divide"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="3"
 android:layout_weight="1"
 android:text="/"
 android:textColor="#ff0000"
 android:textSize="12pt" />
 </TableRow>
 
 <TableRow>
 
 <Button
 android:id="@+id/btn_leftBracket"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_span="1"
 android:layout_weight="1"
 android:text="("
 android:textColor="#ff0000"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_rightBracket"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_span="1"
 android:layout_weight="1"
 android:text=")"
 android:textColor="#ff0000"
 android:textSize="12pt" />
 </TableRow>
 
 <TableRow>
 
 <Button
 android:id="@+id/btn_seven"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="7"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_eight"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="8"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_nine"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="2"
 android:layout_weight="1"
 android:text="9"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_multiply"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="3"
 android:layout_weight="1"
 android:text="*"
 android:textColor="#ff0000"
 android:textSize="12pt" />
 </TableRow>
 
 <TableRow>
 
 <Button
 android:id="@+id/btn_four"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="4"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_five"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="5"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_six"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="2"
 android:layout_weight="1"
 android:text="6"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_subtract"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="3"
 android:layout_weight="1"
 android:text="-"
 android:textColor="#ff0000"
 android:textSize="12pt" />
 </TableRow>
 
 <TableRow>
 
 <Button
 android:id="@+id/btn_one"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="1"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_two"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="2"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_three"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="2"
 android:layout_weight="1"
 android:text="3"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_add"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="3"
 android:layout_weight="1"
 android:text="+"
 android:textColor="#ff0000"
 android:textSize="12pt" />
 </TableRow>
 
 <TableRow>
 
 <Button
 android:id="@+id/btn_point"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="."
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_zero"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="0"
 android:textSize="12pt" />
 
 <Button
 android:id="@+id/btn_equal"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="2"
 android:layout_weight="2"
 android:text="="
 android:textColor="#ff0000"
 android:textSize="12pt" />
 </TableRow>
 
 
</TableLayout>

Activicy code:


package com.example.newcalculator;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
import java.lang.reflect.Type;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 
 /* Button definition */
 Button btn_one; //1
 Button btn_two; //2
 Button btn_three; //3
 Button btn_four; //4
 Button btn_five; //5
 Button btn_six; //6
 Button btn_seven; //7
 Button btn_eight; //8
 Button btn_nine; //9
 Button btn_zero; //0
 Button btn_c; //c
 Button btn_ce; //ce
 Button btn_aliquot; //%
 Button btn_divide; // Division sign 
 Button btn_multiply;//x
 Button btn_subtract;//-
 Button btn_add; //+
 Button btn_point; //.
 Button btn_equal; //=
 Button btn_leftBracket;//(
 Button btn_rightBracket;//)
 EditText contentBox;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initControls();
 initClickEvent();
 }
 
 
 // Find the control 
 private void initControls() {
 btn_one = findViewById(R.id.btn_one);
 btn_two = findViewById(R.id.btn_two);
 btn_three = findViewById(R.id.btn_three);
 btn_four = findViewById(R.id.btn_four);
 btn_five = findViewById(R.id.btn_five);
 btn_six = findViewById(R.id.btn_six);
 btn_seven = findViewById(R.id.btn_seven);
 btn_eight = findViewById(R.id.btn_eight);
 btn_nine = findViewById(R.id.btn_nine);
 btn_zero = findViewById(R.id.btn_zero);
 btn_c = findViewById(R.id.btn_c);
 btn_ce = findViewById(R.id.btn_ce);
 btn_aliquot = findViewById(R.id.btn_aliquot);
 btn_divide = findViewById(R.id.btn_divide);
 btn_multiply = findViewById(R.id.btn_multiply);
 btn_subtract = findViewById(R.id.btn_subtract);
 btn_add = findViewById(R.id.btn_add);
 btn_point = findViewById(R.id.btn_point);
 btn_equal = findViewById(R.id.btn_equal);
 contentBox = findViewById(R.id.content_box);
 btn_leftBracket = findViewById(R.id.btn_leftBracket);
 btn_rightBracket = findViewById(R.id.btn_rightBracket);
 }
 
 @Override
 public void onClick(View view) {
 switch (view.getId()){
 case R.id.btn_one:{
 changeVal("1");
 }break;
 case R.id.btn_two:{
 changeVal("2");
 }break;
 case R.id.btn_three:{
 changeVal("3");
 }break;
 case R.id.btn_four:{
 changeVal("4");
 }break;
 case R.id.btn_five:{
 changeVal("5");
 }break;
 case R.id.btn_six:{
 changeVal("6");
 }break;
 case R.id.btn_seven:{
 changeVal("7");
 }break;
 case R.id.btn_eight:{
 changeVal("8");
 }break;
 case R.id.btn_nine:{
 changeVal("9");
 }break;
 case R.id.btn_zero:{
 changeVal("0");
 }break;
 case R.id.btn_aliquot:{
 changeVal("%");
 }break;
 case R.id.btn_divide:{
 changeVal("/");
 }break;
 case R.id.btn_multiply:{
 changeVal("*");
 }break;
 case R.id.btn_subtract:{
 changeVal("-");
 }break;
 case R.id.btn_add:{
 changeVal("+");
 }break;
 case R.id.btn_rightBracket:{
 changeVal(")");
 }break;
 case R.id.btn_leftBracket:{
 changeVal("(");
 }break;
 case R.id.btn_c:{
 funC();
 }break;
 case R.id.btn_ce:{
 funClearAll();
 }break;
 case R.id.btn_point:{
 changeVal(".");
 }break;
 case R.id.btn_equal:{
 String str = contentBox.getText().toString();
 Calculator calculator = new Calculator();
 Double result = calculator.Eval(str);
 contentBox.setText(result.toString());
 }break;
 }
 }
 private void changeVal(String flag){
 String str = contentBox.getText().toString();
 contentBox.setText(str+flag);
 }
 private void funC(){
 String str = contentBox.getText().toString();
 str = str.substring(0,str.length()-1);
 contentBox.setText(str);
 }
 private void funClearAll(){
 contentBox.setText("");
 }
 private void initClickEvent(){
 btn_one.setOnClickListener(this);
 btn_two.setOnClickListener(this);
 btn_three.setOnClickListener(this);
 btn_four.setOnClickListener(this);
 btn_five.setOnClickListener(this);
 btn_six.setOnClickListener(this);
 btn_seven.setOnClickListener(this);
 btn_eight.setOnClickListener(this);
 btn_nine.setOnClickListener(this);
 btn_zero.setOnClickListener(this);
 btn_aliquot.setOnClickListener(this);
 btn_divide.setOnClickListener(this);
 btn_multiply.setOnClickListener(this);
 btn_subtract.setOnClickListener(this);
 btn_point.setOnClickListener(this);
 btn_equal.setOnClickListener(this);
 btn_add.setOnClickListener(this);
 btn_c.setOnClickListener(this);
 btn_ce.setOnClickListener(this);
 btn_rightBracket.setOnClickListener(this);
 btn_leftBracket.setOnClickListener(this);
 }
}

Evaluate expression code:


package com.example.newcalculator;
 
import java.util.*;
 
// Calculation  2*(4+(88-86)/2)
 
 
// Calculation  2*(4+(88-86)/2)
 
class Caculater {
 private String[] sArry;// Save the segmented string 
 private Stack<String> houx = new Stack<String>();
 private Stack<String> fuhao = new Stack<String>();
 
 // Structure initialization 
 Caculater(String str) {
 int i = str.length() - 1;
 String temp = "";
 int j = 0;
 Boolean bool = true;
 // Add to the left and right of the symbol 1 A # Character partition 
 while (true) {
 if (!bool) break;
 if (i == j) {
 bool = false;
 }
 if (str.charAt(j) == '+'
  || str.charAt(j) == '-'
  || str.charAt(j) == '*'
  || str.charAt(j) == '/'
  || str.charAt(j) == '('
  || str.charAt(j) == ')') {
 temp += '#';
 temp += str.charAt(j);
 temp += '#'; // After filling it out, it is 2#*##(#4#+##(#88#-#86#)##/#32#)#
 } else {
 temp += str.charAt(j);
 }
 j++;
 }
 sArry = temp.split("#+");// Split into strings with regular expressions, #+ Denote 1 One or more # Character // Results: [2,*,(,4,+,(,88,-,85,),/,2,)]
 }
 
 // Post-sequential arrangement 
 public void backsort() {
 // Cycle sArry
 for (int i = 0; i < sArry.length; i++) {
 // If it is not a character, it will be directly push Into houx Stack 
 if (!sArry[i].equals("+")
  && !sArry[i].equals("-")
  && !sArry[i].equals("*")
  && !sArry[i].equals("/")
  && !sArry[i].equals("(")
  && !sArry[i].equals(")")) {
 houx.push(sArry[i]);
 continue;
 // Otherwise, it is a character. If the symbol stack is empty, it will be directly put into the stack 
 } else if (fuhao.isEmpty()) {
 fuhao.push(sArry[i]);
 continue;
 // If it is (parentheses, go directly to the symbol stack 
 } else if (sArry[i].equals("(")) {
 fuhao.push(sArry[i]);
 continue;
 // If) parentheses 
 } else if (sArry[i].equals(")")) {
 /**
  *  Keep going out of the stack until (parentheses appear) 
  *
  */
 while (!fuhao.peek().equals("(")) {
  houx.push(fuhao.pop());
 }
 fuhao.pop();// Clear (brackets) 
 // If it is not empty and the symbol to be entered has higher priority than the symbol at the top of the symbol stack, the push Into symbol stack 
 } else if (!fuhao.isEmpty() && check(sArry[i], fuhao.peek())) { //
 fuhao.push(sArry[i]);
 continue;
 // Otherwise, the symbol with high priority in the symbol stack is removed from the stack, push Into houx Stack, and then store the symbols into the symbol stack 
 } else {
 houx.push(fuhao.pop());
 fuhao.push(sArry[i]);
 continue;
 }
 }
 // After traversing, the symbols in the stack are directly out of the stack in turn. push Into houx Stack 
 while (!fuhao.isEmpty()) {
 houx.push(fuhao.pop());
 }// The result is: 2 4 88 86 - 2 / + *  In-stack sequence 
 }
 
 //check Contrast priority 
 private boolean check(String a, String b) {
 // If the symbol stack is (parentheses, return directly true
 if (b.equals("(")) {
 return true;
 }
 // If the priority in the symbol stack is higher than the one to be entered, return false
 if ((b.equals("*") || b.equals("/")) && (a.equals("+") || a.equals("-"))) { //b>a
 return false;
 }
 // . . . . . . . . . . . . . Returns the low of true
 if ((b.equals("+") || b.equals("-")) && (a.equals("*") || a.equals("/"))) { //b<a
 return true;
 }
 return false;
 }
 
 // Off-stack calculation 
 public Double suan() {
 
 backsort();// Post-sequential arrangement 
 
 // Result stack end
 Stack<Double> end = new Stack<Double>();
 // Traversal houx Stack 
 for (int i = 0; i < houx.size(); i++) {
 // If it's a plus sign, end pop Two numbers come out, and the calculated results are put into the stack 
 if (houx.get(i).equals("+")) {
 Double b = end.pop();
 Double a = end.pop();
 end.push(a + b);
 continue;
 // If it's a minus sign, end pop Two numbers are stacked, and the calculated results are put into the stack 
 } else if (houx.get(i).equals("-")) {
 Double b = end.pop();
 Double a = end.pop();
 end.push(a - b);
 continue;
 // If it's a multiplication sign, end pop Two numbers are stacked, and the calculated results are put into the stack 
 } else if (houx.get(i).equals("*")) {
 Double b = end.pop();
 Double a = end.pop();
 end.push(a * b);
 continue;
 // If it is a division sign, end pop Two numbers are stacked, and the calculated results are put into the stack 
 } else if (houx.get(i).equals("/")) {
 Double b = end.pop();
 Double a = end.pop();
 end.push(a / b);
 continue;
 } else if (houx.get(i).isEmpty()) {
 
 continue;
 } else {
 // It's not a symbol, that is, a number, Integer.parseInt Turn int Type,  push Stack 
 end.push(Double.parseDouble(houx.get(i)));
 }
 
 }
 
 // Output result 
 return end.pop();
 }
 
}
 
public class Calculator {
 static String str="";
 public Double Eval(String str){
 
 // Structured 
 Caculater cl = new Caculater(str);
 // Calculation 
 Double result = cl.suan();
 return result;
 }
}

Please check "Calculator Special Topics" for wonderful articles about calculators, and more wonderful articles are waiting for you to discover!


Related articles: