Пользовательский UIToolbar изменить размер с анимацией

Для того, чтобы tokenize вы можете использовать String.split() с одним пространством в качестве разделителя;

String[] inputs = input.split(" ");

Вот полное решение, которое я только что написал, что использует основанную на Stack в одиночном списке, чтобы сделать постфиксный калькулятор;

A - PostFixCalculator

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class PostFixCalculator {

    private static final String ADD = "+"; 
    private static final String SUB = "-";
    private static final String MUL = "*";
    private static final String DIV = "/";

    public void calculateFile(String fileName) throws IOException {
        BufferedReader br = null;
        StringBuilder sb = null;
        try {
            FileReader fileReader = new FileReader(fileName);
            br = new BufferedReader(fileReader);

            sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }

            String input = sb.toString();
            System.out.println(input + " = " + calculate(input));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            br.close();
        }
    }

    private int calculate(String input) {
        SinglyLinkedListStack<Integer> stack = new SinglyLinkedListStack<>();

        String[] inputs = input.split(" ");

        return handleCalculation(stack, inputs);
    }

    private static int handleCalculation(SinglyLinkedListStack<Integer> stack, String[] el) {
        int operand1, operand2;

        for(int i = 0; i < el.length; i++) {
            if( el[i].equals(ADD) || el[i].equals(SUB) || el[i].equals(MUL) || el[i].equals(DIV) ) {
                operand2 = stack.pop();
                operand1 = stack.pop();
                switch(el[i]) {
                    case ADD: {
                        int local = operand1 + operand2;
                        stack.push(local);
                        break;
                    }

                    case SUB: {
                        int local = operand1 - operand2;
                        stack.push(local);
                        break;
                    }

                    case MUL: {
                        int local = operand1 * operand2;
                        stack.push(local);
                        break;
                    }

                    case DIV: {
                        int local = operand1 / operand2;
                        stack.push(local);
                        break;
                    }
                }
            } else {
                stack.push(Integer.parseInt(el[i]));
            }
        }

        return stack.pop();
    }

}

B - SinglyLinkedListStack

public class SinglyLinkedListStack<T> {

    private int size;
    private Node<T> head;

    public SinglyLinkedListStack() {
        head = null;
        size = 0;
    }

    public void push(T element) {
        if(head == null) {
            head = new Node(element);
        } else {
            Node<T> newNode = new Node(element);
            newNode.next = head;
            head = newNode;
        }

        size++;
    }

    public T pop() {
        if(head == null)
            return null;
        else {
            T topData = head.data;

            head = head.next;
            size--;

            return topData;
        }
    }

    public T top() {
        if(head != null)
            return head.data;
        else
            return null;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    private class Node<T> {
        private T data;
        private Node<T> next;

        public Node(T data) {
            this.data = data;
        }

    }

}

C - Demo

import java.io.IOException;

public class PostFixCalculatorDemo {
    public static void main(String[] args) throws IOException {
        PostFixCalculator calc = new PostFixCalculator();
        calc.calculateFile("postfix.txt");
    }
}

D - Пример входного файла: «postfix.txt»

6 5 2 3 + 8 * + 3 + * 

E - демонстрационный выход

6 5 2 3 + 8 * + 3 + *  = 288
0
задан Filippo 3 April 2012 в 12:02
поделиться