码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-02-21 09:45:20      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:iii   cccccc   ==   war   __init__   veh   name   十进制   color   

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

 

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

标签:iii   cccccc   ==   war   __init__   veh   name   十进制   color   

原文地址:https://www.cnblogs.com/SBJBA/p/12339876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!