Advertisement
Martina312

Untitled

Feb 5th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. public class ExpressionEvaluator {
  2.  
  3. public static void main(String[] args) {
  4. Scanner in=new Scanner(System.in);
  5. String line=in.nextLine();
  6.  
  7. ArrayStack<Integer> stack=new ArrayStack<>(100);
  8. String broj="";
  9. boolean mnozenje=false;
  10.  
  11. for(int i=0;i<line.length();i++){
  12. char znak=line.charAt(i);
  13.  
  14. if(Character.isDigit(znak)){
  15. broj+=znak;
  16. if(i==line.length()-1 || !Character.isDigit(line.charAt(i+1))){
  17. stack.push(Integer.parseInt(broj));
  18. broj="";
  19. }
  20. }
  21. if((i==line.length()-1 || znak=='*' || znak=='+') && mnozenje){
  22. mnozenje=false;
  23. int num1=stack.pop();
  24. int num2=stack.pop();
  25. stack.push(num1*num2);
  26. }
  27.  
  28. if(znak=='*'){
  29. mnozenje=true;
  30. }
  31.  
  32. }
  33. int sum=0;
  34. while (!stack.isEmpty()){
  35. sum+=stack.pop();
  36. }
  37. System.out.println(sum);
  38.  
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement