首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何实现Android(Java)计算器逻辑?

如何实现Android(Java)计算器逻辑?
EN

Stack Overflow用户
提问于 2018-07-25 04:50:23
回答 1查看 0关注 0票数 0

有以下代码:

代码语言:txt
复制
  public static double evaluate(final String str){
    class Parser {
        int pos = -1;
        int c;

        void eatChar() {
            c = (++pos < str.length()) ? str.charAt(pos) : -1;
        }

        void eatSpace() {
            while (Character.isWhitespace(c)) eatChar();
        }

        //Everything below this point confuses me xD

        double parse() {
            eatChar();
            double v = parseExpression();
            if (c != -1) throw new RuntimeException("Unexpected: " + (char)c);
            return v;
        }


        double parseExpression() {
            double v = parseTerm();
            for (;;) {
                eatSpace();
                if (c == '+') { // addition
                    eatChar();
                    v += parseTerm();
                } else if (c == '-') { // subtraction
                    eatChar();
                    v -= parseTerm();
                } else {
                    return v;
                }
            }
        }

        double parseTerm() {
            double v = parseFactor();
            for (;;) {
                eatSpace();
                if (c == '/' || c == '÷') { // division
                    eatChar();
                    v /= parseFactor();
                } else if (c == '*' || c == '(') { // multiplication
                    if (c == '*') eatChar();
                    v *= parseFactor();
                } else {
                    return v;
                }
            }
        }

        double parseFactor() {
            double v;
            boolean negate = false;
            eatSpace();
            if (c == '+' || c == '-') { // unary plus & minus
                negate = c == '-';
                eatChar();
                eatSpace();
            }
            if (c == '(') { // brackets
                eatChar();
                v = parseExpression();
                if (c == ')') eatChar();
            } else { // numbers
                int startIndex = this.pos;
                while ((c >= '0' && c <= '9') || c == '.') eatChar();
                if (pos == startIndex) throw new RuntimeException("Unexpected: " + (char)c);
                v = Double.parseDouble(str.substring(startIndex, pos));
            }

            eatSpace();
            if (c == '^') { // exponentiation
                eatChar();
                v = Math.pow(v, parseFactor());
            }
            if (negate) v = -v;
            return v;
        }
    }
    return new Parser().parse();
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-25 13:56:30

这样试试:

代码语言:txt
复制
Get the first factor
while there's operators (+,-,/,% etc) after that factor,
     then get the next factor and perform that operation with the two factors
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档