首页 > 代码库 > Demo6

Demo6

public class Main {
	public static void main(String args[])
	{
		Stack a = new Stack();
		System.out.println(a.isEmpty());
		System.out.println(a.getTop());
		a.push(new String ("3"));

		System.out.println(a.getTop());
		a.push(new String ("1"));
		a.push(new String ("9"));
		System.out.println(a.pop());
		a.push(new String ("5"));
		a.push(new String ("6"));
		System.out.println(a.getTop());
		System.out.println(a.isEmpty());
		a.trans(32);
	}
}

 

 

import java.util.Scanner;
public class Stack {

	private Object[] stack;
	private int length ;
	
	public Stack(){
		
		stack = new Object[100]; 	
	}
	
	public boolean isEmpty()
	{
		if(length==0)
		return true;
		else return false;
	}
	public int len()
	{
		return length;
	}
	public void push(Object elem)
	{
		
		stack[length++]  =  elem;
	}
	public Object pop()
	{
		return stack[--length];	
	}
	public Object getTop()
	{
		if(length == 0)
			return -1;
		else
		{
			int len = length-1;
			return stack[len];
		}
	}
	public void trans(int n)
	{
		//Scanner scan = new Scanner(System.in);
		//while(scan.hasNext())
		//{
			//int n = scan.nextInt();
			Stack a = new Stack(); 
			while(n != 0)
			{
				a.push(n % 2);
				n = n / 2;
			}
			while(a.isEmpty() == false)
			{	
				System.out.print(a.pop());
			}
				System.out.println();
		//}
	}	
}

 

Demo6