class Stack {
constructor(){
this.dataStore = [];
this.top = 0;
}
push(element){
this.dataStore[this.top++] = element;
}
pop(){
return this.dataStore[--this.top];
}
peek(){
return this.dataStore[this.top - 1];
}
length(){
return this.top
}
clear(){
this.top = 0;
}
}
function mulBase(num, base){
const s = new Stack();
do{
s.push(num % base);
num = Math.floor(num /= base);
}while(num > 0);
let converted = "";
while(s.length() > 0){
converted += s.pop();
}
return converted;
}
let num = 32;
let base = 2;
let newNum = mulBase(num, base);
console.log(num + " 转化为 " + base + " 进制:" + newNum);
num = 125;
base = 8;
newNum = mulBase(num, base);
console.log(num + " 转化为 " + base + " 进制:" + newNum);
function reverse(str){
const s = new Stack();
for(let i = 0, len = str.length; i< len; i++){
s.push(str[i]);
}
let res = '';
while(s.length() > 0){
res+=s.pop();
}
return res;
}
console.log(reverse('asdfghjkl'))