2013년 8월 5일 월요일

자바 스택을 이용한 더하기

소스 참고 하세요~

Add With JAVA STACK


오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의) 



[ArrayStack.java]

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

             /* create a stack with the given initial capacity   */
             public ArrayStack(int initialCapacity) {
                  if (initialCapacity < 1)
                  throw new IllegalArgumentException
                                        ("initialCapacity must be >= 1");
                  stack = new int [initialCapacity] ;
                  top = -1;
            }

  /** create a stack with initial capacity 10  **/
       public ArrayStack() {
    this(20);
       }
       /** return true if stack is empty  */
       public boolean isEmpty( ) {
          return top == -1;
       }      
        /** return top element of stack
           * throw EmptyStackException when the stack is empty */
        public int peek() {
               if (isEmpty() )
                     throw new EmptyStackException();
               return stack[top];
        }    


/** add theElement to the top of the stack    */      
  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;
      }


/** remove top element of stack and return it
   * throw EmptyStackException when the stack is empty */
     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
}




[StackAdd.java]

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]);
}
}


[출처]오라클자바커뮤니티, 오엔제이프로그래밍
www.onjprogramming.co.kr


댓글 없음:

댓글 쓰기