1

I am making a Java calculator with graphic interface and have been looking for a function that evaluates a math expression from a string. I found How to evaluate a math expression given in string form?. After simplifying it, because my calculator is not that complex, I ended up with this function:

// Esto es de StackOverflow, solo que quitando cosas que no se usaban
// https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form
public double calcular() {

    // Obtener la expresión matemática como una cadena
    String str = this.out.getText();

    // Crear un nuevo objeto anónimo para evaluar la expresión
    return new Object() {
        int pos = -1, ch;

        // Método para obtener el siguiente caracter de la cadena
        void nextChar() {
            ch = (++pos < str.length()) ? str.charAt(pos) : -1;
        }

        // Método para verificar si el caracter actual coincide con el pasado por
        // parámetro
        boolean eat(int charToEat) {
            while (ch == ' ')
                nextChar();
            if (ch == charToEat) {
                nextChar();
                return true;
            }
            return false;
        }

        // Método principal para evaluar la expresión
        double parse() {
            nextChar();
            double x = parseExpression();
            if (pos < str.length())
                throw new RuntimeException("No esperado: " + (char) ch);
            return x;
        }

        // Método para evaluar la expresión
        double parseExpression() {
            double x = parseTerm();
            for (;;) {
                if (eat('+'))
                    x += parseTerm(); // suma
                else if (eat('-'))
                    x -= parseTerm(); // resta
                else
                    return x;
            }
        }

        // Método para evaluar los términos
        double parseTerm() {
            double x = parseFactor();
            for (;;) {
                if (eat('*'))
                    x *= parseFactor(); // multiplicación
                else if (eat('/'))
                    x /= parseFactor(); // división
                else
                    return x;
            }
        }

        // Método para evaluar los factores
        double parseFactor() {
            double x;
            int startPos = this.pos;
            if (eat('+'))
                return +parseFactor(); // más unario
            if (eat('-'))
                return -parseFactor(); // menos unario
            while ((ch >= '0' && ch <= '9') || ch == '.')
                nextChar();
            x = Double.parseDouble(str.substring(startPos, this.pos));
            return x;
        }
    }.parse();
}

This is how it triggers the function

boton_equ.setOnMouseClicked((event) -> {
    this.out.setText(String.valueOf(calcular()));
});

I wanted to add a function that if the string is ("5*2%") it show the result, meaning that it can process % symbols. How can I add this functionality to this code?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Zeninツ
  • 175
  • 1
  • 9
  • 1
    You will need to add logic to your parser to identify the `%` operator and convert the number in front of it to a decimal value. On a side note, I recommend that you create a named class whether than using an anonymous class with `new Object{...}`. – Code-Apprentice Apr 03 '23 at 15:42
  • 3
    You need to extend the `while ((ch >= '0' && ch <= '9') || ch == '.')` loop in `parseFactor()` to also accept the `%` character. Then you need to do some manual conversion if `str.substring(startPos, this.pos)` contains that character. – f1sh Apr 03 '23 at 15:44
  • Yup, @f1sh this helped me to solve it, thanks! :) – Zeninツ Apr 03 '23 at 15:57

1 Answers1

2

The code to handle % in the expresion, would be a simple modification like in the metoth parseFactor() change it to

    // Método para evaluar los factores
    double parseFactor() {
        double x;
        int startPos = this.pos;
        if (eat('+'))
            return +parseFactor(); // más unario
        if (eat('-'))
            return -parseFactor(); // menos unario
        while ((ch >= '0' && ch <= '9') || ch == '.' || ch == '%')
            nextChar();
        // Esto para que funcione con los %
        if (str.substring(startPos, this.pos).contains("%"))
            x = Double.parseDouble(str.substring(startPos, this.pos).replace("%", "")) / 100;
        else
            x = Double.parseDouble(str.substring(startPos, this.pos));

        return x;
    }

:)

Zeninツ
  • 175
  • 1
  • 9