面试题:10进制转二十五进制

时间:2021-07-06 20:07:24

import re


class DecimalToTwentyFiveHex:
    """
    面试题:十进制转换成25进制
    """
    def __init__(self, int_ten):

        self.stack = []  # 存储正整数部分转换值
        self.flag = None
        self.num_integer = None  # 存贮整数部分
        self.num_fractional = None  # 存贮小数部分

        if isinstance(int_ten, int):
            self.flag = 1
            self.num_integer = int_ten
        elif isinstance(int_ten, float):
            self.flag = 2
            self.num_integer = int(str(int_ten).split(.)[0])
            self.num_fractional = int(str(int_ten).split(.)[1])
        else:
            self.flag = 3
            print("请输入正确的数字!")
            return

    def conversion(self):
        if self.flag == 1:
            self.main_conversion(self.num_integer)
            self.stack.reverse()
        elif self.flag == 2:
            self.main_conversion(self.num_integer)
            self.stack.reverse()
            self.small_conversion(self.num_fractional)
        elif self.flag == 3:
            pass
        return self.stack

    def __call__(self, *args, **kwargs):
        return self.conversion()

    def main_conversion(self, num):
        """
        除25取余
        反转列表
        :param num:
        :return:
        """
        main_int = num // 25
        yu_int = num % 25
        self.stack.append(yu_int)
        if main_int >= 25:
            self.main_conversion(main_int)
        else:
            self.stack.append(main_int)

    def small_conversion(self, num):
        """
        乘25取整
        :param num:
        :return:
        """
        self.stack.append(.)

        main_int = str(num * 25).split(.)
        self.stack.append(main_int[0])
        if not isinstance(num*25, int):
            self.small_conversion(int(main_int[1]))



if __name__ == "__main__":
    # instantiate = DecimalToTwentyFiveHex(665.358)  # 11f.8niiiiiiie
    num_instantiate = DecimalToTwentyFiveHex(665358.22)  # 1hee8.5ccccc
    # num_instantiate = DecimalToTwentyFiveHex(665358)  # 1hee8.5cccccc
    # num_instantiate = DecimalToTwentyFiveHex(‘665358‘)  # 1hee8.5cccccc
    result = num_instantiate()
    print(result)
    pass