鱼C论坛

 找回密码
 立即注册
查看: 2377|回复: 3

[作品展示] 课后作业17讲:进制转换

[复制链接]
发表于 2022-9-2 22:19:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 MaxFireGun 于 2022-9-2 22:21 编辑
  1. num_dict = {0:'0', 1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'}

  2. symbol_dict = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15}

  3. ### Number Conversion Program####

  4. def Dec_to_Y(num, M, N):
  5.     i = 0
  6.     new_num = 0
  7.     new_str = ''
  8.    
  9.     while True:
  10.         if num < N:
  11.             new_num += num * (10 ** i)
  12.             new_str = num_dict[num] + new_str
  13.             # return (new_num, new_str)
  14.             return (new_str)
  15.         
  16.         else:
  17.             A = num % N
  18.             new_num += A * 10 ** i
  19.             new_str = num_dict[A] + new_str         
  20.             num = num // N
  21.             i += 1

  22. def X_to_Dec(num, M, N):
  23.     num = str(num)
  24.     temp = 0
  25.     num_len = len(num)
  26.     j = 1
  27.     for i in num:
  28.         temp += symbol_dict[i] * (M ** (num_len - j))
  29.         j += 1
  30.     return (temp, M, N)
  31.    

  32. def X_to_Y(num, M, N):
  33.     a = X_to_Dec(num, M, N)
  34.     b = Dec_to_Y(*a)   
  35.     return b        
  36.         
  37. ##########################################

  38. ###### number input and number check #######

  39. def num_input():
  40.     num, M, N = map(lambda x: int(x), input('Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma:').split(','))
  41.     a = num, M, N
  42.     num_check(*a)
  43.     return a

  44. def num_check(num, M, N):
  45.     try:
  46.         if M == 2:
  47.             for i in str(num):
  48.                 if int(i) >= 2:
  49.                     raise ValueError
  50.                 else:
  51.                     pass
  52.         elif M == 8:
  53.             for i in str(num):
  54.                 if int(i) >= 8:
  55.                     raise ValueError
  56.                 else:
  57.                     pass
  58.         else:
  59.             pass
  60.                
  61.     except ValueError:
  62.         print('The number is not in right format')
  63.         num_input()
  64.         
  65. ##############################################

  66. a = num_input()
  67. result = X_to_Y(*a)
  68. print('The final number is:' + result)
复制代码



*代码说明:
1.程序可以实现数字在2,8,10,16进制之间的任意转换。
2.原理说明:所有初始进制都转换成10进制,最后转换到目标进制。
3.内容说明:
        a. 函数num_input为输入函数,
        b. 函数num_check为检查输入的number的格式是否正确,比如二进制里输入2001或者8进制里输入9001就会提示错误,并要求重新输入。
        c. 先利用X_to_Dec函数,将任意进制的数字转换为十进制。
        d.然后利用Dec_to_Y函数,将任意进制的数字转换成目标进制。
4.X_to_Y将X_to_Dec和Dec_to_Y打包在一起。

Beer!Cheers!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-2 23:44:22 | 显示全部楼层
本帖最后由 jackz007 于 2022-9-3 00:05 编辑

      楼主的代码无法输入10以上进制数
      任意进制(2 ~ 16)数互相转换
  1. def foo():
  2.     d , f = [chr(0x30 + i) for i in range(10)] + [chr(0x61 + i) for i in range(6)] , True
  3.     while f:
  4.         num , M , N = input('Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : ') . split(',')
  5.         M , N = int(M) , int(N)
  6.         if M > 1 and M < 17 and N > 1 and N < 17:
  7.             x = 0
  8.             for c in num:
  9.                 if c . lower() in d[: M]:
  10.                     x = x * M + d[: M] . index(c . lower())
  11.                 else:
  12.                     print('*** error : The number is not in right format\n')
  13.                     break
  14.             else:
  15.                 f = False
  16.         else:
  17.             print('*** error : initial number type or final number type wrong !\n')
  18.     s = ''
  19.     while x:
  20.         s = d[x % N] + s
  21.         x //= N
  22.     return s

  23. print(foo())
复制代码

        运行实况:
  1. D:\[00.Exerciese.2022]\Python>python x.py
  2. Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 100,16,10
  3. 256

  4. D:\[00.Exerciese.2022]\Python>python x.py
  5. Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 7ffFFFffFFff,16,10
  6. 140737488355327

  7. D:\[00.Exerciese.2022]\Python>python x.py
  8. Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 999,7,10
  9. *** error : The number is not in right format

  10. Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 777,7,10
  11. *** error : The number is not in right format

  12. Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 666,7,10
  13. 342

  14. D:\[00.Exerciese.2022]\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-3 00:41:45 From FishC Mobile | 显示全部楼层
jackz007 发表于 2022-9-2 23:44
楼主的代码无法输入10以上进制数
      任意进制(2 ~ 16)数互相转换


非常感谢!!我才发现这个问题!
我来学习一下你的代码!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-9 21:28:22 | 显示全部楼层
jackz007 发表于 2022-9-2 23:44
楼主的代码无法输入10以上进制数
      任意进制(2 ~ 16)数互相转换

又学习了一遍你的代码,优雅,太优雅!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-14 18:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表