课后作业17讲:进制转换
本帖最后由 MaxFireGun 于 2022-9-2 22:21 编辑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'}
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}
### Number Conversion Program####
def Dec_to_Y(num, M, N):
i = 0
new_num = 0
new_str = ''
while True:
if num < N:
new_num += num * (10 ** i)
new_str = num_dict + new_str
# return (new_num, new_str)
return (new_str)
else:
A = num % N
new_num += A * 10 ** i
new_str = num_dict + new_str
num = num // N
i += 1
def X_to_Dec(num, M, N):
num = str(num)
temp = 0
num_len = len(num)
j = 1
for i in num:
temp += symbol_dict * (M ** (num_len - j))
j += 1
return (temp, M, N)
def X_to_Y(num, M, N):
a = X_to_Dec(num, M, N)
b = Dec_to_Y(*a)
return b
##########################################
###### number input and number check #######
def num_input():
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(','))
a = num, M, N
num_check(*a)
return a
def num_check(num, M, N):
try:
if M == 2:
for i in str(num):
if int(i) >= 2:
raise ValueError
else:
pass
elif M == 8:
for i in str(num):
if int(i) >= 8:
raise ValueError
else:
pass
else:
pass
except ValueError:
print('The number is not in right format')
num_input()
##############################################
a = num_input()
result = X_to_Y(*a)
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! 本帖最后由 jackz007 于 2022-9-3 00:05 编辑
楼主的代码无法输入10以上进制数
任意进制(2 ~ 16)数互相转换
def foo():
d , f = + , True
while f:
num , M , N = input('Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : ') . split(',')
M , N = int(M) , int(N)
if M > 1 and M < 17 and N > 1 and N < 17:
x = 0
for c in num:
if c . lower() in d[: M]:
x = x * M + d[: M] . index(c . lower())
else:
print('*** error : The number is not in right format\n')
break
else:
f = False
else:
print('*** error : initial number type or final number type wrong !\n')
s = ''
while x:
s = d + s
x //= N
return s
print(foo())
运行实况:
D:\\Python>python x.py
Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 100,16,10
256
D:\\Python>python x.py
Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 7ffFFFffFFff,16,10
140737488355327
D:\\Python>python x.py
Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 999,7,10
*** error : The number is not in right format
Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 777,7,10
*** error : The number is not in right format
Please input *ur number*, *initial number type* and *final number type*, and the data should be sperated by comma : 666,7,10
342
D:\\Python> jackz007 发表于 2022-9-2 23:44
楼主的代码无法输入10以上进制数
任意进制(2 ~ 16)数互相转换
非常感谢!!我才发现这个问题!
我来学习一下你的代码!
jackz007 发表于 2022-9-2 23:44
楼主的代码无法输入10以上进制数
任意进制(2 ~ 16)数互相转换
又学习了一遍你的代码,优雅,太优雅!{:10_288:}{:10_288:}
页:
[1]