Filip_Markoski

[NP] Генерички магацин (Stack)

Nov 24th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. import java.util.Date;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4. import java.util.stream.IntStream;
  5.  
  6. class EmptyStackException extends Exception {
  7. }
  8.  
  9. class Stack<T> {
  10.     T array[];
  11.     int depth;
  12.  
  13.     Stack() {
  14.         this.array = (T[]) new Object[20];
  15.         this.depth = 0;
  16.     }
  17.  
  18.     public boolean isEmpty() {
  19.         return depth == 0;
  20.     }
  21.  
  22.     public T peek() throws EmptyStackException {
  23.         if (depth == 0) throw new EmptyStackException();
  24.         return array[depth - 1];
  25.     }
  26.  
  27.     public void push(T element) {
  28.         if (depth == array.length) {
  29.             resize();
  30.         }
  31.         array[depth++] = element;
  32.     }
  33.  
  34.     public T pop() throws EmptyStackException {
  35.         if (depth == 0) throw new EmptyStackException();
  36.         T topmost = array[--depth];
  37.         array[depth] = null;
  38.         return topmost;
  39.     }
  40.  
  41.     private void resize() {
  42.         T[] newArray = (T[]) (new Object[2 * depth]);
  43.         IntStream.range(0, depth).forEach(i -> newArray[i] = array[i]);
  44.         array = newArray;
  45.     }
  46. }
  47.  
  48. public class StackTest {
  49.  
  50.  
  51.     public static void main(String[] args) throws EmptyStackException {
  52.         Scanner jin = new Scanner(System.in);
  53.         int k = jin.nextInt();
  54.         System.out.println("Test#" + k);
  55.         if (k == 0) {
  56.             System.out.println("testing: Stack::push(T) , Stack::pop():T , T is Integer");
  57.             int n = jin.nextInt();
  58.             Stack<Integer> stack = new Stack<Integer>();
  59.             System.out.println("Pushing elements:");
  60.             for (int i = 1; i <= n; ++i) {
  61.                 if (i > 1) System.out.print(" ");
  62.                 System.out.print(i);
  63.                 stack.push(i);
  64.             }
  65.             System.out.println();
  66.             System.out.println("Poping elements:");
  67.             for (int i = n; i >= 1; --i) {
  68.                 if (i < n) System.out.print(" ");
  69.                 System.out.print(stack.pop());
  70.             }
  71.             System.out.println();
  72.         }
  73.         if (k == 1) {
  74.             System.out.println("testing: Stack::push(T) , Stack::pop():T , T is String");
  75.  
  76.             int n = jin.nextInt();
  77.             Stack<String> stack = new Stack<String>();
  78.             System.out.println("Pushing elements:");
  79.             for (int i = 0; i < n; ++i) {
  80.                 if (i > 0) System.out.print(" ");
  81.                 String next = jin.next();
  82.                 System.out.print(next);
  83.                 stack.push(next);
  84.             }
  85.             System.out.println();
  86.             System.out.println("Poping elements:");
  87.             for (int i = 0; i < n; ++i) {
  88.                 if (i > 0) System.out.print(" ");
  89.                 System.out.print(stack.pop());
  90.             }
  91.         }
  92.         if (k == 2) {
  93.             System.out.println("testing: Stack::push(T) , Stack::pop():T , Stack::isEmpty():boolean, T is Double");
  94.  
  95.             Stack<Double> stack = new Stack<Double>();
  96.             System.out.println("Pushing elements:");
  97.             boolean flag = false;
  98.             while (jin.hasNextDouble()) {
  99.                 double d = jin.nextDouble();
  100.                 stack.push(d);
  101.                 if (flag) System.out.print(" ");
  102.                 System.out.printf("%.2f", d);
  103.                 flag = true;
  104.             }
  105.             int i = 0;
  106.             System.out.println();
  107.             System.out.println("Poping elements:");
  108.             while (!stack.isEmpty()) {
  109.                 if (i > 0) System.out.print(" ");
  110.                 ++i;
  111.                 System.out.printf("%.2f", stack.pop());
  112.             }
  113.         }
  114.         if (k == 3) {
  115.             System.out.println("testing: Stack::push(T) , Stack::pop():T , Stack::isEmpty():boolean , Stack::peek():T , T is Long");
  116.  
  117.             int n = jin.nextInt();
  118.             Stack<Long> stack = new Stack<Long>();
  119.             LinkedList<Long> control_stack = new LinkedList<Long>();
  120.             boolean exact = true;
  121.             for (int i = 0; exact && i < n; ++i) {
  122.                 if (Math.random() < 0.5) {//add
  123.                     long to_add = (long) (Math.random() * 456156168);
  124.                     stack.push(to_add);
  125.                     control_stack.addFirst(to_add);
  126.                 } else {
  127.                     exact &= control_stack.isEmpty() == stack.isEmpty();
  128.                     if (exact && !stack.isEmpty()) {
  129.                         if (Math.random() > 0.7) exact &= control_stack.removeFirst().equals(stack.pop());
  130.                         else exact &= control_stack.peekFirst().equals(stack.peek());
  131.                     }
  132.                 }
  133.             }
  134.             System.out.println("Your stack outputs compared to the built in java stack were the same? " + exact);
  135.         }
  136.         if (k == 4) {
  137.             System.out.println("testing: Stack::pop():T , Stack::isEmpty():boolean , Stack::peek():T , T is Long");
  138.  
  139.             Stack<Date> test_stack = new Stack<Date>();
  140.  
  141.             System.out.println("Stack empty? " + test_stack.isEmpty());
  142.             try {
  143.                 test_stack.pop();
  144.                 System.out.println("NO exeption was thrown when trying to pop from an empty stack!");
  145.  
  146.             } catch (Exception e) {
  147.                 System.out.print("Exeption thrown when trying to pop from an empty stack ");
  148.                 System.out.println(e.getClass().getSimpleName());
  149.             }
  150.             try {
  151.                 test_stack.peek();
  152.                 System.out.println("NO exeption was thrown when trying to peek in an empty stack!");
  153.             } catch (Exception e) {
  154.                 System.out.print("Exeption thrown when trying to peek in an empty stack ");
  155.                 System.out.println(e.getClass().getSimpleName());
  156.             }
  157.         }
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment