<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 栈结构 后进先出
// push // 添加一个
// pop // 出栈
// peek 返回栈顶
// isEmpty
// clear
// size
// tostring
class Stack {
#items = []
pop(){
return this.#items.pop()
}
push(data){
this.#items.push(data);
}
peek(){
return this.#items[this.#items.length - 1]
}
isEmpty(){
return this.#items.length === 0
}
size() {
return this.#items.length
}
clear() {
this.#items = []
}
toString(){
return this.#items.join(' ')
}
}
// 进制转换
function convert(decNumber,base){
let remStack = new Stack()
let number = decNumber
let string = ''
let baseString = '0123456789ABCDEF'
while(number > 0){
remStack.push(number%base)
number = Math.floor(number/base)
}
while(!(remStack.isEmpty())){
string += baseString[remStack.pop()]
}
return string
}
let res = convert(50,2)
console.log(res)
let res2 = convert(50,8)
console.log(res2)
let res3 = convert(500,16)
console.log(res3)
</script>
</body>
</html>