Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.Scanner;
  3.  
  4. public class postfix {
  5. public static void main(String[] args) {
  6. myStack s = new myStack();
  7.  
  8. Scanner input = new Scanner(System.in);
  9. System.out.println("Please input string");
  10. String stringInput = input.nextLine();
  11. String[] numbers = stringInput.split(" ");
  12. for (int i= 0; i< numbers.length; i++){
  13. try {
  14. int tempholder = Integer.parseInt(numbers[i]);
  15. s.push(numbers[i]);
  16. }
  17. catch(NumberFormatException e){
  18. String op = numbers[i];
  19. try{
  20. int num2 = Integer.parseInt(s.pop());
  21. int num1 = Integer.parseInt(s.pop());
  22. if (op.equals("+")){
  23. s.push(Integer.toString((num1 + num2)));
  24. }
  25. else if (op.equals("-")){
  26. s.push(Integer.toString((num1 + num2)));
  27. }
  28. else if (op.equals("*")){
  29. s.push(Integer.toString((num1 * num2)));
  30. }
  31. else if (op.equals("/")){
  32. s.push(Integer.toString((num1 / num2)));
  33. }
  34. }
  35. catch(Exception f){
  36. System.out.println("Pop Failed");
  37. }
  38. try{
  39. s.pop();
  40. }
  41. catch (Exception g){
  42. }
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement