fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Stack s = new Stack();
  10. String a = " 1 6 / ";
  11. Scanner t = new Scanner(a);
  12. int result = 0;
  13.  
  14. while(t.hasNext())
  15. {
  16. if(t.hasNextInt())
  17. {
  18. int num = t.nextInt();
  19. s.push(num);
  20. }
  21. else
  22. {
  23. String operator = (String)t.next();
  24.  
  25. int op1, op2;
  26.  
  27. if(s.isEmpty())
  28. throw new RuntimeException ("not enough operants");
  29. op2 = (int)s.pop();
  30.  
  31. if(s.isEmpty())
  32. throw new RuntimeException ("not enough operator");
  33. op1 = (int)s.pop();
  34.  
  35. if(operator.equals("+"))
  36. result = op2 + op1;
  37. else if(operator.equals("-"))
  38. result = op2-op1;
  39. else if(operator.equals("*"))
  40. result = op2*op1;
  41. else if(operator.equals("/"))
  42. result = op2/op1;
  43. else
  44. throw new RuntimeException ("unrecognize operands");
  45. }
  46. }
  47. System.out.println(result);
  48. }
  49. }
Success #stdin #stdout 0.1s 380672KB
stdin
Standard input is empty
stdout
6