Python - 作业 - 任意进制转换
我正在尝试制作一个程序,可以把任何进制的数字转换成用户选择的另一种进制。到目前为止,我的代码是这样的:
innitvar = float(raw_input("Please enter a number: "))
basevar = int(raw_input("Please enter the base that your number is in: "))
convertvar = int(raw_input("Please enter the base that you would like to convert to: "))
这些是我从用户那里得到的数据:初始数字、它的初始进制,以及用户想转换成的进制。根据我的理解,我需要先把数字转换成10进制,然后再转换成用户指定的目标进制。
现在我遇到了一个难题:我需要把初始数字最左边的数字乘以它的初始进制,然后再加上右边的下一个数字,接着重复这个过程,直到到达最右边的数字。我知道在纸上怎么做,但我不知道怎么把这个过程写成Python代码。我不太确定怎么去乘第一个数字,然后加上下一个数字,也不明白程序怎么知道什么时候停止这个操作。
我并不是想让你帮我写程序,但我希望能得到一些正确的方向。
谢谢你的时间!
9 个回答
首先,慢慢来,想想你真正需要的东西。你可能并不需要你想象中的那些东西。
从头开始:用户输入一个数字,输入一个进制。这两个都是字符串。比如说,进制是12,数字是1AB3。那么在12的3次方的位置上有一个'1',在12的2次方的位置上有一个'A',在12的1次方的位置上有一个'B',在12的0次方(也就是个位)的位置上有一个'3'。如果你想把这个数字转换成十进制,你需要把一些数字加起来。
具体来说,你需要计算 1*12^3 + 10*12^2 + 11*12^1 + 3*12^0。注意这里的数字:3、2、1、0。这些数字正好对应输入字符串1AB3的长度。所以,使用一个for
循环会很有帮助。用户输入的不是整数,而是字符串。所以你需要从字符串中提取字符,而不是直接用数字。
那么,如何知道'A'和'C'在十进制中代表什么呢?可以看看Noctis Skytower的回答!
所以你首先要做的就是弄清楚如何遍历一个字符串。第二步是弄清楚如何使用字符串中的每个字符值去查找Noctis Skytower回答中的字典,第三步是写一个循环,利用这些信息。
我需要把最左边的数字乘以它的初始基数,然后再加上右边的下一个数字,接着重复这个过程,直到到达最右边的数字。
所以你需要把数字拆分成一个个数字,放到一个列表里。
提示1:可以用 divmod()
函数把一个数字拆分成数字。用10去除这个数字,就能得到十进制的每一位数字。
提示2:当 n 大于0时,你可以用 divmod()
来得到一个商和一个余数。把余数保存到列表里,然后用商作为新的 n 的值,这样你的数字会越来越小,直到剩下的就是零,这样就完成了。
提示3:你得到的数字是从右到左的顺序。如果这个顺序让你觉得麻烦,可以用 reverse
来反转列表的顺序。或者在创建列表时用 insert(0,digit)
来插入数字。
现在你已经有了数字,放在一个列表里。你可以遍历这个列表。
试试用 for
语句来处理这个列表。
你可能需要用一个“乘法加法”的循环。循环体通常看起来像这样:total = total * new_base + next_digit
。
这应该是你问题的前半部分答案。你能弄明白怎么转换成其他进制吗?
# Create a symbol-to-value table.
SY2VA = {'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,
'G': 16,
'H': 17,
'I': 18,
'J': 19,
'K': 20,
'L': 21,
'M': 22,
'N': 23,
'O': 24,
'P': 25,
'Q': 26,
'R': 27,
'S': 28,
'T': 29,
'U': 30,
'V': 31,
'W': 32,
'X': 33,
'Y': 34,
'Z': 35,
'a': 36,
'b': 37,
'c': 38,
'd': 39,
'e': 40,
'f': 41,
'g': 42,
'h': 43,
'i': 44,
'j': 45,
'k': 46,
'l': 47,
'm': 48,
'n': 49,
'o': 50,
'p': 51,
'q': 52,
'r': 53,
's': 54,
't': 55,
'u': 56,
'v': 57,
'w': 58,
'x': 59,
'y': 60,
'z': 61,
'!': 62,
'"': 63,
'#': 64,
'$': 65,
'%': 66,
'&': 67,
"'": 68,
'(': 69,
')': 70,
'*': 71,
'+': 72,
',': 73,
'-': 74,
'.': 75,
'/': 76,
':': 77,
';': 78,
'<': 79,
'=': 80,
'>': 81,
'?': 82,
'@': 83,
'[': 84,
'\\': 85,
']': 86,
'^': 87,
'_': 88,
'`': 89,
'{': 90,
'|': 91,
'}': 92,
'~': 93}
# Take a string and base to convert to.
# Allocate space to store your number.
# For each character in your string:
# Ensure character is in your table.
# Find the value of your character.
# Ensure value is within your base.
# Self-multiply your number with the base.
# Self-add your number with the digit's value.
# Return the number.
def str2int(string, base):
integer = 0
for character in string:
assert character in SY2VA, 'Found unknown character!'
value = SY2VA[character]
assert value < base, 'Found digit outside base!'
integer *= base
integer += value
return integer
这里是解决方案的后半部分。通过使用这两个函数,转换进制就变得非常简单了。
# Create a value-to-symbol table.
VA2SY = dict(map(reversed, SY2VA.items()))
# Take a integer and base to convert to.
# Create an array to store the digits in.
# While the integer is not zero:
# Divide the integer by the base to:
# (1) Find the "last" digit in your number (value).
# (2) Store remaining number not "chopped" (integer).
# Save the digit in your storage array.
# Return your joined digits after putting them in the right order.
def int2str(integer, base):
array = []
while integer:
integer, value = divmod(integer, base)
array.append(VA2SY[value])
return ''.join(reversed(array))
把所有内容结合起来后,你应该能得到下面的程序。请花点时间去理解它!
innitvar = raw_input("Please enter a number: ")
basevar = int(raw_input("Please enter the base that your number is in: "))
convertvar = int(raw_input("Please enter the base that you would like to convert to: "))
# Create a symbol-to-value table.
SY2VA = {'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,
'G': 16,
'H': 17,
'I': 18,
'J': 19,
'K': 20,
'L': 21,
'M': 22,
'N': 23,
'O': 24,
'P': 25,
'Q': 26,
'R': 27,
'S': 28,
'T': 29,
'U': 30,
'V': 31,
'W': 32,
'X': 33,
'Y': 34,
'Z': 35,
'a': 36,
'b': 37,
'c': 38,
'd': 39,
'e': 40,
'f': 41,
'g': 42,
'h': 43,
'i': 44,
'j': 45,
'k': 46,
'l': 47,
'm': 48,
'n': 49,
'o': 50,
'p': 51,
'q': 52,
'r': 53,
's': 54,
't': 55,
'u': 56,
'v': 57,
'w': 58,
'x': 59,
'y': 60,
'z': 61,
'!': 62,
'"': 63,
'#': 64,
'$': 65,
'%': 66,
'&': 67,
"'": 68,
'(': 69,
')': 70,
'*': 71,
'+': 72,
',': 73,
'-': 74,
'.': 75,
'/': 76,
':': 77,
';': 78,
'<': 79,
'=': 80,
'>': 81,
'?': 82,
'@': 83,
'[': 84,
'\\': 85,
']': 86,
'^': 87,
'_': 88,
'`': 89,
'{': 90,
'|': 91,
'}': 92,
'~': 93}
# Take a string and base to convert to.
# Allocate space to store your number.
# For each character in your string:
# Ensure character is in your table.
# Find the value of your character.
# Ensure value is within your base.
# Self-multiply your number with the base.
# Self-add your number with the digit's value.
# Return the number.
integer = 0
for character in innitvar:
assert character in SY2VA, 'Found unknown character!'
value = SY2VA[character]
assert value < basevar, 'Found digit outside base!'
integer *= basevar
integer += value
# Create a value-to-symbol table.
VA2SY = dict(map(reversed, SY2VA.items()))
# Take a integer and base to convert to.
# Create an array to store the digits in.
# While the integer is not zero:
# Divide the integer by the base to:
# (1) Find the "last" digit in your number (value).
# (2) Store remaining number not "chopped" (integer).
# Save the digit in your storage array.
# Return your joined digits after putting them in the right order.
array = []
while integer:
integer, value = divmod(integer, convertvar)
array.append(VA2SY[value])
answer = ''.join(reversed(array))
# Display the results of the calculations.
print answer