1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package tp1;
public abstract class CaltoNew {
public static double calculate(String arg){
String nombre1="";
double resultat=0;
String nombre2="";
String operation=null;
String tab[]=arg.split(" ");
for(String value : tab){
if(!CheckIp.isNumeric(value)){
operation = new String(value);
}
else{
if(nombre1.isEmpty()){
nombre1 = new String(value);
}
else{
nombre2 = new String(value);
}
}
if(operation == "+"){
resultat = resultat + Integer.parseInt(nombre1) + Integer.parseInt(nombre2);
}
if(operation == "-"){
resultat = resultat + Integer.parseInt(nombre1) - Integer.parseInt(nombre2);
}
if(operation == "/"){
resultat = resultat + Integer.parseInt(nombre1) / Integer.parseInt(nombre2);
}
if(operation == "*"){
resultat = resultat + Integer.parseInt(nombre1) * Integer.parseInt(nombre2);
}
}
return resultat;
}
public static void main(String[] argu){
String op = "17 + 3 * 5";
System.out.println(calculate(op));
}
} |
Partager