0

Could you please look at my code and help me with adding square root(√) percent(%) and backspace (<-) buttons to my project. I think the problem is with the CalcUtils Java code if you want I can add also the MainActivity.java and activity_main.xml files.

public final class CalcUtils {
private CalcUtils(){

}

public static boolean isOperator(char c){
    return c == '/' ||
            c == '÷' ||
            c == '*' ||
            c == '-' ||
            c == '+';
}

public static double evaluate(final String str){
    class Parser {
        int pos = -1, c;

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

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

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

        // Grammar:
        // expression = term | expression `+` term | expression `-` term
        // term = factor | term `*` factor | term `/` factor | term brackets
        // factor = brackets | number | factor `^` factor
        // brackets = `(` expression `)`

        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; // unary minus is applied after exponentiation; e.g. -3^2=-9
            return v;
        }
    }
    return new Parser().parse();
}
A.Atik
  • 3
  • 2
  • If you're just strugling on how to render those chars inside a button, you could try unicode chars, check that out: http://stackoverflow.com/questions/15672161/print-a-square-root-symbol-%E2%88%9A-in-java Not sure what backspace character you want it though. – Rogério Peixoto Jun 20 '16 at 20:45

1 Answers1

0

Not sure what is that backspace character you want.

But you could set text of your button with a unicode char for square root or any other unicode symbol you want for example:

Button mySquareRootButton = (Button) findViewById(R.id.my_square_root_button);
mySquareRootButton.setText("\u221A");

You could do also do it in you layout's xml:

 android:text="\u221A"
Rogério Peixoto
  • 2,176
  • 2
  • 23
  • 31