TStack0.java
Select all
/* TStack0.java David Klick CIS 260 3/23/09 This program provides an interactive interface to an underlying stack. Items may be pushed and popped from the stack, and the user may also peek at the top item. */ import java.util.*; import java.io.*; public class TStack0 { private static BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); public enum Command { QUIT, PRINT, STATUS, HELP, PEEK, PUSH, POP, CLEAR } private static Hashtable
initCommandTable() { Hashtable
hash = new Hashtable
(); hash.put("QUIT", Command.QUIT); hash.put("Q", Command.QUIT); hash.put("EXIT", Command.QUIT); hash.put("X", Command.QUIT); hash.put("PRINT", Command.PRINT); hash.put("P", Command.PRINT); hash.put("STATUS", Command.STATUS); hash.put("STAT", Command.STATUS); hash.put("S", Command.STATUS); hash.put("HELP", Command.HELP); hash.put("H", Command.HELP); hash.put("PEEK", Command.PEEK); hash.put("?", Command.PEEK); hash.put("PUSH", Command.PUSH); hash.put("+", Command.PUSH); hash.put("POP", Command.POP); hash.put("-", Command.POP); hash.put("CLEAR", Command.CLEAR); hash.put("C", Command.CLEAR); return hash; } private static void error(String msg) { System.out.println(msg); } private static boolean isInteger(String s) { return s.matches("[+-]?[0-9]+"); } private static void println(String msg) { System.out.println(msg); } public static void main(String[] args) throws IOException { boolean done = false; MyStack stk = new MyStack(); // intialize command lookup table Hashtable
commands = initCommandTable(); // process commands displayHelp(); System.out.println("\nProgram ready for processing"); while (!done) { int position, value; String[] cmd = getCommand(); int cmdLength = cmd.length; Command command = commands.get(cmd[0].toUpperCase()); if (command == null) error("Unrecognized command"); else { switch (command) { case QUIT: if (cmdLength > 1) error("QUIT command takes no arguments"); else done = true; break; case PRINT: if (cmdLength > 1) error("PRINT command takes no arguments"); else stk.print(); break; case STATUS: if (cmdLength > 1) error("STATUS command takes no arguments"); else displayStatus(stk); break; case HELP: if (cmdLength > 1) error("HELP command takes no arguments"); else displayHelp(); break; case PEEK: if (cmdLength > 1) error("PEEK command takes no arguments"); else { try { println("" + stk.peek()); } catch (MyStackException e) { println(e.getMessage()); } } break; case PUSH: if (cmdLength != 2) error("PUSH command takes one argument"); else if (!isInteger(cmd[1])) error("PUSH command argument must be an integer"); else { stk.push(Integer.parseInt(cmd[1])); stk.print(); } break; case POP: if (cmdLength > 1) error("POP command takes no arguments"); else { try { println("Popped from stack: " + stk.pop()); stk.print(); } catch (MyStackException e) { println(e.getMessage()); } } break; case CLEAR: if (cmdLength > 1) error("CLEAR command takes no arguments"); else { try { while (!stk.isEmpty()) { println("Popped from stack: " + stk.pop()); } } catch (MyStackException e) { println(e.getMessage()); } } break; default: error("Unrecognized command"); } } } // end of command processing loop println("End of processing"); } public static String[] getCommand() { String s = new String(); System.out.print("\n> "); try { while ((s = in.readLine()).length() == 0); } catch (IOException e) { System.out.println("I/O Exception"); } s = s.toUpperCase(); StringTokenizer p = new StringTokenizer(s); String[] cmds = new String[p.countTokens()]; try { int i = 0; while (p.hasMoreTokens()) cmds[i++] = p.nextToken(); } catch (NoSuchElementException e) { System.out.println(e); } if (cmds.length > 0) { // This handles the special case where the first character // is a plus (insert) or minus (delete) and is followed by // a number without any delimiter in between. This code // breaks the single "word" into two separate "words" char c = cmds[0].charAt(0); if ((cmds[0].length() > 1) && ((c == '+') || (c == '-'))) { String[] newCmds = new String[cmds.length + 1]; for (int i=1; i
v; public MyStack() { v = new Vector
(); } public void clear() { v.clear(); } public int length() { return v.size(); } public void push(int n) { v.addElement(new Integer(n)); } public int pop() throws MyStackException { int n = peek(); v.removeElementAt(v.size()-1); return n; } public int peek() throws MyStackException { if (isEmpty()) throw new MyStackException("Stack empty"); else return ((Integer) (v.lastElement())).intValue(); } public boolean isEmpty() { return v.isEmpty(); } public String toString() { return v.toString(); } public void print() { System.out.println(v.toString()); } } class MyStackException extends Exception { public MyStackException() {} public MyStackException(String msg) { super(msg); } }