栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

import java.util.Stack;


public class SuanShu {


    
    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实现,能够进行函数表达式运算,自动解析算数表达式,支持加减乘除

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号