chillurbrain

8. Стек с защитой от ошибок.

May 21st, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         int[] stack = new int[10000];
  8.         int size = 0;
  9.         String command;
  10.  
  11.         while (true) {
  12.             command = sc.nextLine();
  13.             if (command.contains("push")) {
  14.                 stack[size++] = Integer.parseInt(command.substring(5));
  15.                 System.out.println("ok");
  16.             }
  17.             if (command.contains("pop")) {
  18.                 if (size <= 0) {
  19.                     System.out.println("error");
  20.                 } else {
  21.                     System.out.println(stack[size - 1]);
  22.                     stack[size--] = 0;
  23.                 }
  24.             }
  25.             if (command.contains("back")) {
  26.                 if (size <= 0) {
  27.                     System.out.println("error");
  28.                 } else {
  29.                     System.out.println(stack[size - 1]);
  30.                 }
  31.             }
  32.             if (command.contains("size")) {
  33.                 System.out.println(size);
  34.             }
  35.             if (command.contains("clear")) {
  36.                 if (size == 0) {
  37.                     System.out.println("ok");
  38.                 } else {
  39.                     for (int i = 0; i < size; i++) {
  40.                         stack[i] = 0;
  41.                     }
  42.                     size = 0;
  43.                     System.out.println("ok");
  44.                 }
  45.             }
  46.             if (command.contains("exit")) {
  47.                 System.out.println("bye");
  48.                 break;
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment