Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- import java.util.NoSuchElementException;
- interface Stack<E> {
- // The elements of the Stack are any kind of objects
- // Access methods:
- public boolean isEmpty();
- // Returns true only if the stack is empty.
- public E peek();
- // Returns the element on the top od the stack.
- // Transformation methods:
- public void clear();
- // Clears the stack.
- public void push(E x);
- // Adds x on the top of the stack.
- public E pop();
- // Removes and returns the element on the top.
- }
- class ArrayStack<E> implements Stack<E> {
- private E[] elems;
- private int depth;
- @SuppressWarnings("unchecked")
- public ArrayStack(int maxDepth) {
- // Creating new empty stack
- elems = (E[]) new Object[maxDepth];
- depth = 0;
- }
- public boolean isEmpty() {
- // Returns true only if the stack is empty.
- return (depth == 0);
- }
- public E peek() {
- // Returns the element on the top od the stack.
- if (depth == 0)
- throw new NoSuchElementException();
- return elems[depth - 1];
- }
- public void clear() {
- // Clears the stack.
- for (int i = 0; i < depth; i++) elems[i] = null;
- depth = 0;
- }
- public void push(E x) {
- // Adds x on the top of the stack.
- elems[depth++] = x;
- }
- public E pop() {
- // Removes and returns the element on the top.
- if (depth == 0)
- throw new NoSuchElementException();
- E topmost = elems[--depth];
- elems[depth] = null;
- return topmost;
- }
- }
- public class PostFixEvaluation {
- static boolean isOperator(char sign) {
- if (sign == '+' || sign == '-' || sign == '*' || sign == '/') {
- return true;
- }
- return false;
- }
- static int evaluatePostfix(char symbols[], int n) {
- ArrayStack<Integer> integers = new ArrayStack<>(n);
- ArrayStack<Character> operators = new ArrayStack<>(n);
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < n; i++) {
- /* Make sure you're getting the numbers and not just their digits */
- if (Character.isDigit(symbols[i])) {
- if (Character.isDigit(symbols[i + 1])) {
- sb.append(symbols[i]);
- continue;
- } else {
- sb.append(symbols[i]);
- integers.push(Integer.parseInt(sb.toString()));
- sb.setLength(0);
- }
- }
- int result = 0;
- if (isOperator(symbols[i])) {
- char sign = symbols[i];
- if (sign == '-') {
- int temp = integers.pop();
- result = integers.pop() - temp;
- } else if (sign == '+') {
- int temp = integers.pop();
- result = integers.pop() + temp;
- } else if (sign == '/') {
- int temp = integers.pop();
- result = integers.pop() / temp;
- } else if (sign == '*') {
- int temp = integers.pop();
- result = integers.pop() * temp;
- }
- integers.push(result);
- }
- }
- return integers.pop();
- }
- public static void main(String[] args) throws Exception {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String expression = br.readLine();
- char exp[] = expression.toCharArray();
- int rez = evaluatePostfix(exp, exp.length);
- System.out.println(rez);
- br.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement