Skip to content

Instantly share code, notes, and snippets.

@hamburger1984
Created June 27, 2016 09:25
Show Gist options
  • Save hamburger1984/cab5982eb3effab0c75fcba9ceef3ef7 to your computer and use it in GitHub Desktop.
Save hamburger1984/cab5982eb3effab0c75fcba9ceef3ef7 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Evaluate{
public static void main(String[] args){
String[] samples = new String[]{
" 5 + 6 * 7 /2",
"5/2",
"500/(3+2)",
"5^2/4"
};
Evaluate e = new Evaluate();
for(String s: samples){
e.run(s);
}
}
public float run(String input){
System.out.println("--- " + input + " -> " + normalize(input) + " ---");
Scanner s = new Scanner(normalize(input));
while(s.hasNext()){
System.out.println(s.next());
}
return 0.0f;
}
private String normalize(String input){
return input.replaceAll("\\s*([()+\\*/^-])\\s*", " $1 ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment