Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.Scanner;
- public class postfix {
- public static void main(String[] args) {
- myStack s = new myStack();
- Scanner input = new Scanner(System.in);
- System.out.println("Please input string");
- String stringInput = input.nextLine();
- String[] numbers = stringInput.split(" ");
- for (int i= 0; i< numbers.length; i++){
- try {
- int tempholder = Integer.parseInt(numbers[i]);
- s.push(numbers[i]);
- }
- catch(NumberFormatException e){
- String op = numbers[i];
- try{
- int num2 = Integer.parseInt(s.pop());
- int num1 = Integer.parseInt(s.pop());
- if (op.equals("+")){
- s.push(Integer.toString((num1 + num2)));
- }
- else if (op.equals("-")){
- s.push(Integer.toString((num1 + num2)));
- }
- else if (op.equals("*")){
- s.push(Integer.toString((num1 * num2)));
- }
- else if (op.equals("/")){
- s.push(Integer.toString((num1 / num2)));
- }
- }
- catch(Exception f){
- System.out.println("Pop Failed");
- }
- try{
- s.pop();
- }
- catch (Exception g){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement