Java十进制数换算二进制数(栈实现)

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 126 收藏 0 点赞 0 评论 0

import java.io.*;
public class textBoard {
public Node head = null;

public static void main(String[] args){
String s = “”;
int num = 0;
textBoard board = new textBoard();
System.out.print(“Please input a Decimal number : “);
//Input function!
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
num = Integer.parseInt(s);
}catch(IOException e) {}
System.out.println(“Your entering Binary number is: ” + board.swap(num));
}

public String swap(int i){
while(i != 1) {
Node node = new Node(i % 2);
push(node);
i = i / 2;
}
Node node1 = new Node(1);
push(node1);
String s = “”;
while(!isEmpty()){
s += head.getkey();
head = head.getnext();
}
return s;
}

public void push(Node element){
if(element != null){
element.setnext(head);
head = element;
}
}

public boolean isEmpty(){
if(head == null){
return true;
}
return false;
}
}
class Node {
private int key = 0;
private Node next = null;

public Node(int i) {
this.key = i;
this.next = null;
}

public int getkey(){
return key;
}

public void setkey(int i){
this.key = i;
}

public Node getnext(){
return next;
}

public void setnext(Node next){
this.next = next;
}
}

评论列表
文章目录