栈的应用-数制转换
package com.wrh.lab.dataStructure.stackAndQueue.practise;
import com.wrh.lab.dataStructure.stackAndQueue.Stack;
import com.wrh.lab.dataStructure.stackAndQueueImpl.LinkedStackImpl;
/**
*
* @author wrh
* number system conversion
* convert decimal system to other system
*/
public class NumberConversion {
private int numberSystem; //the number system
private int number; //the number that is converted
/**
* constructor
*/
public NumberConversion(int numberSystem, int number) {
this.number = number;
this.numberSystem = numberSystem;
}
/**
* convert the number to a float
*
*/
public void convert() {
Stack<Integer> s = new LinkedStackImpl<Integer>();
//int num = 1;
int modNum = 0;
while (0 != number) {
modNum = number % numberSystem;
number = number / numberSystem;
s.push(modNum);
}
//s.pop();
while (!s.isEmpty()) {
System.out.print(s.pop());
}
}
public static void main(String[] args) {
NumberConversion n = new NumberConversion(4,8);
n.convert();
}
}
跟我走啊~~