设为首页 加入收藏

TOP

Java十进制数换算二进制数(栈实现)
2014-11-23 23:57:18 来源: 作者: 【 】 浏览:2
Tags:Java 十进制数 换算 二进制数 实现

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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇中华网 招聘java ,C#,PHP开发,.. 下一篇如何用英语表达“相关经验”

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: