Stack类计算器函数java实现,能够进行函数表达式运算,自动解析算数表达式,支持加减乘除

 废话不多说,直接贴代码,复制就能用


import java.util.Stack;

/**
 * 算术运算类
 * by lcf
 * 2022/6/27
 *
 */
public class SuanShu {


    /***
     * 表达式计算支持加减乘除
     * @param expstr 表达式,比如 1+1=2
     * @return 计算结果
     */
    public static double expressionEvaluation(String expstr) {
        Stack s1 = new Stack();
        String[] hou = new String[30];
        String[] hao = new String[30];
        int i = 0;
        int j = 0;
        boolean fu = false;

        while(true) {
            while(i < expstr.length()) {
                String shu = "";
                char ch = expstr.charAt(i);
                switch(ch) {
                    case '(':
                        shu = shu + ch;
                        hao[j] = shu;
                        ++i;
                        ++j;
                        continue;
                    case ')':
                        shu = shu + ch;
                        hao[j] = shu;
                        ++i;
                        ++j;
                        continue;
                    case '*':
                    case '/':
                        shu = shu + ch;
                        hao[j] = shu;
                        ++i;
                        ++j;
                        continue;
                    case '+':
                        shu = shu + ch;
                        hao[j] = shu;
                        ++i;
                        ++j;
                        continue;
                    case '-':
                        if (i > 0 && expstr.charAt(i - 1) == '(') {
                            fu = true;
                            ++i;
                            continue;
                        }

                        if (i == 0) {
                            fu = true;
                            ++i;
                        } else {
                            shu = shu + ch;
                            hao[j] = shu;
                            ++i;
                            ++j;
                        }
                        continue;
                    case 'C':
                        shu = shu + ch;
                        hao[j] = shu;
                        ++i;
                        ++j;
                        continue;
                    case 'K':
                        shu = shu + ch;
                        hao[j] = shu;
                        ++i;
                        ++j;
                        continue;
                }

                while(ch >= '0' && ch <= '9' || ch == '.') {
                    shu = shu + ch;
                    ++i;
                    if (i < expstr.length()) {
                        ch = expstr.charAt(i);
                    } else {
                        ch = '=';
                    }
                }

                if (fu) {
                    shu = '-' + shu;
                    fu = false;
                }

                hao[j] = shu;
                ++j;
            }

            int t = 0;
            int o = 0;

            while(true) {
                while(t < j) {
                    if (hao[t].equals("+")) {
                        while(!s1.empty() && !s1.peek().equals("(")) {
                            hou[o] = (String)s1.pop();
                            ++o;
                        }

                        s1.push(hao[t]);
                        ++t;
                    } else if (hao[t].equals("-")) {
                        while(!s1.empty() && !s1.peek().equals("(")) {
                            hou[o] = (String)s1.pop();
                            ++o;
                        }

                        s1.push(hao[t]);
                        ++t;
                    } else if (hao[t].equals("*")) {
                        while(!s1.empty() && !s1.peek().equals("(") && !s1.peek().equals("+") && !s1.peek().equals("-")) {
                            hou[o] = (String)s1.pop();
                            ++o;
                        }

                        s1.push(hao[t]);
                        ++t;
                    } else if (hao[t].equals("/")) {
                        while(!s1.empty() && !s1.peek().equals("(") && !s1.peek().equals("+") && !s1.peek().equals("-")) {
                            hou[o] = (String)s1.pop();
                            ++o;
                        }

                        s1.push(hao[t]);
                        ++t;
                    } else if (hao[t].equals("C")) {
                        s1.push(hao[t]);
                        ++t;
                    } else if (hao[t].equals("K")) {
                        s1.push(hao[t]);
                        ++t;
                    } else if (hao[t].equals("(")) {
                        s1.push(hao[t]);
                        ++t;
                    } else if (!hao[t].equals(")")) {
                        hou[o] = hao[t];
                        ++o;
                        ++t;
                    } else {
                        for(String out = (String)s1.pop(); !out.equals("("); out = (String)s1.pop()) {
                            hou[o] = out;
                            ++o;
                        }

                        ++t;
                    }
                }

                while(!s1.isEmpty()) {
                    hou[o] = (String)s1.pop();
                    ++o;
                }

                Stack s2 = new Stack();
                int m = 0;

                while(m < o) {
                    double a;
                    double b;
                    double c;
                    String p;
                    String q;
                    String x;
                    if (hou[m].equals("+")) {
                        p = (String)s2.pop();
                        q = (String)s2.pop();
                        a = Double.parseDouble(p);
                        b = Double.parseDouble(q);
                        c = b + a;
                        x = Double.toString(c);
                        s2.push(x);
                        ++m;
                    } else if (hou[m].equals("-")) {
                        p = (String)s2.pop();
                        q = (String)s2.pop();
                        a = Double.parseDouble(p);
                        b = Double.parseDouble(q);
                        c = b - a;
                        x = Double.toString(c);
                        s2.push(x);
                        ++m;
                    } else if (hou[m].equals("*")) {
                        p = (String)s2.pop();
                        q = (String)s2.pop();
                        a = Double.parseDouble(p);
                        b = Double.parseDouble(q);
                        c = b * a;
                        x = Double.toString(c);
                        s2.push(x);
                        ++m;
                    } else if (hou[m].equals("/")) {
                        p = (String)s2.pop();
                        q = (String)s2.pop();
                        a = Double.parseDouble(p);
                        b = Double.parseDouble(q);
                        c = b / a;
                        x = Double.toString(c);
                        s2.push(x);
                        ++m;
                    } else if (hou[m].equals("C")) {
                        p = (String)s2.pop();
                        a = Double.parseDouble(p);
                        c = a * a;
                        x = Double.toString(c);
                        s2.push(x);
                        ++m;
                    } else if (hou[m].equals("K")) {
                        p = (String)s2.pop();
                        a = Double.parseDouble(p);
                        c = Math.sqrt(a);
                        x = Double.toString(c);
                        s2.push(x);
                        ++m;
                    } else {
                        s2.push(hou[m]);
                        ++m;
                    }
                }

                if (s2.empty()) {
                    return 0.0D;
                }

                String y = (String)s2.pop();
                return Double.parseDouble(y);
            }
        }
    }
}

Stack类计算器函数java实现,能够进行函数表达式运算,自动解析算数表达式,支持加减乘除

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值