定义栈
function Stack() {
window.items = []
this.push = function (e) {
items.push(e)
}
this.pop = function (e) {
return items.pop()
}
this.peek = function (e) {
return items[(items.length - 1)]
}
this.isEmpty = function (e) {
return items.length == 0
}
this.size = function (e) {
return items.length
}
this.clear = function (e) {
items = []
}
this.print = function (e) {
console.log(items.toString())
}
}
定义转换方法
function baseConverter(decNumber, base) {
let remStack = new Stack(),
rem,
baseString = '',
digits = '0123456789ABCDEF'
while (decNumber > 0) {
rem = Math.floor(decNumber % base)
remStack.push(rem)
decNumber = Math.floor(decNumber / base)
}
while (!remStack.isEmpty()) {
console.log(items)
let idx = remStack.pop()
console.log(idx)
baseString += digits[idx]
}
return baseString
}
baseConverter(100345, 8)