package com.turlir;

import android.support.v4.util.Pair;

import java.util.ArrayList;
import java.util.List;

class Demo implements IDemo {

    @Override
    public List<Pair<String, String>> input(final String s) {
        final String str = s.trim();
        List<Pair<String, String>> arr = new ArrayList<>();
        int length = str.length();
        int index = 0;
        int offset = 0;
        do {
            char c = str.charAt(index);
            boolean f = digit(c);
            boolean l; // false
            do {
                offset++;
                if (index + offset < length) {
                    c = str.charAt(index + offset);
                    l = digit(c);
                } else {
                    break;
                }
            } while (f == l);

            String subs = str.substring(index, index + offset);
            if (f) {
                arr.add(new Pair<>(TYPE_AND, subs));
                if (index + offset < length &&
                    operator(str.charAt(index + offset))) {
                    String o = str.substring(index + offset, index + offset + 1);
                    arr.add(new Pair<>(TYPE_OR, o)); // односложный оператор, быстрый путь
                    index++;
                }

            } else {
                arr.add(new Pair<>(TYPE_OR, subs)); // односложный оператор, медленный путь
            }

            index += offset;
            offset = 0;
        } while (index < length);

        return arr;

        /*
        for (int index = 0; index < length; index++) {
            char c = str.charAt(index);
            boolean isNumber = Character.isDigit(c);
            if (!isNumber) {
                if (c == '-') {
                    boolean u = unary(str, length, index);
                    if (u) {
                        // унарный минус
                    } else {
                        // обычный минус
                    }
                } else {
                    // оператор
                }
            } else {
                // операнд
            }
        }
        */
    }

    private boolean operator(char c) {
        return c == '+' || c == '-' || c == '*' || c == '/';
    }

    private static boolean digit(char c) {
        return Character.isDigit(c) || c == '.' /*|| c == ','*/;
    }

    private static boolean unary(String str, int l, int i) {
        //final boolean unary;
        boolean last = i == l - 1;
        if (!last) {
            boolean first = i == 0; // в начале выражения
            boolean afterSymbol = !Character.isDigit(str.charAt(i - 1)); // после оператора или скобок
            return first || afterSymbol; // унарный минус
        } else {
           return false; // не доделанный минус
        }
    }

}

@@ -1492,1278 +1492,229 @@
 rr;%0A
-%0A        /*%0A        for (int index = 0; index %3C length; index++) %7B%0A            char c = str.charAt(index);%0A            boolean isNumber = Character.isDigit(c);%0A            if (!isNumber) %7B%0A                if (c == '-') %7B%0A                    boolean u = unary(str, length, index);%0A                    if (u) %7B%0A                        // %D1%83%D0%BD%D0%B0%D1%80%D0%BD%D1%8B%D0%B9 %D0%BC%D0%B8%D0%BD%D1%83%D1%81%0A                    %7D else %7B%0A                        // %D0%BE%D0%B1%D1%8B%D1%87%D0%BD%D1%8B%D0%B9 %D0%BC%D0%B8%D0%BD%D1%83%D1%81%0A                    %7D%0A                %7D else %7B%0A                    // %D0%BE%D0%BF%D0%B5%D1%80%D0%B0%D1%82%D0%BE%D1%80%0A                %7D%0A            %7D else %7B%0A                // %D0%BE%D0%BF%D0%B5%D1%80%D0%B0%D0%BD%D0%B4%0A            %7D%0A        %7D%0A        */%0A    %7D%0A%0A    private boolean operator(char c) %7B%0A        return c == '+' %7C%7C c == '-' %7C%7C c == '*' %7C%7C c == '/';%0A    %7D%0A%0A    private static boolean digit(char c) %7B%0A        return Character.isDigit(c) %7C%7C c == '.' /*%7C%7C c == ','*/;%0A    %7D%0A%0A    private static boolean unary(String str, int l, int i) %7B%0A        //final boolean unary;%0A        boolean last = i == l - 1;%0A        if (!last) %7B%0A            boolean first = i == 0; // %D0%B2 %D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D0%B5 %D0%B2%D1%8B%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F%0A            boolean afterSymbol = !Character.isDigit(str.charAt(i - 1)); // %D0%BF%D0%BE%D1%81%D0%BB%D0%B5 %D0%BE%D0%BF%D0%B5%D1%80%D0%B0%D1%82%D0%BE%D1%80%D0%B0 %D0%B8%D0%BB%D0%B8 %D1%81%D0%BA%D0%BE%D0%B1%D0%BE%D0%BA%0A            return first %7C%7C afterSymbol; // %D1%83%D0%BD%D0%B0%D1%80%D0%BD%D1%8B%D0%B9 %D0%BC%D0%B8%D0%BD%D1%83%D1%81%0A        %7D else %7B%0A           return false; // %D0%BD%D0%B5 %D0%B4%D0%BE%D0%B4%D0%B5%D0%BB%D0%B0%D0%BD%D0%BD%D1%8B%D0%B9 %D0%BC%D0%B8%D0%BD%D1%83%D1%81%0A        %7D
+    %7D%0A%0A    private boolean operator(char c) %7B%0A        return c == '+' %7C%7C c == '-' %7C%7C c == '*' %7C%7C c == '/';%0A    %7D%0A%0A    private static boolean digit(char c) %7B%0A        return Character.isDigit(c) %7C%7C c == '.' /*%7C%7C c == ','*/;
 %0A   

clear comments