SHARE
TWEET

Untitled

a guest Feb 17th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Stack
  2. {
  3.     constructor () {
  4.         this.items = []
  5.     }
  6.  
  7.     push (item) {
  8.         this.items.push(item)
  9.     }
  10.  
  11.  
  12.     pop () {
  13.         return this.items.pop()
  14.     }
  15.  
  16.     peek () {
  17.         return this.items[this.items.length - 1]
  18.     }
  19.  
  20.     isEmpty () {
  21.         return this.items.length === 0
  22.     }
  23.  
  24.     size () {
  25.         return this.items.length
  26.     }
  27.  
  28.     clear () {
  29.         this.items.length = 0
  30.     }
  31. }
  32.  
  33. export default Stack
  34.  
  35. // 通用的进制转换器
  36. let baseConvertor = (num, base = 2) => {
  37.     let remStack = new Stack()
  38.     let rem
  39.     let binaryString = ''
  40.     let digits = '0123456789ABCDEF'
  41.  
  42.     while (num > 0) {
  43.         rem = Math.floor(num % base)
  44.         remStack.push(rem)
  45.         num = Math.floor(num / base)
  46.     }
  47.  
  48.  
  49.     while(!remStack.isEmpty()) {
  50.         binaryString += remStack.pop().toString()
  51.     }
  52.  
  53.     return binaryString
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top