-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
80 lines (74 loc) · 1.58 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo01</title>
</head>
<body>
<h1>demo01:栈的创建</h1>
<h4>请打开调试工具(F12)</h4>
</body>
</html>
<script>
//创建一个类来表示栈
function Stack(){
//使用数组来保存栈里的元素
var items = [];
//压栈
this.push = function(element){
items.push(element);
};
//出栈
this.pop = function(){
return items.pop();
};
//返回栈顶元素
this.peek = function(){
return items[items.length - 1];
};
//判断栈是否为空
this.isEmpty = function(){
return items.length == 0;
};
//栈的元素个数
this.size = function(){
return items.length;
};
//清空栈
this.clear = function(){
items = [];
};
//输出全栈
this.print = function(){
console.log(items.toString());
};
}
// ----------------简单使用stack类---------------------------
// var stack = new Stack();
// console.log(stack.isEmpty()); // true
// stack.push(5);
// console.log(stack.isEmpty()); //false
// stack.push(8);
// stack.print();
//------------------使用stack实现进制转换---------------------
function baseTenToTwo(baseTen,base){
var remStack = new Stack();
var rem;
var result = '';
var digits = '0123456789ABCDEF';
while(baseTen>0){
rem = baseTen%base; //余数压栈
remStack.push(rem);
baseTen = Math.floor(baseTen/base); //商做被除数
}
while(!remStack.isEmpty())
{
result += digits[remStack.pop()]; //依次出栈
}
return result;
}
//10进制转2进制
console.log(baseTenToTwo(10,2)); //1010
//10进制转3进制
console.log(baseTenToTwo(10,3)); //101
</script>