fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void parse(String in)
  12. {
  13. try {
  14. // New string that will have spaces before the operators
  15. String in2 = "";
  16. // Insert spaces
  17. for (char c : in.toCharArray()) {
  18. if (c == '+' || c == '-' || c == '*' || c == '/') {
  19. in2 += ' ';
  20. }
  21. in2 += c;
  22. }
  23. // Scanner to parse the string
  24. Scanner inp = new Scanner(in2);
  25. int p1 = inp.nextInt();
  26. String op = inp.next();
  27. int p2 = inp.nextInt();
  28. solve(p1, op, p2);
  29. }
  30. catch (Exception e) {
  31. // End up here if something went wrong
  32. System.out.println(e);
  33. }
  34. }
  35.  
  36. public static int solve(int p1, String op, int p2)
  37. {
  38. try {
  39. // Do the calculation
  40. int answer = 0;
  41. switch (op) {
  42. case "+":
  43. answer = p1 + p2;
  44. break;
  45. case "-":
  46. answer = p1 - p2;
  47. break;
  48. case "*":
  49. answer = p1 * p2;
  50. break;
  51. case "/":
  52. // Note: will throw if p2 == 0
  53. answer = p1 / p2;
  54. break;
  55. default:
  56. }
  57. System.out.println(p1 + op + p2 + "=" + answer);
  58. return answer;
  59. }
  60. catch (Exception e) {
  61. // End up here if something went wrong
  62. System.out.println(e);
  63. return 0;
  64. }
  65. }
  66.  
  67. public static void main (String[] args) throws java.lang.Exception
  68. {
  69. parse("-4443- +397");
  70. parse(" -8844443 / -73897 ");
  71. parse(" 4433* -7 ");
  72. parse(" +4 * -5897");
  73. }
  74. }
Success #stdin #stdout 0.24s 2841600KB
stdin
Standard input is empty
stdout
-4443-397=-4840
-8844443/-73897=119
4433*-7=-31031
4*-5897=-23588