0

I am doing an exercise and I am having some problems to do a combined operation.

This is my first attempt to make the code where it is able to do multiple doubles operations but with the same sign. Example (33.5+6+2+2+44.5)

This method works fine

if (!exitOrHelp(respuesta)) {
    if (respuesta.contains("+")) {
        double solucion = 0;
        String[] numeros = respuesta.split("\\+");

        for (String a : numeros) {
            solucion = solucion + Double.parseDouble(a);
        }
        System.out.println(solucion);
        System.out.println("Introduzca otra operación");
    } else if (respuesta.contains("-")) {
        double solucion = 0;
        String[] numeros = respuesta.split("-");

        for (String a : numeros) {
            solucion = solucion - Double.parseDouble(a);
        }
        System.out.println(solucion);
        System.out.println("Introduzca otra operación");

    } else if (respuesta.contains("*") || (respuesta.contains("x"))) {
        double solucion = 1;
        String[] numeros = respuesta.split("\\*");

        for (String t : numeros) {
            solucion = solucion * Double.parseDouble(t);
        }
        System.out.println(solucion);
        System.out.println("Introduzca otra operación");
    } else if (respuesta.contains("/")) {
        double solucion = 0;
        String[] numeros = respuesta.split("/");
        solucion = Double.parseDouble(numeros[0]) / Double.parseDouble(numeros[1]);

        if (numeros.length > 2) {
            for (int r = 2; r < numeros.length; r++) {
                solucion = solucion / Double.parseDouble(numeros[r]);
            }
        }
        System.out.println(solucion);
        System.out.println("Introduzca otra operación");
    } else {
        System.out.println("No introdujiste ninguno de estos operandos: '+,-,*,/'");
    }
}

This is the 2nd test where I try to do combined operations. This method only works for ints since the program only takes the value that is before and only for numbers that only have one digit. Example: 3*7/2/2+4

double calc = 0;
String scan = "3*7/2/2+4";
int resta = scan.indexOf('/');

while (resta > 0) {
    calc = Double.parseDouble(scan.substring(resta - 1, resta)) / Double.parseDouble(scan.substring(resta + 1, resta + 2));
    String new_scan = scan.substring(0, resta - 1) + (int) calc + scan.substring(resta + 2, scan.length());
    scan = new_scan;
    resta = scan.indexOf('/');
}
System.out.println("Scan 1: " + scan);
int resta2 = scan.indexOf('*');
while (resta2 > 0) {
    calc = Double.parseDouble(scan.substring(resta2 - 1, resta2)) * Double.parseDouble(scan.substring(resta2 + 1, resta2 + 2));
    String new_scan = scan.substring(0, resta2 - 1) + (int) calc + scan.substring(resta2 + 2, scan.length());
    scan = new_scan;
    resta2 = scan.indexOf('*');
}
System.out.println("Scan 2: " + scan);
int resta3 = scan.indexOf('+');
while (resta3 > 0) {
    calc = Double.parseDouble(scan.substring(resta3 - 1, resta3)) + Double.parseDouble(scan.substring(resta3 + 1, resta3 + 2));
    String new_scan = scan.substring(0, resta3 - 1) + (int) calc + scan.substring(resta3 + 2, scan.length());
    scan = new_scan;
    resta3 = scan.indexOf('+');
}
System.out.println("Scan 3: " + scan);
int resta4 = scan.indexOf('-');
while (resta4 > 0) {
    calc = Double.parseDouble(scan.substring(resta4 - 1, resta4)) - Double.parseDouble(scan.substring(resta4 + 1, resta4 + 2));
    String new_scan = scan.substring(0, resta4 - 1) + (int) calc + scan.substring(resta4 + 2, scan.length());
    scan = new_scan;
    resta4 = scan.indexOf('-');
}
System.out.println("Scan 4: " + scan);

After several hours of trying various methods, I have run out of ideas and therefore I ask for help.

The final objective I am looking for is to be able to do multiple operations of doubles, negative numbers and 2 digits. Example: -73.5*4+744/2+76-6

I know more or less how the structure has to be, but I have not been able to develop it. I attach a picture of what I think it would look like.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jcorderop
  • 1
  • 1
  • Does this answer your question? [How to evaluate a math expression given in string form?](https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form) – PM 77-1 Nov 19 '21 at 20:46
  • It "does" but it uses things that i dident learn yet. I will wait if anyone else has an idea. :) – Jcorderop Nov 19 '21 at 21:08
  • Expression parsing is not an easy task when you deal with variable number of operands and operations. It's well described in books and articles though. So good luck with that. – PM 77-1 Nov 19 '21 at 21:11
  • Thx! :) Hope i get it jeje – Jcorderop Nov 19 '21 at 21:20
  • So right now you are just doing everything in one method. That works for a simple calculator, but if you want to create a more complex calculator you save yourself alot of trouble if you start working object-oriented. A good start could be to create a `Node` that contains one part of the calculation (for example *7) and can be evaluated against another `Node`. – magicmn Nov 21 '21 at 11:52

0 Answers0