2 篇文章 0 订阅

步骤

算法分析

1，函数turing_conversion()将输入的数据转化为二进制扩展码
2，函数turing_operate()实现图灵机（XN*2）的操作
3，函数turing_result对操作完的数 实现转化并输出

相关代码

# @File: turing.py
# @Data: 2021/4/11
# @Author: CheYuHang
# @Software: Pycharm

class turing:

def turing_conversion(number):
"""
对输入的数据转化成二进制的扩展码
:return:
"""
x = number
b = bin(x).replace('0b', '')
# 二进制转化
print("转化为二进制为：" + b)

# 将二进制结果转为列表
c = list(map(int, b))

# 判断输入值0时的情况
if c[0] == '0':
return 0
else:
n_c = [0, 1, 0]
for a in c[1:]:
if a == 1:
n_c.append(10)
else:
if a == 0:
n_c.append(0)
n_c.append(110)

return n_c

def turing_operate(new_tensor):
"""
图灵机的操作
:param new_tensor:
:return:
"""
tend = [str(i) for i in new_tensor]
tend.append("0")
tend.append('0')

a = ''.join(tend)
eve_tend = list(a)

# 初始化内态
state = '0'
arr = []
for i in range(0, len(eve_tend)):
print("第" + str(i + 1) + "步")
if state == '0 ' and eve_tend[i] == '0':
state = '0'
eve_tend[i] = '0'
elif state == '0' and eve_tend[i] == '1':
state = '1'
eve_tend[i] = '0'
elif state == '1' and eve_tend[i] == '0':

state = '0'
eve_tend[i] = '1'
elif state == '1' and eve_tend[i] == '1':

state = '10'
eve_tend[i] = '0'
elif state == '10' and eve_tend[i] == '0':

state = '11'
eve_tend[i] = '1'
elif state == '11' and eve_tend[i] == '0':

state = '0'
eve_tend[i] = '1'

arr.append(eve_tend)
carry = [str(i) for i in eve_tend]
temp = ''.join(carry)

# 每一步进行输出
print("当前内态: " + state + "    " + "当前扩展码: " + temp)
print("stop")
return arr

def turing_result(arr):
"""
结果实现
:param arr:
:return:
"""
a1 = arr[0]
t = [int(i) for i in a1]
result = []
for e in range(0, len(t)):
if t[e] == 1 and t[e + 1] == 0:
result.append(1)

elif (e + 1) < len(t) and t[e] == 0 and t[e + 1] == 0:
result.append(0)

elif t[e] == 1 and t[e + 1] == 1 and t[e + 2] == 0:
result.append(',')
break
result.remove(',')
ls = [str(i) for i in result]
ls1 = ''.join(ls)
print('转化完成的二进制码为：' + ls1)
Final1 = int(ls1, 2)
print("XN*2:", end=" ")
print(Final1)

# 调用实现
x = int(input("请输入要操作的一个整数："))
a = turing.turing_conversion(x)
b = turing.turing_operate(a)
turing.turing_result(b)


测试及调试

• 0
点赞
• 1
收藏
• 打赏
• 3
评论
09-12
03-23 3994
03-31 218
04-03 760
04-18 542
03-20 2093
03-22 1967
03-22 421
03-22 928
03-23 694
12-02 6334
12-04 7204
03-20 1090
03-24

“相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。