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

06-01 1013阅读

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

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

java代码如下:

public List Str2infix(String str){
        //中缀表达式列表初始长度
        int index = 0;
        this.list = null;
        //表达式列表
        List 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 = 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            }
            /*三角函数处理组*/
            else if ("s".indexOf(str.charAt(index)) >= 0) {
                //sin
                String str1 = "";
                while (index = 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            } else if ("c".indexOf(str.charAt(index)) >= 0) {
                //cos
                String str1 = "";
                while (index = 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            } else if ("t".indexOf(str.charAt(index)) >= 0) {
                //tan
                String str1 = "";
                while (index = 0){
                    str1 += str.charAt(index);
                    index ++;
                }
                list.add(str1);
            }
            /*反三角函数组*/
            else if ("a".indexOf(str.charAt(index)) >= 0) {
                //arc
                String str1 = "";
                while (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 = 0){
                        str1 += str.charAt(index);
                        index ++;
                    }
                } else {
                    //角度相关功能
                    while (index = 0){
                        str1 += str.charAt(index);
                        index ++;
                    }
                }
                list.add(str1);
            }
        }while (index  
/**
     * 判断当前接收的字符串是否为操作符
     *
     * @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 传回的后缀表达式
     * @author HansAwake
     * @create 2024/12/31
     * @edit 2025/1/2
     **/
    public List infix2suffix(List list){
        //中缀表达式转换称后缀表达式
        //使用堆栈存储字符情况
        Stack operator = new Stack();
        //后缀表达式存储列表
        List list2 = new ArrayList();
        if (!list.isEmpty()) {//传入的中缀表达式非空判断
            for (int i = 0; i  
 /**
     * 判断输入是否错误
     *
     * @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 = 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 degree = new ArrayList();
        /*提取角度信息*/
        for (int i = 0;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  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 
                            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 
                            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 
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码