安卓计算器实现

2023-01-08 16:58:53
  最近刚开始接触安卓编程,移动应用开发可以使用Java或者Kotlin语言,因为没有学过Kotlin,所以计算器的实现采用Java语言。要实现计算器的功能就需要将我们输入的算式(中缀式)转化为后缀式。这样方便计算。关于如何将中缀式转化为后缀式,大家可以参考其他文章。

Java代码

package com.example.caculate;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView textView;    // 文本框
    StringBuilder content = new StringBuilder();   //获取TextView里的内容
    StringBuilder stringBuilder = new StringBuilder();

    Stack<String> sta = new Stack<String>();        //符号栈
    List<String> stack = new ArrayList<String>();   //后缀式的集合
    Map<String, Integer> priority = new HashMap<String, Integer>();  //设置符号优先级

    //数字0-9
    private Button number_zero;
    private Button number_one;
    private Button number_two;
    private Button number_three;
    private Button number_four;
    private Button number_five;
    private Button number_six;
    private Button number_seven;
    private Button number_eight;
    private Button number_nine;

    //操作符
    private Button operator_mod;
    private Button operator_add;
    private Button operator_back;     //退一格
    private Button operator_point;
    private Button operator_equal;
    private Button operator_clear;    //清除
    private Button operator_remove;
    private Button operator_subtract;
    private Button operator_multiply;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //设置符号优先级
        priority.put("+", 1);
        priority.put("-", 1);
        priority.put("*", 0);
        priority.put("/", 0);
        priority.put("%", 0);

        //TextView
        textView = findViewById(R.id.textView);

        //数字键
        number_zero = findViewById(R.id.number_zero);
        number_zero.setOnClickListener(this);
        number_one = findViewById(R.id.number_one);
        number_one.setOnClickListener(this);
        number_two = findViewById(R.id.number_two);
        number_two.setOnClickListener(this);
        number_three = findViewById(R.id.number_three);
        number_three.setOnClickListener(this);
        number_four = findViewById(R.id.number_four);
        number_four.setOnClickListener(this);
        number_five = findViewById(R.id.number_five);
        number_five.setOnClickListener(this);
        number_six = findViewById(R.id.number_six);
        number_six.setOnClickListener(this);
        number_seven = findViewById(R.id.number_seven);
        number_seven.setOnClickListener(this);
        number_eight = findViewById(R.id.number_eight);
        number_eight.setOnClickListener(this);
        number_nine = findViewById(R.id.number_nine);
        number_nine.setOnClickListener(this);

        //算数符号
        operator_mod =findViewById(R.id.operator_mod);
        operator_mod.setOnClickListener(this);
        operator_add =findViewById(R.id.operator_add);
        operator_add.setOnClickListener(this);
        operator_back = findViewById(R.id.operator_back);
        operator_back.setOnClickListener(this);
        operator_equal =findViewById(R.id.operator_equal);
        operator_equal.setOnClickListener(this);
        operator_clear =findViewById(R.id.operator_clear);
        operator_clear.setOnClickListener(this);
        operator_remove =findViewById(R.id.operator_remove);
        operator_remove.setOnClickListener(this);
        operator_subtract =findViewById(R.id.operator_subtract);
        operator_subtract.setOnClickListener(this);
        operator_multiply =findViewById(R.id.operator_multiply);
        operator_multiply.setOnClickListener(this);
        operator_point = findViewById(R.id.operator_point);
        operator_point.setOnClickListener(this);
    }

    //重写按钮点击的click方法
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.number_zero:
                content.append("0");
                textView.setText(content);
                break;
            case R.id.number_one:
                content.append("1");
                textView.setText(content);
                break;
            case R.id.number_two:
                content.append("2");
                textView.setText(content);
                break;
            case R.id.number_three:
                content.append("3");
                textView.setText(content);
                break;
            case R.id.number_four:
                content.append("4");
                textView.setText(content);
                break;
            case R.id.number_five:
                content.append("5");
                textView.setText(content);
                break;
            case R.id.number_six:
                content.append("6");
                textView.setText(content);
                break;
            case R.id.number_seven:
                content.append("7");
                textView.setText(content);
                break;
            case R.id.number_eight:
                content.append("8");
                textView.setText(content);
                break;
            case R.id.number_nine:
                content.append("9");
                textView.setText(content);
                break;
            case R.id.operator_add:
                inpu("+");
                break;
            case R.id.operator_subtract:
                inpu("-");
                break;
            case R.id.operator_multiply:
                inpu("*");
                break;
            case R.id.operator_remove:
                inpu("/");
                break;
            case R.id.operator_mod:
                inpu("%");
                break;
            case R.id.operator_point:
                inpu(".");
                break;
            case R.id.operator_back:           //  输入的内容后退一格
                if (content.length()!=0){
                    content.deleteCharAt(content.length()-1); //删除最后一个字符
                    textView.setText(content);
                }
                break;
            case R.id.operator_clear:
                textView.setText(content.delete(0,content.length()));
                break;
            case R.id.operator_equal:
                if (stack.size()!=0||sta.size()!=0){
                    sta.clear();
                    stack.clear();
                }
                String str ;
                String first;
                String primary = content.toString();
                if(primary.length()==1){ // 当用户输入的字符串仅为一个运算符或者没有注入
                    if (!Character.isDigit(primary.charAt(0))){
                        textView.setText("");
                        break;
                    }
                    str = primary;
                } else if(primary.length()==0){
                    str = primary;
                    break;
                } else{
                    first = String.valueOf(primary.charAt(0));
                    if(first.equals("+")||first.equals("*")||first.equals("/")||first.equals("%")||first.equals(".")){
                        str = primary.substring(1,primary.length());
                        if (!Character.isDigit(primary.charAt(primary.length()-1))){ //判断字符串的结尾是否为数字
                            str = str.substring(0,str.length()-1);
                        }
                    } else{
                        if (!Character.isDigit(primary.charAt(primary.length()-1))){
                            str = primary.substring(0,primary.length()-1);
                        }else{
                            str = primary;
                        }
                    }
                }
                //将中缀表达式转换为后缀表达式
                for (int i = 0; i < str.length(); i++) {
                    if (Character.isDigit(str.charAt(i))||String.valueOf(str.charAt(i)).equals(".")) {
                        stringBuilder.append(str.charAt(i));
                        if (i == str.length() - 1) {
                            stack.add(stringBuilder.toString());
                            stringBuilder.delete(0, stringBuilder.length());
                        }
                    } else {
                       stack.add(stringBuilder.toString());
                        stringBuilder.delete(0, stringBuilder.length());
                        if (!sta.empty()) {
                            for (int k = 0; k < sta.size(); k++) {
                                if (priority.get(String.valueOf(str.charAt(i))) >= priority.get(sta.peek())) {
                                    stack.add(sta.pop());
                                    if (sta.isEmpty()) {
                                        sta.push(String.valueOf(str.charAt(i)));
                                        break;
                                    } else {
                                        k--;
                                    }
                                } else {
                                    sta.push(String.valueOf(str.charAt(i)));
                                    break;
                                }
                            }
                        } else {
                            sta.push(String.valueOf(str.charAt(i)));
                        }
                    }
                    if (i == str.length() - 1) {
                        while (!sta.isEmpty()) {
                            stack.add(sta.pop());
                        }
                    }
                }

                for (int p=0; p<stack.size(); p++){
                    if (stack.get(p).equals("")){
                        stack.remove(p);
                    }
                }
                for (int i = 0; i < stack.size(); i++) {
                    if (stack.get(i).equals("+") || stack.get(i).equals("-") || stack.get(i).equals("*")|| stack.get(i).equals("/") || stack.get(i).equals("%")) {
                        switch (stack.get(i)) {
                            case "*":
                                String sum = String.valueOf(Double.valueOf(stack.get(i - 2))* Double.valueOf(stack.get(i - 1)).intValue());
                                simplify(stack,<							
  • 作者:weixin_47529039
  • 原文链接:https://blog.csdn.net/weixin_47529039/article/details/105899104
    更新时间:2023-01-08 16:58:53