Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. @SuppressWarnings("serial")
  4. class CollectionException extends Exception {
  5.     public CollectionException(String msg) {
  6.          super(msg);
  7.     }
  8. }
  9.  
  10. interface Collection {
  11.     static final String ERR_MSG_EMPTY = "Collection is empty.";
  12.     static final String ERR_MSG_FULL = "Collection is full.";
  13.     boolean isEmpty();
  14.     boolean isFull();
  15.     int count();
  16.     String toStringg();
  17. }
  18.  
  19. interface Stack<T> extends Collection {
  20.     T top() throws CollectionException;
  21.     void push(T x) throws CollectionException;
  22.     T pop() throws CollectionException;
  23.     void dup() throws CollectionException;
  24.     void dup2() throws CollectionException;
  25.     void echo() throws CollectionException;
  26.     void swap() throws CollectionException;
  27.     void reverse();
  28.     void even() throws CollectionException;
  29.     void odd() throws CollectionException;
  30.     void faculty() throws CollectionException;
  31.     void greaterSmaller() throws CollectionException;
  32.     void lessThan() throws CollectionException;
  33.     void lessEqual() throws CollectionException;
  34.     void equal() throws CollectionException;
  35.     void greater() throws CollectionException;
  36.     void greaterEqual() throws CollectionException;
  37.     void addition() throws CollectionException;
  38.     void substraction() throws CollectionException;
  39.     void multiplication() throws CollectionException;
  40.     void division() throws CollectionException;
  41.     void remainderAtDivision() throws CollectionException;
  42.     void concate() throws CollectionException;
  43.     void rnd() throws CollectionException;  
  44. }
  45.  
  46. interface Deque<T> extends Collection {
  47.     T front() throws CollectionException;
  48.     T back() throws CollectionException;
  49.     void enqueue(T x) throws CollectionException;
  50.     void enqueueFront(T x) throws CollectionException;
  51.     T dequeue() throws CollectionException;
  52.     T dequeueBack() throws CollectionException;
  53. }
  54.  
  55.  
  56. interface Sequence<T> extends Collection {
  57.     static final String ERR_MSG_INDEX = "Wrong index in sequence.";
  58.     T get(int i) throws CollectionException;
  59.     T set(int i, T x) throws CollectionException;
  60. }
  61.  
  62. /**
  63.  * @author Jure Zajc
  64.  *
  65.  * @param <T>
  66.  */
  67.  
  68. class ArrayDeque<T> implements Deque<T>, Stack<T>, Sequence<T> {
  69.    
  70.     private static final int DEFAULT_CAPACITY = 64;
  71.  
  72.     @SuppressWarnings("unchecked")
  73.     T[] items = (T[]) new Object[DEFAULT_CAPACITY];
  74.     int front;
  75.     int back;
  76.     int count;
  77.  
  78.     @SuppressWarnings("unchecked")
  79.     public ArrayDeque() {
  80.         @SuppressWarnings("unused")
  81.         T[] items = (T[]) new Object[DEFAULT_CAPACITY];
  82.         @SuppressWarnings("unused")
  83.         int front = 0;
  84.         @SuppressWarnings("unused")
  85.         int back = 0;
  86.         @SuppressWarnings("unused")
  87.         int count = 0;
  88.     }
  89.  
  90.     public int count() {
  91.         return count;
  92.     }
  93.  
  94.     public boolean isEmpty() {
  95.         return count == 0;
  96.     }
  97.  
  98.     public boolean isFull() {
  99.         return count == items.length;
  100.     }
  101.  
  102.     public String toStringg() {
  103.         StringBuilder finalString = new StringBuilder("[");
  104.         if (count > 0) {
  105.                 finalString.append(items[front]);
  106.                 for (int i = 1; i < count; i++) {
  107.                         finalString.append(", ");
  108.                         finalString.append(items[(front + i) % items.length]);
  109.                 }
  110.         }
  111.         finalString.append("]");
  112.         return finalString.toString();
  113.     }
  114.  
  115.     public T front() throws CollectionException {
  116.         if (isEmpty())
  117.                 throw new CollectionException(ERR_MSG_EMPTY);
  118.         return items[front];
  119.     }
  120.  
  121.     public T back() throws CollectionException {
  122.         if (isEmpty())
  123.                 throw new CollectionException(ERR_MSG_EMPTY);
  124.         return items[(back - 1 + items.length) % items.length];
  125.     }
  126.  
  127.     public void enqueue(T x) throws CollectionException {
  128.         if (isFull())
  129.                 throw new CollectionException(ERR_MSG_FULL);
  130.         items[back] = x;
  131.         back = (back + 1) % items.length;
  132.         count++;
  133.     }
  134.  
  135.     public void enqueueFront(T x) throws CollectionException {
  136.         if (isFull())
  137.                 throw new CollectionException(ERR_MSG_FULL);
  138.         front = (front - 1 + items.length) % items.length;
  139.         items[front] = x;
  140.         count++;
  141.     }
  142.  
  143.     public T dequeue() throws CollectionException {
  144.         if (isEmpty())
  145.                 throw new CollectionException(ERR_MSG_EMPTY);
  146.         T temp = items[front];
  147.         items[front] = null;
  148.         front = (front + 1) % items.length;
  149.         count--;
  150.         return temp;
  151.     }
  152.  
  153.     public T dequeueBack() throws CollectionException {
  154.         if (isEmpty())
  155.                 throw new CollectionException(ERR_MSG_EMPTY);
  156.         T temp = items[(back - 1 + items.length) % items.length];
  157.         items[(back - 1 + items.length) % items.length] = null;
  158.         back = (back - 1 + items.length) % items.length;
  159.         count--;
  160.         return temp;
  161.     }
  162.  
  163.     public T pop() throws CollectionException {
  164.         if (isEmpty())
  165.             throw new CollectionException(ERR_MSG_EMPTY);
  166.         return dequeueBack();
  167.     }
  168.  
  169.     public void push(T x) throws CollectionException {
  170.         if (isFull())
  171.             throw new CollectionException(ERR_MSG_FULL);
  172.         enqueue(x);
  173.     }
  174.  
  175.     public T top() throws CollectionException {
  176.         if (isEmpty())
  177.             throw new CollectionException(ERR_MSG_EMPTY);
  178.         return back();
  179.     }
  180.  
  181.     public void dup() throws CollectionException {
  182.         if (isEmpty())
  183.             throw new CollectionException(ERR_MSG_EMPTY);
  184.         enqueue(back());
  185.     }
  186.  
  187.     public void dup2() throws CollectionException {
  188.         if (isEmpty())
  189.             throw new CollectionException(ERR_MSG_EMPTY);
  190.         T temp1 = dequeueBack();
  191.         T temp2 = dequeueBack();
  192.         enqueue(temp2); enqueue(temp1); enqueue(temp2); enqueue(temp1);
  193.     }
  194.  
  195.     public void echo() throws CollectionException {
  196.         if (isEmpty())
  197.             System.out.println("");
  198.         else
  199.             System.out.println(back());
  200.     }
  201.  
  202.     public void swap() throws CollectionException {
  203.         if (isEmpty())
  204.             throw new CollectionException(ERR_MSG_EMPTY);
  205.         T temp1 = dequeueBack();
  206.         T temp2 = dequeueBack();
  207.         enqueue(temp1); enqueue(temp2);
  208.     }
  209.  
  210.     @SuppressWarnings("unchecked")
  211.     public void reverse() {
  212.         T[] elements = (T[]) new Object[DEFAULT_CAPACITY];
  213.         for (int i = 0; i < count; i++) {
  214.             elements[i] = items[count - 1 - i];
  215.         }
  216.         items = elements;
  217.     }
  218.  
  219.     public T get(int i) throws CollectionException {
  220.         if (i < 0 || i >= count)
  221.                 throw new CollectionException(ERR_MSG_INDEX);
  222.         return items[(front + i) % items.length];
  223.     }
  224.  
  225.     public T set(int i, T x) throws CollectionException {
  226.         if (i < 0 || i > count)
  227.                 throw new CollectionException(ERR_MSG_INDEX);
  228.         int pos = (front + i) % items.length;
  229.         T temp = items[pos];
  230.         items[pos] = x;
  231.         if (i == count) {
  232.                 count++;
  233.                 back = (back + 1) % items.length;
  234.         }
  235.         return temp;
  236.     }
  237.  
  238.     @SuppressWarnings("unchecked")
  239.     public void even() throws CollectionException {
  240.         if (Integer.parseInt((String) dequeueBack()) %2 ==0)
  241.             push((T) "1");
  242.         else
  243.             push((T) "0");
  244.     }
  245.  
  246.     @SuppressWarnings("unchecked")
  247.     public void odd() throws CollectionException {
  248.         if (Integer.parseInt((String) dequeueBack()) %2 ==0)
  249.             push((T) "0");
  250.         else
  251.             push((T) "1");
  252.     }
  253.  
  254.     @SuppressWarnings("unchecked")
  255.     public void faculty() throws CollectionException {
  256.         int top = Integer.parseInt((String) dequeueBack());
  257.         int facul = 1;
  258.         for (int i = top; i > 0; i--) {
  259.             facul *= i;
  260.         }
  261.         push((T) Integer.toString(facul));;
  262.        
  263.     }
  264.  
  265.     @SuppressWarnings("unchecked")
  266.     public void greaterSmaller() throws CollectionException {
  267.         String top1 = (String) dequeueBack();
  268.         String top2 = (String) dequeueBack();
  269.         String compare;
  270.         if (!top2.equals(top1))
  271.             compare = "1";
  272.         else
  273.             compare = "0";
  274.         push((T) compare); 
  275.     }
  276.  
  277.     @SuppressWarnings("unchecked")
  278.     public void lessThan() throws CollectionException {
  279.         int top1 = Integer.parseInt((String) dequeueBack());
  280.         int top2 = Integer.parseInt((String) dequeueBack());
  281.         String compare;
  282.         if (top2 < top1)
  283.             compare = "1";
  284.         else
  285.             compare = "0";
  286.         push((T) compare);     
  287.     }
  288.  
  289.     @SuppressWarnings("unchecked")
  290.     public void lessEqual() throws CollectionException {
  291.         int top1 = Integer.parseInt((String) dequeueBack());
  292.         int top2 = Integer.parseInt((String) dequeueBack());
  293.         String compare;
  294.         if (top2 <= top1)
  295.             compare = "1";
  296.         else
  297.             compare = "0";
  298.         push((T) compare);     
  299.     }
  300.  
  301.     @SuppressWarnings("unchecked")
  302.     public void equal() throws CollectionException {
  303.         int top1 = Integer.parseInt((String) dequeueBack());
  304.         int top2 = Integer.parseInt((String) dequeueBack());
  305.         String compare;
  306.         if (top2 == top1)
  307.             compare = "1";
  308.         else
  309.             compare = "0";
  310.         push((T) compare);
  311.     }
  312.  
  313.     @SuppressWarnings("unchecked")
  314.     public void greater() throws CollectionException {
  315.         int top1 = Integer.parseInt((String) dequeueBack());
  316.         int top2 = Integer.parseInt((String) dequeueBack());
  317.         String compare;
  318.         if (top2 > top1)
  319.             compare = "1";
  320.         else
  321.             compare = "0";
  322.         push((T) compare);     
  323.     }
  324.  
  325.     @SuppressWarnings("unchecked")
  326.     public void greaterEqual() throws CollectionException {
  327.         int top1 = Integer.parseInt((String) dequeueBack());
  328.         int top2 = Integer.parseInt((String) dequeueBack());
  329.         String compare;
  330.         if (top2 >= top1)
  331.             compare = "1";
  332.         else
  333.             compare = "0";
  334.         push((T) compare);         
  335.     }
  336.  
  337.     @SuppressWarnings("unchecked")
  338.     public void addition() throws CollectionException {
  339.         int top1 = Integer.parseInt((String) dequeueBack());
  340.         int top2 = Integer.parseInt((String) dequeueBack());
  341.         push((T)Integer.toString(top2 + top1));    
  342.     }
  343.  
  344.     @SuppressWarnings("unchecked")
  345.     public void substraction() throws CollectionException {
  346.         int top1 = Integer.parseInt((String) dequeueBack());
  347.         int top2 = Integer.parseInt((String) dequeueBack());
  348.         push((T) Integer.toString(top2 - top1));
  349.        
  350.     }
  351.  
  352.     @SuppressWarnings("unchecked")
  353.     public void multiplication() throws CollectionException {
  354.         int top1 = Integer.parseInt((String) dequeueBack());
  355.         int top2 = Integer.parseInt((String) dequeueBack());
  356.         push((T) Integer.toString(top2 * top1));
  357.        
  358.     }
  359.  
  360.     @SuppressWarnings("unchecked")
  361.     public void division() throws CollectionException {
  362.         int top1 = Integer.parseInt((String) dequeueBack());
  363.         int top2 = Integer.parseInt((String) dequeueBack());
  364.         push((T) Integer.toString(top2 / top1));
  365.        
  366.     }
  367.  
  368.     @SuppressWarnings("unchecked")
  369.     public void remainderAtDivision() throws CollectionException {
  370.         int top1 = Integer.parseInt((String) dequeueBack());
  371.         int top2 = Integer.parseInt((String) dequeueBack());
  372.         push((T) Integer.toString(top2 % top1));
  373.        
  374.     }
  375.    
  376.     @SuppressWarnings("unchecked")
  377.     public void concate() throws CollectionException {
  378.         String concate1 = (String) dequeueBack();
  379.         String concate2 = (String) dequeueBack();
  380.         push((T) (concate2 + concate1));
  381.        
  382.     }
  383.  
  384.     @SuppressWarnings("unchecked")
  385.     public void rnd() throws CollectionException {
  386.         int top1 = Integer.parseInt((String) dequeueBack());
  387.         int top2 = Integer.parseInt((String) dequeueBack());
  388.         int rnd = (int) (Math.random() * (top2 - top1 + 1)) + top1;
  389.         push((T) Integer.toString(rnd));     
  390.        
  391.     }
  392. }
  393. //glavni class
  394. public class Naloga1 {
  395.  
  396.     static Sequence<Stack<String>> allStacks;
  397.     static boolean condition;
  398.  
  399.     public static void main(String[] args) throws Exception {
  400.         String[] commands = new String[0];
  401.         if (true) {
  402.             @SuppressWarnings("resource")
  403.             Scanner sc = new Scanner(System.in);
  404.             String newLine = null;
  405.             while (sc.hasNextLine()) {//naredimo 42 stackov
  406.                 allStacks = new ArrayDeque<Stack<String>>();
  407.                 for (int i = 0; i < 42; i++) {
  408.                     allStacks.set(i, new ArrayDeque<String>());
  409.                 }
  410.                 condition = false;
  411.                 newLine = sc.nextLine();
  412.                 if (!newLine.trim().isEmpty()) {
  413.                     commands = newLine.trim().split("\\s+");
  414.                     parseCommands(commands);
  415.                 }
  416.             }
  417.         }      
  418.     }
  419.  
  420.     public static void parseCommands(String[] commands) throws CollectionException {
  421.         Stack<String> mainStack = allStacks.get(0);
  422.         for (int i = 0; i < commands.length; i++) {
  423.             if (commands[i].toCharArray()[0] == '?') {
  424.                 if (condition)
  425.                     commands[i] = commands[i].substring(1);
  426.             }
  427.              switch (commands[i])
  428.                 {
  429.                     case "echo": mainStack.echo();break;
  430.                     case "pop":  mainStack.pop();break;
  431.                     case "dup":  mainStack.dup();break;
  432.                     case "dup2": mainStack.dup2();break;
  433.                     case "swap": mainStack.swap();break;
  434.                     case "char": mainStack.push(Character.toString((char) Integer.parseInt(mainStack.pop())));break;
  435.                     case "even": mainStack.even();break;
  436.                     case "odd":  mainStack.odd();;break;
  437.                     case "!":    mainStack.faculty();break;
  438.                     case "len":  mainStack.push(Integer.toString(mainStack.pop().length()));break;
  439.                     case "<>":   mainStack.greaterSmaller();break;
  440.                     case "==":   mainStack.equal();break;
  441.                     case "<":    mainStack.lessThan();break;
  442.                     case "<=":   mainStack.lessEqual();break;
  443.                     case ">":    mainStack.greater();break;
  444.                     case ">=":   mainStack.greaterEqual();break;
  445.                     case "+":    mainStack.addition();break;
  446.                     case "-":    mainStack.substraction();break;
  447.                     case "*":    mainStack.multiplication();break;
  448.                     case "/":    mainStack.division();break;
  449.                     case "%":    mainStack.remainderAtDivision();break;
  450.                     case ".":    mainStack.concate();break;
  451.                     case "rnd":  mainStack.rnd();break;
  452.                     case "then":
  453.                         String state = mainStack.pop();
  454.                         if (state.equals("0")) {
  455.                             condition = false;
  456.                         } else {
  457.                             condition = true;
  458.                         }break;
  459.                     case "else":  condition = !condition;break;
  460.                     case "print":
  461.                         Stack<String> auxStack = allStacks.get(Integer.parseInt(mainStack.pop()));
  462.                         int len = auxStack.count();
  463.                         auxStack.reverse();
  464.                         String[] elements = new String[len];
  465.                         for (int j = 0; j < len; j++) {
  466.                             elements[j] = auxStack.pop();
  467.                             System.out.print(elements[j] + " ");
  468.                         }
  469.                         for (int j = 0; j < elements.length; j++) {
  470.                             auxStack.push(elements[j]);
  471.                         }System.out.println(); break;
  472.                     case "clear":
  473.                         Stack<String> clearStack = allStacks.get(Integer.parseInt(mainStack.pop()));
  474.                         int sizeClear = clearStack.count();
  475.                         for (int j = 0; j < sizeClear; j++) {
  476.                             clearStack.pop();
  477.                         } break;
  478.                     case "run":
  479.                         Stack<String> runStack = allStacks.get(Integer.parseInt(mainStack.pop()));
  480.                         int lenRun = runStack.count();
  481.                         runStack.reverse();
  482.                         String[] finalElements = new String[lenRun];
  483.                         for (int j = 0; j < lenRun; j++) {
  484.                             finalElements[j] = runStack.pop();
  485.                         }
  486.                         for (int j = 0; j < finalElements.length; j++) {
  487.                             runStack.push(finalElements[j]);
  488.                         }
  489.                         parseCommands(finalElements);break;
  490.                     case "loop":
  491.                         Stack<String> loopStack = allStacks.get(Integer.parseInt(mainStack.pop()));
  492.                         int lenLoopAgainThisCase = loopStack.count();
  493.                         int numRepeats = Integer.parseInt(mainStack.pop());
  494.                         for (int j = 0; j < numRepeats; j++) {
  495.                             loopStack.reverse();
  496.                             String[] elementsWhyIDoneWithCases = new String[lenLoopAgainThisCase];
  497.                             for (int k = 0; k < lenLoopAgainThisCase; k++) {
  498.                                 elementsWhyIDoneWithCases[k] = loopStack.pop();
  499.                             }
  500.                             for (int k = 0; k < elementsWhyIDoneWithCases.length; k++) {
  501.                                 loopStack.push(elementsWhyIDoneWithCases[k]);
  502.                             }
  503.                             parseCommands(elementsWhyIDoneWithCases);
  504.                         } break;
  505.                     case "fun":
  506.                         Stack<String> justAnotherStack = allStacks.get(Integer.parseInt(mainStack.pop()));
  507.                         int numCommands = Integer.parseInt(mainStack.pop());
  508.                         for (int j = 0; j < numCommands; j++) {
  509.                             justAnotherStack.push(commands[i + 1 + j]);
  510.                         }
  511.                         i += numCommands;break;
  512.                     case "move":
  513.                         Stack<String> whyWeCantHaveSameVariableNames = allStacks.get(Integer.parseInt(mainStack.pop()));
  514.                         int someCommandsToDo = Integer.parseInt(mainStack.pop());
  515.                         for (int j = 0; j < someCommandsToDo; j++) {
  516.                             whyWeCantHaveSameVariableNames.push(mainStack.pop());
  517.                         }break;
  518.                     case "reverse":  allStacks.get(Integer.parseInt(mainStack.pop())).reverse();break;             
  519.                     default:
  520.                         if (commands[i].charAt(0) != '?') {
  521.                             mainStack.push(commands[i]);
  522.                         }break;
  523.                 }
  524.             }          
  525.         }
  526. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement