二、八、十、十六进制数之间相互转换。 ● 输入输出 1. 输出操作菜单: 一级菜单: (1)2进制数转换 (2)8进制数转换 (3)10进制数转换 (4)16进制数转换 (0)退出 二级菜单:(只是以2进制转其他进制为例,其他进制相同) (1)2进制数 ---> 8进制数 (2)2进制数 ---> 10进制数 (3)2进制数 ---> 16进制数 (0)返回上级目录
时间: 2024-01-03 22:03:06 浏览: 81
二进制,八进制,十进制,十六进制相互转换
好的,这是一个十分简单的数制转换程序。以下是代码实现,你可以参考一下:
```python
def binary_to_octal(binary_num):
decimal_num = int(binary_num, 2)
octal_num = oct(decimal_num)
return octal_num
def binary_to_decimal(binary_num):
decimal_num = int(binary_num, 2)
return decimal_num
def binary_to_hexadecimal(binary_num):
decimal_num = int(binary_num, 2)
hexadecimal_num = hex(decimal_num)
return hexadecimal_num
def decimal_to_binary(decimal_num):
binary_num = bin(decimal_num)
return binary_num
def decimal_to_octal(decimal_num):
octal_num = oct(decimal_num)
return octal_num
def decimal_to_hexadecimal(decimal_num):
hexadecimal_num = hex(decimal_num)
return hexadecimal_num
def octal_to_binary(octal_num):
decimal_num = int(octal_num, 8)
binary_num = bin(decimal_num)
return binary_num
def octal_to_decimal(octal_num):
decimal_num = int(octal_num, 8)
return decimal_num
def octal_to_hexadecimal(octal_num):
decimal_num = int(octal_num, 8)
hexadecimal_num = hex(decimal_num)
return hexadecimal_num
def hexadecimal_to_binary(hexadecimal_num):
decimal_num = int(hexadecimal_num, 16)
binary_num = bin(decimal_num)
return binary_num
def hexadecimal_to_decimal(hexadecimal_num):
decimal_num = int(hexadecimal_num, 16)
return decimal_num
def hexadecimal_to_octal(hexadecimal_num):
decimal_num = int(hexadecimal_num, 16)
octal_num = oct(decimal_num)
return octal_num
def main_menu():
print("操作菜单:")
print("一级菜单:")
print("(1)2进制数转换")
print("(2)8进制数转换")
print("(3)10进制数转换")
print("(4)16进制数转换")
print("(0)退出")
choice = input("请输入您的选择:")
return choice
def binary_submenu():
print("二级菜单:")
print("(1)2进制数 ---> 8进制数")
print("(2)2进制数 ---> 10进制数")
print("(3)2进制数 ---> 16进制数")
print("(0)返回上级目录")
choice = input("请输入您的选择:")
return choice
def octal_submenu():
print("二级菜单:")
print("(1)8进制数 ---> 2进制数")
print("(2)8进制数 ---> 10进制数")
print("(3)8进制数 ---> 16进制数")
print("(0)返回上级目录")
choice = input("请输入您的选择:")
return choice
def decimal_submenu():
print("二级菜单:")
print("(1)10进制数 ---> 2进制数")
print("(2)10进制数 ---> 8进制数")
print("(3)10进制数 ---> 16进制数")
print("(0)返回上级目录")
choice = input("请输入您的选择:")
return choice
def hexadecimal_submenu():
print("二级菜单:")
print("(1)16进制数 ---> 2进制数")
print("(2)16进制数 ---> 8进制数")
print("(3)16进制数 ---> 10进制数")
print("(0)返回上级目录")
choice = input("请输入您的选择:")
return choice
def main():
while True:
choice = main_menu()
if choice == '1':
while True:
sub_choice = binary_submenu()
if sub_choice == '1':
binary_num = input("请输入2进制数:")
octal_num = binary_to_octal(binary_num)
print("转换结果为:", octal_num)
elif sub_choice == '2':
binary_num = input("请输入2进制数:")
decimal_num = binary_to_decimal(binary_num)
print("转换结果为:", decimal_num)
elif sub_choice == '3':
binary_num = input("请输入2进制数:")
hexadecimal_num = binary_to_hexadecimal(binary_num)
print("转换结果为:", hexadecimal_num)
elif sub_choice == '0':
break
else:
print("输入无效,请重新输入!")
elif choice == '2':
while True:
sub_choice = octal_submenu()
if sub_choice == '1':
octal_num = input("请输入8进制数:")
binary_num = octal_to_binary(octal_num)
print("转换结果为:", binary_num)
elif sub_choice == '2':
octal_num = input("请输入8进制数:")
decimal_num = octal_to_decimal(octal_num)
print("转换结果为:", decimal_num)
elif sub_choice == '3':
octal_num = input("请输入8进制数:")
hexadecimal_num = octal_to_hexadecimal(octal_num)
print("转换结果为:", hexadecimal_num)
elif sub_choice == '0':
break
else:
print("输入无效,请重新输入!")
elif choice == '3':
while True:
sub_choice = decimal_submenu()
if sub_choice == '1':
decimal_num = int(input("请输入10进制数:"))
binary_num = decimal_to_binary(decimal_num)
print("转换结果为:", binary_num)
elif sub_choice == '2':
decimal_num = int(input("请输入10进制数:"))
octal_num = decimal_to_octal(decimal_num)
print("转换结果为:", octal_num)
elif sub_choice == '3':
decimal_num = int(input("请输入10进制数:"))
hexadecimal_num = decimal_to_hexadecimal(decimal_num)
print("转换结果为:", hexadecimal_num)
elif sub_choice == '0':
break
else:
print("输入无效,请重新输入!")
elif choice == '4':
while True:
sub_choice = hexadecimal_submenu()
if sub_choice == '1':
hexadecimal_num = input("请输入16进制数:")
binary_num = hexadecimal_to_binary(hexadecimal_num)
print("转换结果为:", binary_num)
elif sub_choice == '2':
hexadecimal_num = input("请输入16进制数:")
octal_num = hexadecimal_to_octal(hexadecimal_num)
print("转换结果为:", octal_num)
elif sub_choice == '3':
hexadecimal_num = input("请输入16进制数:")
decimal_num = hexadecimal_to_decimal(hexadecimal_num)
print("转换结果为:", decimal_num)
elif sub_choice == '0':
break
else:
print("输入无效,请重新输入!")
elif choice == '0':
break
else:
print("输入无效,请重新输入!")
if __name__ == '__main__':
main()
```
这个程序可以实现 2 进制、8 进制、10 进制和 16 进制之间的互相转换。程序会首先输出一级菜单,用户可以根据需要选择要进行的数制转换类型。接下来,程序会输出对应的二级菜单,供用户选择具体的转换方式。用户只需按照程序提示输入相应的数值,即可获得转换结果。
阅读全文