2013년 9월 9일 월요일


(Stack구현)자바 스택을 이용한 덧셈 예제 , 자바교육, 자바알고리즘, JAVA STACK

 interface Stack {  
      public boolean isEmpty();
      public int peek();
      public void push(int theObject);
      public int pop();
 }
 class EmptyStackException extends RuntimeException {
   public EmptyStackException()   {
      super ("The stack is empty.");
   }
   public EmptyStackException (String message)   {
      super (message);
   }
}

 public class ArrayStack implements Stack   {           
             int top;         // current top of stack
             int [] stack;   // element array
              
             public ArrayStack(int initialCapacity) {
                  if (initialCapacity < 1)
                  throw new IllegalArgumentException
                                        ("initialCapacity must be >= 1");
                  stack = new int [initialCapacity] ;
                  top = -1;
             }
     
       public ArrayStack() {
      this(20);
       }
      
       public boolean isEmpty( ) {
          return top == -1;
       }      
       
        public int peek() {
               if (isEmpty() )
                     throw new EmptyStackException();
               return stack[top];
        }    

       
  public void push(int theElement) {
    // increase array size if necessary   
    if (top == stack.length - 1) ensureCapacity();
        
            // put theElement at the top of the stack 
            stack[++top] = theElement;
      }

 
     public int pop() {
            if  (isEmpty())
                  throw new EmptyStackException();
            int topElement = stack[top];
             return topElement;
      }   
   private void ensureCapacity()  {
      int[] larger = new int[stack.length*2];
      for (int index=0; index < stack.length; index++)
         larger[index] = stack[index];
      stack = larger;
   }
   public String toString() {
    if (isEmpty())
      return "<empty stack>";
    String result = "<stack :";
    for (int i = top; i >= 0; i--)
      result += stack[i] + " ";
    return result + ">";
  } // end toString
}
class StackAdd extends ArrayStack{
 private int v1, v2;
 private ArrayStack stackA = new ArrayStack();
 private ArrayStack stackB = new ArrayStack();
 private ArrayStack stackC = new ArrayStack();
 void fillStack(String s1, String s2) {
  for(int i=0; i<s1.length(); i++) {
   System.out.println(s1.charAt(i));
   if (i < s1.length()) stackA.push(Integer.parseInt(s1.charAt(i)));
  }
System.out.println("-------------------------");
  for(int i=0; i<s2.length(); i++) {
   System.out.println(s2.charAt(i));
   if (i < s2.length()) stackB.push(Integer.parseInt(s2.charAt(i)));
  }
  System.out.println(stackA.peek());
    System.out.println(stackB.peek());

 }
 void add() {

 }

 public static void main(String[] args) {
  if (args.length < 2) {
   System.out.println("Usage : java  StackAdd number1 number2");
   System.exit(1);
  }
  StackAdd s = new StackAdd();
  s.fillStack(args[0], args[1]);
 }
}



댓글 없음:

댓글 쓰기