进制转换过程
实现
<!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>
<h1>题目描述</h1>
<h4>输入一个大于0的十进制数,将其转换为二进制</h4>
<h1>解题思路</h1>
<h4>10进制转2进制实际上就是用10不断去除以2,将余数从下往上返回即可</h4>
<h1>尝试使用</h1>
请输入一个大于0的十进制数(一个大于0的整数):
<input type="text" id="input" />
<br />
<button id="btn">计算</button>
<br /> 2进制结果:
<input type="text" id="output" />
<br />
<h1>代码实现</h1>
<pre>
<code>
const ten2Two = function(number) {
console.log(number);
// 声明一个栈
const stack = new Stack();
// 声明一个字符串
let binary = "";
// 入栈
while (number > 0) {
// 取余入栈
stack.push(number % 2);
// 取整
number = Math.floor(number / 2);
}
// 出栈
while (stack.size() > 0) {
// 拼接字符串
binary += stack.pop();
}
return binary;
};
document.getElementById("btn").addEventListener("click", function() {
const inputValue = document.getElementById("input").value;
// 字符类型转数值类型
const outputValue = main(inputValue - 0);
document.getElementById("output").value = outputValue;
});
</code>
</pre>
<body>
<script src="../../Stack.js"></script>
<script>
const main = function(number) {
console.log(number);
// 声明一个栈
const stack = new Stack();
// 声明一个字符串
let binary = "";
// 入栈
while (number > 0) {
// 取余入栈
stack.push(number % 2);
// 取整
number = Math.floor(number / 2);
}
// 出栈
while (stack.size() > 0) {
// 拼接字符串
binary += stack.pop();
}
return binary;
};
document.getElementById("btn").addEventListener("click", function() {
const inputValue = document.getElementById("input").value;
// 字符类型转数值类型
const outputValue = main(inputValue - 0);
document.getElementById("output").value = outputValue;
});
</script>
</body>
</html>
Stack.js
// 栈
const Stack = function(item) {
// 不使用因为const item = [];当使用Stack创建多个栈时,使用的都是这个[]
// const item = [];
this.item = item ? item : [];
// 不使用this.push的原因是因为这样做,每一个new出来的对象都会去开辟一块空间用来保存这个方法,因为this会指向对象
// 入栈
this.__proto__.push = function(Element) {
this.item.push(Element);
};
// 出栈
this.__proto__.pop = function() {
return this.item.pop();
};
// 获取整个栈
this.__proto__.getStack = function() {
return this.item;
};
// 检查栈顶元素
this.__proto__.peek = function() {
return this.item[this.item.length - 1];
};
// 检查栈是否为空
this.__proto__.isEmpty = function() {
return this.item.length == 0;
};
// 清除栈
this.__proto__.clear = function() {
this.item = [];
};
// 获取栈的长度
this.__proto__.size = function() {
return this.item.length;
};
};