Search code examples
javastringmathcalculatorevaluate

Evaluate math expressions that have percentages


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?


Solution

  • 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;
        }
    

    :)