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.