资源简介
形式语言与自动机,图灵机的C++代码实现,用作转换器,有计算功能,X的y次幂
代码片段和文件信息
#include
using namespace std;
// 图灵机节点,用于构造一个双向链表
struct turing_node
{
turing_node *pro *next;
char data;
};
int main()
{
int x = 0 y = 0;
cout << “输入x和y,计算x的y次幂:“;
cin >> x >> y;
// 开始创建链表,得到y0x010的初始带
turing_node *p *tail *s; // 采用尾插法,tail为尾,s用于创建新节点
p = new turing_node; // p指向链表开头
p->data = ‘1‘;
p->pro = NULL;
p->next = NULL;
tail = p;
s = NULL;
int i = 0;
while (i < y - 1)
{
s = new turing_node;
s->data = ‘1‘;
s->pro = tail;
s->next = NULL;
tail->next = s;
tail = s;
i++;
}
s = new turing_node;
s->data = ‘0‘;
s->pro = tail;
s->next = NULL;
tail->next = s;
tail = s;
i = 0;
while (i < x)
{
s = new turing_node;
s->data = ‘1‘;
s->pro = tail;
s->next = NULL;
tail->next = s;
tail = s;
i++;
}
s = new turing_node;
s->data = ‘0‘;
s->pro = tail;
s->next = NULL;
tail->next = s;
tail = s;
s = new turing_node;
s->data = ‘1‘;
s->pro = tail;
s->next = NULL;
tail->next = s;
tail = s;
s = new turing_node;
s->data = ‘0‘;
s->pro = tail;
s->next = NULL;
tail->next = s;
tail = s;
// 链表创建完毕
// 计算开始
turing_node *read_write = p; // 读写头
int state = 0; // 状态标志
while (state != 17) //17设为终点状态
{
switch (state)
{
case 0:
if (read_write->data == ‘0‘)
{
state = 17;
read_write = read_write->next;
}
else if (read_write->data == ‘1‘)
{
state = 1;
read_write->data = ‘a‘;
read_write = read_write->next;
}
else
{
state = 17;
}
break;
case 1:
if (read_write->data == ‘0‘)
{
state = 2;
read_write = read_write->next;
}
else if (read_write->data == ‘1‘)
{
read_write = read_write->next;
}
else
{
state = 17;
}
break;
case 2:
if (read_write->data == ‘0‘)
{
state = 10;
read_write = read_write->pro;
}
else if (read_write->data == ‘1‘)
{
state = 3;
read_write->data = ‘b‘;
read_write = read_write->next;
}
else
{
state = 17;
}
break;
case 3:
if (read_write->data == ‘0‘)
{
state = 4;
read_write = read_write->next;
}
else if (read_write->data == ‘1‘)
{
read_write = read_write->next;
}
else
{
state = 17;
}
break;
case 4:
if (read_write->data == ‘0‘)
{
state = 9;
read_write = read_write->pro;
}
else if (read_write->data == ‘1‘)
{
state = 5;
read_write->data = ‘c‘;
read_write = read_write->next;
}
else
{
state = 17;
}
break;
case 5:
if (read_write->data == ‘0‘)
{
state = 6;
read_write = read_write->next;
}
else if (read_write->data == ‘1‘)
{
read_write = read_write->next;
}
else
{
state = 17;
}
break;
case 6:
if (read_write == NULL)
{
s = new turing_node;
s->data = ‘1‘;
s->pro = tail;
s->ne
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6630 2018-10-25 16:30 turing_x_exp_y.cpp
文件 61275 2018-11-04 19:30 图灵机.docx
- 上一篇:C++实现的高斯投影正算和反算
- 下一篇:单片机C语言实现流水灯,跑马灯仿真程序
评论
共有 条评论