Stack of applications (binary conversion)

class Stack:
     DEF  the __init__ (Self): 
        self.items = []
     DEF isEmpty (Self):
         return self.items == []
     DEF Push (Self, Item):
         # additive element stop 
        self.items.append (item)
     DEF PEEK (Self):
         # print top element 
        return self.items [len (self.items) -1 ]
     DEF POP (Self):
         # remove elements from the stack 
        return self.items.pop ()
     DEF size (Self) :
         # returns the number of elements in the stack 
        return len (self.items)
#使用Python自带库
def divideByBase(decNumber,base):
    #进制转换,base代表进制
    remstack=Stack()
    digits="0123456789ABCDEF"
    #十六进制数
    while decNumber>0:
        rem=decNumber%base
        #取余
        remstack.push(rem)
        decNumber=decNumber//base
        #做除法取商,再次取余

    binString=""
    while not remstack.isEmpty():
        binString=binString+digits[remstack.pop()]
    return binString

print(divideByBase(1783,16))
View Code

 

Guess you like

Origin www.cnblogs.com/jzxs/p/11090384.html