fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner s = new Scanner(System.in);
  13. String st = s.nextLine();
  14. Stack<Integer> od = new Stack<Integer>();
  15. Stack<Character> op = new Stack<Character>();
  16. int temp;
  17. int flag = 0;
  18. for(int i =0;i<st.length();i++){
  19. char c = st.charAt(i);
  20.  
  21. if(st.charAt(i) != ' '){
  22. if((c<='9') && (c>='0')){
  23. int no = 0;
  24. int j=0;
  25. for(j =i;j<st.length();j++){
  26. char h = st.charAt(j);
  27. if(h==' ' || h=='+' || h=='-' || h=='*' || h=='/'){
  28. i=j-1;
  29. break;
  30. }
  31.  
  32. else{
  33. if(j==(st.length()-1))
  34. i = j;
  35. no = no*10+ (h-'0');
  36. }
  37.  
  38. }
  39.  
  40. od.push(no);
  41. if(flag == 1){
  42. temp = od.pop();
  43. int t = od.pop();
  44. if(op.pop() == '*')
  45. od.push(t*temp);
  46. else
  47. od.push(t/temp);
  48. flag =0;
  49. }
  50. }
  51. else{
  52. if(c == '*' || c=='/'){
  53. flag = 1;
  54. }
  55. op.push(c);
  56. }
  57. }
  58. }
  59. Stack<Integer> od1 = new Stack<Integer>();
  60. Stack<Character> op1 = new Stack<Character>();
  61.  
  62. //System.out.println(od1);
  63. //System.out.println(op1);
  64. while(!od.empty()){
  65. od1.push(od.pop());
  66. }
  67. while(!op.empty()){
  68. op1.push(op.pop());
  69. }
  70. while(!op1.empty()){
  71. temp = od1.pop();
  72. int t= od1.pop();
  73. if(op1.pop() == '+')
  74. od1.push(t+temp);
  75. else
  76. od1.push(temp-t);
  77. }
  78. System.out.println(od1.pop());
  79.  
  80. }
  81. }
Success #stdin #stdout 0.1s 380608KB
stdin
42
stdout
42