Android studio进阶开发(五)--教你做一个科学计算器

众所周知,计算器是我们日常生活中必不可少的部分,但每次使用都要打开手机里的计算器使用,很不方便,今天教大家做一个科学计算器,方便使用

今天的代码比较简单,就不过多介绍,当作模板,正常使用即可

java代码如下:

public List<String> Str2infix(String str){
        //中缀表达式列表初始长度
        int index = 0;
        this.list = null;
        //表达式列表
        List<String> list = new ArrayList<>();
        //取值循环
        //使用do-while至少执行一次
        do{
            char ch = str.charAt(index);//取到当前位置处的字符
            /*基础处理组*/
            //操作符直接添加到中缀表达式中
            if("+-*×/÷^!√()".indexOf(str.charAt(index)) >= 0){
                //是操作符,直接添加至list中
                index ++;
                list.add(ch+"");    //ch+""操作使ch转化为string
            }
            /*对数逻辑组*/
            else if ("l".indexOf(str.charAt(index)) >= 0) {
                String str1 = "";
                //lg:以10为底的对数计算;ln:以自然对数为底的对数计算
                while (index < str.length() && "lgln".indexOf(str.charAt(index)) >= 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            }
            /*三角函数处理组*/
            else if ("s".indexOf(str.charAt(index)) >= 0) {
                //sin
                String str1 = "";
                while (index < str.length() && "sin".indexOf(str.charAt(index)) >= 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            } else if ("c".indexOf(str.charAt(index)) >= 0) {
                //cos
                String str1 = "";
                while (index < str.length() && "cos".indexOf(str.charAt(index)) >= 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            } else if ("t".indexOf(str.charAt(index)) >= 0) {
                //tan
                String str1 = "";
                while (index < str.length() && "tan".indexOf(str.charAt(index)) >= 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            }
            /*反三角函数组*/
            else if ("a".indexOf(str.charAt(index)) >= 0) {
                //arc
                String str1 = "";
                while (index < str.length() && "arcsincostan".indexOf(str.charAt(index)) >= 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            }
            /*其余符号处理组*/
            else if (str.charAt(index) == 'e' || str.charAt(index) == 'p' || str.charAt(index) == 'π'){
                //这里的e是自然对数,p是π的简写
                index ++;
                list.add(ch+"");
            }
            /*数字处理组*/
            else if("0123456789".indexOf(str.charAt(index)) >= 0){
                //如果输入的是数字,判断其是否为多为数字多位数的情况
                String str1 = "";
                if (!degree_check){
                    //小数点在内循环中检查
                    //只检查小数点,不添加数字转角度功能
                    while (index < str.length() && "0123456789.".indexOf(str.charAt(index)) >= 0){
                        str1 += str.charAt(index);
                        index ++;
                    }
                } else {
                    //角度相关功能
                    while (index < str.length() && "0123456789.°\"\'".indexOf(str.charAt(index)) >= 0){
                        str1 += str.charAt(index);
                        index ++;
                    }
                }
                list.add(str1);
            }
        }while (index < str.length());
        //输出以List形式存储的中缀表达式
        this.list = list;
        return list;
/**
     * 判断当前接收的字符串是否为操作符
     *
     * @param op 输入的字符代号
     * @return boolean 是:True;否:False
     * @author HansAwake
     * @create 2024/12/31
     **/
    public static boolean isOperator(String op){
        //判断是否为操作符
        if ("0123456789.ep".indexOf(op.charAt(0)) == -1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断当前接收的字符串是否为操作数
     *
     * @param num 输入的字符代号
     * @return boolean 是:True;否:False
     * @author HansAwake
     * @create 2024/12/31
     **/
    public static boolean isNumber(String num){//判断是否为操作数
        if ("0123456789ep".indexOf(num.charAt(0)) >= 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断操作负的优先级
     *
     * @param op 等待判断操作符
     * @return int
     * @author HansAwake
     * @create 2024/12/31
     **/
    public static int adv(String op){//判断操作符的优先级
        int result = 0; //默认返回值
        switch(op) {
            //加减是基础算符,计算等级低
            case "+":
                result = 1;
                break;
            case "-":
                result = 1;
                break;
            //乘除是2级算符
            case "*": //默认的乘法算符
            case "×": //微软自带的乘法算符
                result = 2;
                break;
            case "/":
            case "÷":
                result = 2;
                break;
            //乘方是3级算符
            case "^":
                result = 3;
                break;
            // 阶乘\g开根\取对数ln\取对数log2
            // \sin\cos\tan\反三角函数是4级算符
            case "!":
                result = 4;
                break;
            case "g":
            case "√":
                result = 4;
                break;
            /*计算器中没有以未知数x为底的对数计算*/
            case "l":
            case "ln":
                result = 4;
                break;
            case "o":
            case "lg":
                result = 4;
                break;
            //三角函数sin\cos\tan
            case "s":
            case "sin":
                result = 4;
                break;
            case "c":
            case "cos":
                result = 4;
                break;
            case "t":
            case "tan":
                result = 4;
                break;
            //反三角函数arcsin\arccos\arctan
            case "arcsin":
                result = 4;
                break;
            case "arccos":
                result = 4;
                break;
            case "arctan":
                result = 4;
                break;
            //div
        }
        //将操作符等级回返给计算器
        return result;
    }
/**
     * 将中缀表达式转换成为后缀表达式
     * 
     * @param list 待处理的中缀表达式
     * @return java.util.List<java.lang.String> 传回的后缀表达式
     * @author HansAwake
     * @create 2024/12/31
     * @edit 2025/1/2
     **/
    public List<String> infix2suffix(List<String> list){
        //中缀表达式转换称后缀表达式
        //使用堆栈存储字符情况
        Stack<String> operator = new Stack<>();
        //后缀表达式存储列表
        List<String> list2 = new ArrayList<>();
        if (!list.isEmpty()) {//传入的中缀表达式非空判断
            for (int i = 0; i < list.size(); i++) {
                if (isNumber(list.get(i))){//是数字直接压入新列表中
                    list2.add(list.get(i));
                } else if (list.get(i).charAt(0) == '('){//左括号入栈
                    operator.push(list.get(i));
                }
                //是操作符且非左括号,进入2级选择
                else if (isOperator(list.get(i)) && list.get(i).charAt(0) != '('){
                    if (operator.isEmpty()){
                        operator.push(list.get(i));
                    } else {//符栈不为空
                        if (list.get(i).charAt(0) != ')'){//如果不是右括号
                            if (adv(operator.peek()) <= adv(list.get(i))){
                                //入栈
                                operator.push(list.get(i));
                            } else {//出栈
                                //如果堆栈非空且左括号不在栈顶
                                while (!operator.isEmpty() && !"(".equals(operator.peek())){
                                    //判断操作符等级
                                    if(adv(list.get(i)) <= adv(operator.peek())){
                                        list2.add(operator.pop());
                                    }
                                }
                                if (operator.isEmpty() || operator.peek().charAt(0) == '('){
                                    operator.push(list.get(i));
                                }
                            }
                        } else if (list.get(i).charAt(0) == ')'){
                            while (operator.peek().charAt(0) != '('){
                                list2.add(operator.pop());
                            }
                            operator.pop();
                        }
                    }
                }
            }
            //对余下的操作字符进行处理
            while (!operator.isEmpty()){
                list2.add(operator.pop());
            }
        } else {
            //如果传入的字符串是空的,进行如下操作
        }
        return list2;
    }

    /**
     * 将经纬度转换为度分秒格式
     *
     */
    public String num2degree(double angle) {
        //角度取整
        int degree = (int) angle;
        //取小数部分
        double decimals = (angle - degree) * 60;
        //分的计算
        int minute = (int) decimals;
        //秒的计算
        String second = String.format("%.2f", Math.abs(((decimals - minute) * 60)));
        return degree + "°" + Math.abs(minute) + "'" + second + "\"";
    }
    public String num2degree(String angle) {
        double temp_angle = Double.parseDouble(angle);
        //角度取整
        int degree = (int) temp_angle;
        //取小数部分
        double decimals = (temp_angle - degree) * 60;
        //分的计算
        int minute = (int) decimals;
        //秒的计算
        String second = String.format("%.2f", Math.abs(((decimals - minute) * 60)));
        return degree + "°" + Math.abs(minute) + "'" + second + "\"";
    }

    /**
     * 度分秒转经纬度
     *
     */
    @SuppressLint("DefaultLocale")
    public double degree2num(String angle) {
        if (angle == null) return 0;

        /*结果返回*/
        double ans = 0;

        try {
            boolean degree_tag = true;
            boolean minute_tag = true;

            //空格处理
            angle = angle.replace(" ", "");

            /*度分秒代理存储*/
            List<String> list = new ArrayList<>();

            //分离"度"-将String以°分割为两个字符串
            String[] str2 = angle.split("°");
            if (str2.length > 2){
                //如果分离出超过2个字符串(3+)那么表示度分秒输入错误
                return -201;//度分秒-度符号超限
            } else if (str2.length == 2) {
                //如果分离出2个字符串,提取index=0的数字为degree
                if (!Objects.equals(str2[0], "")){
                    list.add(str2[0]);//存储度
                }
            } else if (angle.contains("°") && str2.length == 1) {
                list.add(str2[0]);
                degree_tag = false;
            } else {
                degree_tag = false;
                list.add("0");
            }

            //分离"分"-将String以'分割为两个字符串
            String[] str3 = null;
            if (degree_tag){
                str3 = str2[1].split("'");
            } else str3 = angle.split("'");

            if (str3.length > 2){
                //如果分离出超过2个字符串(3+)那么表示度分秒输入错误
                return -202;//度分秒-分符号超限
            } else if (str3.length == 2) {
                //如果分离出2个字符串,提取index=0的数字为minute
                if (!Objects.equals(str3[0], "")){
                    list.add(str3[0]);
                }
            } else if (angle.contains("'") && str3.length == 1) {
                list.add(str3[0]);
            } else {
                minute_tag = false;
                list.add("0");
                str3 = null;
            }

            //分离"秒"-补充分不存在时的情况与没有秒存在时的情况
            if (minute_tag){
                if (str3.length == 2){
                    if (str3[1].contains("\""))
                        list.add(str3[1].substring(0, str3[1].length() - 1));//这里默认最后一位是\"
                    else list.add("0");
                }
                else{
                    list.add("0");
                }
            } else {
                //字符串是度-秒形式的
                if (str2.length == 2 && str2[1].contains("\"")){
                    list.add(str2[1].substring(0, str2[1].length() - 1));//这里默认最后一位是\"
                }
                //字符串只有秒的
                else if (str2[0].contains("\""))
                    list.add(str2[0].substring(0, str2[0].length() - 1));//这里默认最后一位是\"
                else list.add("0");
            }
            str2 = null;

            System.gc();//回收空数组

            //将数值全部转化为秒
            double num = 0;
            num = Math.abs(Double.parseDouble(list.get(0)))*3600 +
                    Math.abs(Double.parseDouble(list.get(1)))*60 +
                    Math.abs(Double.parseDouble(list.get(2)));

            ans = num / 3600;
            if (Double.parseDouble(list.get(0)) < 0) ans *= -1 ;

            return Double.parseDouble(String.format("%.7f", ans));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
 /**
     * 判断输入是否错误
     *
     * @param context 当前窗体
     * @author HansAwake
     * @create 2025/1/2
     * @p.s. 错误列表需要在实际测试中进行完善
     **/
    public void estimate(Context context){//判断输入是否错误
        String error = "输入错误,请检查表达式.";
        callback = null;

        int i = 0;
        if (str.length() == 0){
//            Toast.makeText(context,"输入为空,请检查输入的内容",Toast.LENGTH_SHORT).show();
            callback = "输入为空,请检查输入的内容";
        }
        if (str.length() == 1){
            //当只有一位字符时,只能是“0123456789epπ”中的一个
            if ("0123456789epπ".indexOf(str.charAt(0)) == -1){
//                Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                callback = error;
                expression_checked = 1;
            }
        }
        if (str.length() > 1){
            for (i = 0; i < str.length() - 1; i++) {
                //1.第一个字符只能为"lsct√a(0123456789epπ"中的一个
                if ("lsct√a(0123456789epπ".indexOf(str.charAt(0)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                //2."+-*×/÷"后面只能是"0123456789lsct√a(epπ"中的一个
                if ("+-*×/÷".indexOf(str.charAt(i)) >= 0 && "0123456789lsct√a(epπ".indexOf(str.charAt(i + 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                //3."."后面只能是"0123456789"中的一个
                if (str.charAt(i) == '.' && "0123456789".indexOf(str.charAt(i + 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }

                //4.1."!"后面只能是"+-*/÷^)"中的一个
                if (str.charAt(i) == '!' && "+-*×/÷^)".indexOf(str.charAt(i + 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                //4.2."!"前面不能是"度分秒"中的一个
                if (str.charAt(i) == '!' && "°\"'".indexOf(str.charAt(i - 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }

                //5."lnlgsincostan√"后面只能是"0123456789(epπ"中的一个
                if ("lnlgsincostan√a".indexOf(str.charAt(i)) >= 0 && "0123456789(epπ".indexOf(str.charAt(i + 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                //6."0"的判断操作
                //两个零
                if (str.charAt(0) == '0' && str.charAt(1) == '0'){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                if (i >= 1 && str.charAt(i) == '0'){
                    //&& str.charAt(0) == '0' && str.charAt(1) == '0'
                    int m = i;  //输入字符串的长度
                    int n = i;
                    int is = 0;
                    //6.1.当0的上一个字符不为"0123456789."时,后一位只能是"+-*×/÷.!^)°\"\'"中的一个
                    if ("0123456789.".indexOf(str.charAt(m - 1)) == -1 && "+-*×/÷.!^)°\"\'".indexOf(str.charAt(i + 1)) == -1){
//                        Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                        callback = error;
                        expression_checked = 1;
                    }
                    //6.2.如果0的上一位为".",则后一位只能是"0123456789+-*×/÷^)\""中的一个
                    if (str.charAt(m - 1) == '.' && "0123456789+-*×/÷^)\"".indexOf(str.charAt(i + 1)) == -1){
//                        Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                        callback = error;
                        expression_checked = 1;
                    }

                    n -= 1;
                    while (n > 0){
                        if ("(+-*×/÷^lnlgsincostan√".indexOf(str.charAt(n)) >= 0){
                            break;
                        }
                        if (str.charAt(n) == '.'){
                            is++;
                        }
                        n--;
                    }
                    //6.3.如果0到上一个运算符之间没有"."的话,整数位第一个只能是"123456789",
                    //  且后一位只能是"0123456789+-*×/÷.!^)"中的一个。
                    if ((is == 0 && str.charAt(n) == '0') || "0123456789+-*×/÷.!^)".indexOf(str.charAt(i + 1)) == -1){
//                        Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                        callback = error;
                        expression_checked = 1;
                    }
                    //6.4.如果0到上一个运算符之间有一个"."的话,则后一位只能是"0123456789+-*×/÷^)"中的一个
                    if (is == 1 && "0123456789+-*×/÷^)".indexOf(str.charAt(i + 1)) == -1){
//                        Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                        callback = error;
                        expression_checked = 1;
                    }
                    //6.5.如果0到上一个运算符之间有多个"."的话,则是错误的表达式
                    if (is > 1){
//                        Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                        callback = error;
                        expression_checked = 1;
                    }
                }
                //7."123456789"后面只能是"0123456789+-*/÷.!^)°\"\'"中的一个
                if ("123456789".indexOf(str.charAt(i)) >= 0 && "0123456789+-*×/÷.!^)°\"\'".indexOf(str.charAt(i + 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                //8."("后面只能是"0123456789locstg()epπ"中的一个
                if (str.charAt(i) == '(' && "0123456789lscta√()epπ".indexOf(str.charAt(i + 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                //9.")"后面只能是"+-*×/÷!^)"中的一个
                if (str.charAt(i) == ')' && "+-*×/÷!^)".indexOf(str.charAt(i + 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                //10.最后一位字符只能是"0123456789!)epπ°\"\'"中的一个
                if ("0123456789!)epπ°\"\'".indexOf(str.charAt(str.length() - 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
                //11.不能有多个"."
                if (i > 2 && str.charAt(i) == '.'){
                    int n = i - 1;
                    int is = 0;
                    while (n > 0){
                        if ("(+-*×/÷^lglnsincostanarc".indexOf(str.charAt(n)) >= 0){
                            break;
                        }
                        if (str.charAt(n) == '.'){
                            is++;
                        }
                        n--;
                    }
                    if (is > 0){
//                        Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                        callback = error;
                        expression_checked = 1;
                    }
                }
                //12."epπ"后面只能是"+-*×/÷^)"中的一个
                if ("epπ".indexOf(str.charAt(i)) >= 0 && "+-*×/÷^)".indexOf(str.charAt(i + 1)) == -1){
//                    Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                    callback = error;
                    expression_checked = 1;
                }
            }
        }
    }
/**
     * 度分秒检查
     *
     * @param context 文字输出窗体
     * @author HansAwake
     * @create 2025/1/3
     * @p.s. 暂时未进行深入修改
     **/
    public void Angle_check(Context context){
        /*角度-度分秒的检查*/
        List<String> degree = new ArrayList<>();

        /*提取角度信息*/
        for (int i = 0;i<list.toArray().length;i++){
            String s = list.get(i);
            if (s.contains("°") || s.contains("'") || s.contains("\"")){
                degree.add(s);
            }
        }

        int i = 0;
        do {
            String s = degree.get(i);
            //degree.1 度\分前没有小数点,秒前只能有一个小数点

            //degree.2

            //degree.3 分秒前数字不能是>=60

            //degree.4 度分秒前不应为空


            //到上一个计算符号时,只能出现一次度分秒
        }while (i < degree.toArray().length);

    }

    /**
     * 角度判断函数
     *
     * @param angle 输入的角度(string)
     * @return boolean 返回bool
     * @author HansAwake
     * @create 2025/1/4
     **/
    public static boolean isAngle(String angle){
        return angle.contains("°") || angle.contains("'") || angle.contains("\"");
    }

    /**
     * 切换角度-将角度切换为数值
     *
     * @param strings 输入文字
     * @return java.lang.String
     * @author HansAwake
     * @create 2025/1/9
     **/
    public String AngleChange(String strings){
        Calculation_Operation cal = new Calculation_Operation();
        cal.degree_check = true;
        List<String> temp = cal.Str2infix(strings);
        for(int i = 0;i < temp.toArray().length;i++){
            if (isAngle(temp.get(i))){
                String a = Double.toString(cal.degree2num(temp.get(i)));
                temp.set(i,a);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i < temp.toArray().length;i++){
            sb.append(temp.get(i));
        }
        return sb.toString();
    }

    /**
     * 切换角度-将数值切换为角度
     *
     * @param strings 输入文字
     * @return java.lang.String
     * @author HansAwake
     * @create 2025/1/9
     **/
    public String NumberChange(String strings){
        Calculation_Operation cal = new Calculation_Operation();
        cal.degree_check = false;
        Log.i("NumberChange","in1");
        List<String> temp = cal.Str2infix(strings);
        for(int i = 0;i < temp.toArray().length;i++){
            if (!isAngle(temp.get(i)) && isNumber(temp.get(i))){
                String a = cal.num2degree(temp.get(i));
                temp.set(i,a);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i < temp.toArray().length;i++){
            sb.append(temp.get(i));
        }
        return sb.toString();
    }

    /**
     * 数学计算函数
     *
     * @param list2 待进行计算的后缀表达式
     * @param context 提示弹出的activity
     * @return double 返回double类型精度的结果
     * @author HansAwake
     * @create 2025/1/9
     **/
    public double math(List<String> list2,Context context) {
        //通过后缀表达式进行计算
        Stack<String> stack = new Stack<String>();
        callback = null;

        if (degree_check){
            for (int i = 0; i < list2.size(); i++){
                if (isAngle(list2.get(i))){
                    double s = degree2num(list2.get(i));
                    String rad = Double.toString(s / 180 * Math.PI);
                    list2.set(i,rad);
                }
            }
        }

        for (int i = 0; i < list2.size(); i++) {
            if (isNumber(list2.get(i))) {
                if (list2.get(i).charAt(0) == 'e'){
                    stack.push(String.valueOf(Math.E));
                } else if (list2.get(i).charAt(0) == 'p' || list2.get(i).charAt(0) == 'π'){
                    stack.push(String.valueOf(Math.PI));
                } else {
                    stack.push(list2.get(i));
                }
            } else if (isOperator(list2.get(i))){
                double res = 0;
                if (list2.get(i).equals("+")) {
                    double num2 = Double.parseDouble(stack.pop());
                    double num1 = Double.parseDouble(stack.pop());
                    res = num1 + num2;
                } else if (list2.get(i).equals("-")) {
                    double num2 = Double.parseDouble(stack.pop());
                    double num1 = Double.parseDouble(stack.pop());
                    res = num1 - num2;
                } else if (list2.get(i).equals("*") || list2.get(i).equals("×")) {
                    double num2 = Double.parseDouble(stack.pop());
                    double num1 = Double.parseDouble(stack.pop());
                    res = num1 * num2;
                } else if (list2.get(i).equals("/") || list2.get(i).equals("÷")) {
                    double num2 = Double.parseDouble(stack.pop());
                    double num1 = Double.parseDouble(stack.pop());
                    if (num2 != 0){
                        res = num1 / num2;
                    } else {
//                        Toast.makeText(context,"被除数不能为零",Toast.LENGTH_SHORT).show();
                        callback = "被除数不能为零";
                        System.out.println("-311");
                        expression_checked = 1;
                    }
                } else if (list2.get(i).equals("^")) {
                    double num2 = Double.parseDouble(stack.pop());
                    double num1 = Double.parseDouble(stack.pop());
                    res = Math.pow(num1, num2);
                } else if (list2.get(i).equals("!")) {
                    double num1 = Double.parseDouble(stack.pop());
                    if (num1 == 0 || num1 == 1){
                        res = 1;
                    }
                    else if (num1 == (int)num1 && num1 > 1){
                        int d = 1;
                        for (int j = (int)num1; j >0; j--) {
                            d *= j;
                        }
                        res = d;
                    }
                    else {
                        /*p.s.这里需要在之后的版本中进行修改,将负数单独添加出来*/
//                        Toast.makeText(context,"阶乘数不为负值",Toast.LENGTH_SHORT).show();
                        callback = "阶乘数不为负值";
                        System.out.println("-312");
                        expression_checked = 1;
                    }
                } else if (list2.get(i).equals("g") || list2.get(i).equals("√")) {
                    double num1 = Double.parseDouble(stack.pop());
                    res = Math.sqrt(num1);
                } else if (list2.get(i).equals("ln")) {
                    double num1 = Double.parseDouble(stack.pop());
                    if (num1 > 0){
                        res = Math.log(num1);
                    } else {
//                        Toast.makeText(context,"对数值(ln)不为负值",Toast.LENGTH_SHORT).show();
                        callback = "对数值(ln)不为负值";
                        System.out.println("-313");
                        expression_checked = 1;
                    }
                } else if (list2.get(i).equals("lg")) {
                    double num1 = Double.parseDouble(stack.pop());
                    if (num1 > 0){
                        res = Math.log10(num1);
                    } else {
//                        Toast.makeText(context,"对数值(lg)不为负值",Toast.LENGTH_SHORT).show();
                        callback = "对数值(lg)不为负值";
                        System.out.println("-314");
                        expression_checked = 1;
                    }
                }
                /*三角函数-弧度制*/
                if(!degree_check){
                    if (list2.get(i).equals("sin")) {
                        double num1 = Double.parseDouble(stack.pop());
                        res = Math.sin(num1);
                    } else if (list2.get(i).equals("cos")) {
                        double num1 = Double.parseDouble(stack.pop());
                        res = Math.cos(num1);
                    } else if (list2.get(i).equals("tan")) {
                        double num1 = Double.parseDouble(stack.pop());
                        if (Math.cos(num1) != 0){
                            res = Math.tan(num1);
                        } else {
//                            Toast.makeText(context,"x值不应该为±π",Toast.LENGTH_SHORT).show();
                            callback = "x值不应该为±π";
                            System.out.println("-314");
                            expression_checked = 1;
                        }
                    }
                    /*反三角函数-弧度制*/
                    else if (list2.get(i).equals("arctan")) {
                        double num1 = Double.parseDouble(stack.pop());
                        res = Math.atan(num1);
                    }else if (list2.get(i).equals("arcsin")) {
                        double num1 = Double.parseDouble(stack.pop());
                        if (-1 <= num1 && num1 <= 1){
                            res = Math.asin(num1);
                        } else {
//                            Toast.makeText(context,"x值域错误",Toast.LENGTH_SHORT).show();
                            callback = "x值域错误";
                            System.out.println("-315");
                            expression_checked = 1;
                        }
                    }else if (list2.get(i).equals("arccos")) {
                        double num1 = Double.parseDouble(stack.pop());
                        if (-1 <= num1 && num1 <= 1){
                            res = Math.acos(num1);
                        } else {
//                            Toast.makeText(context,"x值域错误",Toast.LENGTH_SHORT).show();
                            callback = "x值域错误";
                            System.out.println("-316");
                            expression_checked = 1;
                        }
                    }
                }
                /*三角函数-角度制*/
                if(degree_check){
                    if (list2.get(i).equals("sin")) {
                        double num1 = Double.parseDouble(stack.pop());
                        res = Math.sin(num1);
                    } else if (list2.get(i).equals("cos")) {
                        double num1 = Double.parseDouble(stack.pop());
                        res = Math.cos(num1);
                    } else if (list2.get(i).equals("tan")) {
                        double num1 = Double.parseDouble(stack.pop());
                        if (Math.cos(num1) != 0){
                            res = Math.tan(num1);
                        } else {
                            Toast.makeText(context,"x值不应该为±π",Toast.LENGTH_SHORT).show();
                            callback = "x值不应该为±π";
                            System.out.println("-314");
                            expression_checked = 1;
                        }
                    }
                }
                stack.push("" + res);
            }
        }
        if (expression_checked == 0){
            if (!stack.isEmpty()){
                return Double.parseDouble(stack.pop());
            } else {
                return 0;
            }
        } else {
            //无法处理的报错返回值
            return -999999;
        }
    }

    /**
     * 函数计算+括号匹配情况
     * 最终的计算经由此进行
     *
     * @param context 提示窗activity界面
     * @author HansAwake
     * @create 2025/1/9
     **/
    public String calculate(String s,Context context){
        //进行计算,并且判断括号是否匹配
        this.str = s;
        callback = null;

        StringBuilder khao = new StringBuilder();
        double  ans = 0;
        int leftkh = 0;
        int rightkh = 0;
        int m = 0;
        //判断括号是否成对
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '('){
                khao.append('(');
                leftkh++;
            }
            if (str.charAt(i) == ')'){
                khao.append(')');
                rightkh++;
            }
        }
        if (leftkh != rightkh){
//            Toast.makeText(context,"左右括号数量不匹配",Toast.LENGTH_SHORT).show();
            callback = "左右括号数量不匹配";
            System.out.println("-411");
            expression_checked = 1;
            return null;
        }
        if ((leftkh == 0 && rightkh == 0) || ((leftkh == rightkh && leftkh > 0) && khao.charAt(0) == '(' && khao.charAt(khao.length() - 1) == ')')){
            if (expression_checked == 0){
                List<String> list1 = Str2infix(str);
                //System.out.println(list1);
                List<String> list2 = infix2suffix(list1);
                //System.out.println(list2);
                if (expression_checked == 0){
                    ans = math(list2,context);
                    if (ans == -999999){
//                        Toast.makeText(context,"表达式输入错误",Toast.LENGTH_SHORT).show();
                        callback = "表达式输入错误";
                        System.out.println("-412");
                        return null;
                    } else {
                        //计算正确
                        System.out.println("1");
                        return String.valueOf(ans);
                    }
                }
                return null;
            }
        } else {
            System.out.println("-414");
            expression_checked = 1;
            return null;
        }
        return null;
    }
}

代码较多,读者可以自己拼一下

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!--最外层-->
<LinearLayout 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="match_parent"
    android:background="@color/Flower_white"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--顶部工具栏-->
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/calculator_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="4dp"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:title="科学计算器" />
        <!--文字显示区-->
        <ScrollView
            android:id="@+id/calculator_zone_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/calculator_toolbar">
            <EditText
                android:id="@+id/calculator_text_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:gravity="center_vertical|right"
                android:textSize="25sp" />
        </ScrollView>
        <EditText
            android:text="="
            android:id="@+id/calculator_text_ans"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_above="@id/cal_btn_contianer"
            android:background="@null"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:textSize="25sp"
            tools:ignore="RtlHardcoded" />
        <!--下方按钮-->
        <!--按钮区外层-->
        <LinearLayout
            android:id="@+id/cal_btn_contianer"
            android:layout_width="match_parent"
            android:layout_height="490dp"
            android:layout_alignParentBottom="true"
            android:orientation="vertical"
            android:paddingStart="15dp"
            android:paddingTop="5dp"
            android:paddingEnd="15dp"
            android:paddingBottom="15dp">
            <!--按钮区-1-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <!--按钮区-1-1-->
                <LinearLayout
                    android:id="@+id/cal_btn_2nd"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onClickRadian">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="2nd"
                        android:textSize="20sp" />
                </LinearLayout>
                <!--按钮区-1-2-->
                <LinearLayout
                    android:id="@+id/cal_btn_degree"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onDegree">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="角度"
                        android:textSize="16sp" />
                </LinearLayout>
                <!--按钮区-1-3-->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onClickSin">
                    <TextView
                        android:id="@+id/cal_btn_sin"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="sin"
                        android:textSize="20sp" />
                </LinearLayout>
                <!--按钮区-1-4-->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onClickCos">
                    <TextView
                        android:id="@+id/cal_btn_cos"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="cos"
                        android:textSize="20sp" />
                </LinearLayout>
                <!--按钮区-1-5-->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onClickTan">
                    <TextView
                        android:id="@+id/cal_btn_tan"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tan"
                        android:textSize="20sp" />
                </LinearLayout>
            </LinearLayout>
            <!--按钮区-2-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <!--按钮区-2-1-->
                <LinearLayout
                    android:id="@+id/cal_btn_lg"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text="@string/logarithm_cal"
                        android:textSize="19sp" />
                </LinearLayout>
                <!--按钮区-2-2-->
                <LinearLayout
                    android:id="@+id/cal_btn_ln"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text="@string/logarithm_cal2"
                        android:textSize="18sp" />
                </LinearLayout>
                <!--按钮区-2-3-->
                <LinearLayout
                    android:id="@+id/cal_btn_power"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text="@string/power"
                        android:textSize="19sp" />
                </LinearLayout>
                <!--按钮区-2-4-->
                <LinearLayout
                    android:id="@+id/cal_btn_lkh"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="("
                        android:textSize="20sp" />
                </LinearLayout>
                <!--按钮区-2-5-->
                <LinearLayout
                    android:id="@+id/cal_btn_rkh"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=")"
                        android:textSize="20sp" />
                </LinearLayout>
            </LinearLayout>
            <!--按钮区-3-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <!--按钮区-3-1-->
                <LinearLayout
                    android:id="@+id/cal_btn_pi"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="π"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-3-2-->
                <LinearLayout
                    android:id="@+id/cal_btn_clear"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onCalculateClear">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="AC"
                        android:textSize="21sp" />
                </LinearLayout>
                <!--按钮区-3-3-->
                <LinearLayout
                    android:id="@+id/cal_btn_back"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onBackspace">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        android:textSize="23sp" />
                </LinearLayout>
                <!--按钮区-3-4-->
                <LinearLayout
                    android:id="@+id/cal_btn_percent"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="%"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-3-5-->
                <LinearLayout
                    android:id="@+id/cal_btn_div"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="÷"
                        android:textSize="25sp" />
                </LinearLayout>
            </LinearLayout>
            <!--按钮区-4-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <!--按钮区-4-1-->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onClickDegree">
                    <TextView
                        android:id="@+id/cal_btn_square"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text="@string/square"
                        android:textSize="20sp" />
                </LinearLayout>
                <!--按钮区-4-2-->
                <LinearLayout
                    android:id="@+id/cal_btn_7"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="7"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-4-3-->
                <LinearLayout
                    android:id="@+id/cal_btn_8"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="8"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-4-4-->
                <LinearLayout
                    android:id="@+id/cal_btn_9"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="9"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-4-5-->
                <LinearLayout
                    android:id="@+id/cal_btn_muti"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="×"
                        android:textSize="25sp" />
                </LinearLayout>
            </LinearLayout>
            <!--按钮区-5-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <!--按钮区-5-1-->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onClickMinute">
                    <TextView
                        android:id="@+id/cal_btn_factorial"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="X!"
                        android:textSize="20sp" />
                </LinearLayout>
                <!--按钮区-5-2-->
                <LinearLayout
                    android:id="@+id/cal_btn_4"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="4"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-5-3-->
                <LinearLayout
                    android:id="@+id/cal_btn_5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="5"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-5-4-->
                <LinearLayout
                    android:id="@+id/cal_btn_6"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="6"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-5-5-->
                <LinearLayout
                    android:id="@+id/cal_btn_minus"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="-"
                        android:textSize="28sp" />
                </LinearLayout>
            </LinearLayout>
            <!--按钮区-6-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <!--按钮区-6-1-->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onClickSecond">
                    <TextView
                        android:id="@+id/cal_btn_root"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="√X"
                        android:textSize="20sp" />
                </LinearLayout>
                <!--按钮区-6-2-->
                <LinearLayout
                    android:id="@+id/cal_btn_1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="1"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-6-3-->
                <LinearLayout
                    android:id="@+id/cal_btn_2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="2"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-6-4-->
                <LinearLayout
                    android:id="@+id/cal_btn_3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="3"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-6-5-->
                <LinearLayout
                    android:id="@+id/cal_btn_plus"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="+"
                        android:textSize="25sp" />
                </LinearLayout>
            </LinearLayout>
            <!--按钮区-7-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <!--按钮区-7-1-->
                <LinearLayout
                    android:id="@+id/cal_btn_decimal"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="1/x"
                        android:textSize="20sp" />
                </LinearLayout>
                <!--按钮区-7-2-->
                <LinearLayout
                    android:id="@+id/cal_btn_e"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="e"
                        android:textSize="23sp" />
                </LinearLayout>
                <!--按钮区-7-3-->
                <LinearLayout
                    android:id="@+id/cal_btn_0"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="0"
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-7-4-->
                <LinearLayout
                    android:id="@+id/cal_btn_point"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="."
                        android:textSize="25sp" />
                </LinearLayout>
                <!--按钮区-7-5-->
                <LinearLayout
                    android:id="@+id/cal_btn_ans"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/angle_rounded_cal_btn_2"
                    android:clickable="true"
                    android:foreground="@drawable/animation_calculator"
                    android:gravity="center"
                    android:onClick="onCalculateCal">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="="
                        android:textColor="@color/white"
                        android:textSize="23sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

效果图如下:

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隐-梵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值