python 数据结构 栈的实例 进制转换 除二取余 除基数取余法

# 栈的实例 除基数取余 进制转换
from pythonds.basic import Stack

def baseConverter(decNumber,base):
    digits = "0123456789ABCDEF"

    remstack = Stack()

    while decNumber > 0:
        rem = decNumber % base
        remstack.push(rem)
        decNumber = decNumber // base

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

    return newString

print(baseConverter(100,2))
print(baseConverter(100,8))
print(baseConverter(100,16))

运行结果

posted @ 2020-06-29 20:27  allmenmustdie123  阅读(517)  评论(0编辑  收藏  举报