栈
class Stack{
items = []
pop(){
return this.items.pop()
}
push(x){
this.items.push(x)
}
top(){
console.log(this.items.at(-1))
}
size(){
return this.items.length
}
isEmpty(){
return this.items.length === 0
}
}
进制转换
function convert(num,scale){
let remainder = 0
let stack = new Stack()
let string = ''
let code = '0123456789ABCDEF'
while(num>0){
remainder = num % scale
stack.push(remainder)
num = Math.floor(num/scale)
}
while(!stack.isEmpty()){
string += code[stack.pop()]
}
console.log(string)
}
convert(138,16)
convert(14,2)