栈结构
function Stack () {
this.items = [];
if(typeof this.push != 'function') {
// 添加栈元素
Stack.prototype.push = function(element) {
this.items.push(element);
}
// 移除栈顶元素并返回
Stack.prototype.pop = function() {
return this.items.pop();
}
// 返回栈顶原元素,不修改栈
Stack.prototype.peek = function() {
return this.items[this.items.length - 1];
}
// 判断栈空
Stack.prototype.isEmpty = function() {
return this.items.length == 0
}
// 清空栈
Stack.prototype.clear = function() {
this.items = [];
}
// 栈的大小
Stack.prototype.size = function() {
return this.items.length;
}
}
}
// num是数字,base是进制
function devideChange(num, base) {
var stack = new Stack();
var str = '';
var digis = '0123456789ABCDEF';
while(num/base != 0) {
stack.push(num % base);
num = Math.floor(num / base);
}
while(!stack.isEmpty()) {
str += stack.pop().toString();
}
return str;
}
console.log(devideChange(10, 2));