模拟图灵机(XN×2)



前言

按照XN×2图灵机的规则进行算法设计,流程按照自顶向下一步走完的。


一、算法分析

(1)规则转换。
接收一个十进制数的输入,转换为二进制数在末尾添加一个‘,’号,按照0->0、1->01、,->110的规则进行数据转换。

(2)指令计算。
按照XN×2图灵机:0 0->0 0 R、0 1->1 0 R、1 0->0 1 R、1 1->11 0 R、10 0->11 1 R、11 0->0 1 STOP的指令规则在经过转换的数据上进行移动,输出已经转换成功的数据。

(3)结果输出。
按照第一步的规则对数据进行逆转换为二进制数,再转换为十进制数进行返回输出。

二、概要设计

结构设计

Coding类,包含4个成员变量,1个字典用于定义规则、2个列表用于存储二进制数据和规则转换之后的数据、1个字符串变量返回给下一层函数处理;4个成员函数,Coding()用于数据的初始化、hex2()将数据转换为二进制并在末尾添加‘,’、encoding()按照规则对数据进行转换、cleardata()重定义数据。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
 * @Classname Coding
 * @Description 将十进制数转化为编码后的数据
 * @Date 2021/4/2 11:26
 * @Created 30500
 */
public class Coding {
    private Map<String, String> rule;
    private List<String> coding;
    private List<String> hexNumber;
    private String strCode;
    /*
     * @Description //初始化内部成员 并对编码规则进行确定
     * @author 30500
     * @date 2021/4/2 11:40
     * @param []
     * @return
     */
    public Coding() {
        this.coding = new ArrayList<String>();
        this.hexNumber = new ArrayList<String>();
        this.strCode = "";
        this.rule = new HashMap<>();
        this.rule.put("0", "0");
        this.rule.put("1", "10");
        this.rule.put(",", "110");
    }
    /*
     * @Description //将十进制数转换为二进制数并在末尾添加’,‘
     * @author 30500
     * @date 2021/4/2 11:59
     * @param [str]
     * @return java.util.List<java.lang.String>
     */
    public List<String> hex2(Integer str){
        String temp = Integer.toBinaryString(str);
        for (int i = 0; i < temp.length(); i++) {
            this.hexNumber.add(String.valueOf(temp.charAt(i)));
        }
        this.hexNumber.add(",");
        return this.hexNumber;
    }
    /*
     * @Description //将数据进行编码转换
     * @author 30500
     * @date 2021/4/2 12:35
     * @param []
     * @return java.lang.String
     */
    public String encoding(){
        for (String str : this.hexNumber){
            this.coding.add(this.rule.get(str));
            this.strCode +=this.rule.get(str);
        }
        return this.strCode;
    }
    public void cleardata(){
        this.strCode = "";
        this.hexNumber.clear();
        this.coding.clear();
    }
}
TuringMachine类,包含6个成员变量,分别表示进行运算的数据、转换之后的二进制数据、图灵机的状态值、图灵机的输入值、索引的偏差、是否结束的标志;4个成员函数,TURingMachine()用于初始化图灵机、setCode()用于测试时对图灵机内部变量进行重新复制、getTURingMachine()用于进行指令计算、conversionResult()用于将计算后的指令再转换为十进制数据。
/**
 * @Classname TuringMachine
 * @Description 进行XN*2运算
 * @Date 2021/4/2 12:22
 * @Created 30500
 */
public class TuringMachine {
    private StringBuilder code;
    private String BinaryCode;
    private String state;
    private String input;
    private Integer index;
    private boolean flag;
    /*
     * @Description //构造函数
     * @author 30500
     * @date 2021/4/2 20:43
     * @param [code, state, input, output]
     * @return
     */
    public TuringMachine(String code){
        this.code = new StringBuilder(code);
        this.BinaryCode = "";
        this.state = "0";
        this.input = "";
        this.index = 0;
        this.flag = true;
    }
    public void setCode(String code){
        this.code = new StringBuilder(code);
        this.BinaryCode = "";
        this.state = "0";
        this.input = "";
        this.index = 0;
        this.flag = true;
    }
    /*
     * @Description //进行XN*2预算 并返回运算结果
     * @author 30500
     * @date 2021/4/2 21:12
     * @param []
     * @return java.lang.StringBuilder
     */
    public StringBuilder getTuringMachine(){
        while (this.flag){
            this.input = String.valueOf(this.code.charAt(this.index));
            if(this.state.equals("0") && this.input.equals("0")){
                this.state = "0";
                this.code.setCharAt(this.index, '0');
                this.index++;
            }
            else if(this.state.equals("0") && this.input.equals("1")){
                this.state = "1";
                this.code.setCharAt(this.index, '0');
                this.index++;
            }
            else if(this.state.equals("1") && this.input.equals("0")){
                this.state = "0";
                this.code.setCharAt(this.index, '1');
                this.index++;
            }
            else if(this.state.equals("1") && this.input.equals("1")){
                this.state = "10";
                this.code.setCharAt(this.index, '0');
                this.index++;
            }
            else if(this.state.equals("10") && this.input.equals("0")){
                this.state = "11";
                this.code.setCharAt(this.index, '1');
                this.index++;
            }
            else if(this.state.equals("11") && this.input.equals("0")){
                this.state = "0";
                this.code.setCharAt(this.index, '1');
                this.index++;
                this.flag = false;
            }
            if(this.index >= this.code.length()){
                this.code.append("0");
            }
        }
        return this.code;
    }
    /*
     * @Description //将结果转换为十进制输出
     * @author 30500
     * @date 2021/4/3 14:13
     * @param []
     * @return java.lang.Integer
     */
    public Integer conversionResult(){
        this.index = 0;
        while (!this.flag){
            if(this.code.charAt(this.index) == '0'){
                this.BinaryCode += "0";
                this.index++;
            }
            else if(this.code.charAt(this.index) == '1' && this.code.charAt(this.index + 1) == '0'){
                this.BinaryCode += "1";
                this.index += 2;
            }
            else if(this.code.charAt(this.index) == '1' && this.code.charAt(this.index + 1) == '1') {
                this.flag = true;
            }
        }
        Integer num = Integer.parseInt(this.BinaryCode, 2);
        return num;
    }
}
Test类,只有一个静态函数入口,用于整个算法的功能测试,并将测试结果以txt文件的格式进行保存输出。
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname Test
 * @Description 测试各个模块函数的正确性
 * @Date 2021/4/3 14:18
 * @Created 30500
 */
public class Test {
    public static void main(String[] args) {
        Coding demo = new Coding();                                                                 //创建编码对象
        TuringMachine turingMachine = new TuringMachine("");                                  //创建图灵机
        List<String> packet = new ArrayList<>();                                                    //存储100次的返回结果
        String temp;                                                                                //临时变量接收模块的返回值
        for (int i = 0; i < 100; i++) {                                                             //循环测试
            temp = "";
            demo.hex2(i);
            String original = demo.encoding();
            turingMachine.setCode(original);
            String change = turingMachine.getTuringMachine().toString();
            Integer result = turingMachine.conversionResult();
            temp = i + "----->" + original + "----->" + change + "----->" + result;                 //--->表示数据间的递进关系
            packet.add(temp);
            demo.cleardata();
        }
        try{
            BufferedWriter br = new BufferedWriter(new FileWriter("src/result.txt"));       //数据保存在本地
            for(int i = 0;i< packet.size();i++){
                br.write(packet.get(i)+"\n");
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

App类,主函数的入口类,整个程序由此开始进入。
/**
 * @Classname App
 * @Description 程序入口类
 * @Date 2021/4/2 12:07
 * @Created 30500
 */
public class App {
    public static void main(String[] args) {
        Coding demo = new Coding();
        demo.hex2(1);
        String str = demo.encoding();
        TuringMachine turingMachine = new TuringMachine(str);
        turingMachine.getTuringMachine();
        Integer result = turingMachine.conversionResult();
        System.out.println(result);
    }
}

三、测试

分别对算法的三大功能模块进行测试,测试用例为0-99总计一百个正整数,将每个模块的记录值都返回并存储在本地的result.txt文件中,由一个for循环进行控制; 结果的验证则是由人工随机挑选数据进行计算后进行对比,还有一个重要指标就是最终的返回结果是否是输入数据的两倍计算量。
0----->0110----->000110----->0
1----->10110----->0100110----->2
2----->100110----->01000110----->4
3----->1010110----->010100110----->6
4----->1000110----->010000110----->8
5----->10010110----->0100100110----->10
6----->10100110----->0101000110----->12
7----->101010110----->01010100110----->14
8----->10000110----->0100000110----->16
9----->100010110----->01000100110----->18
10----->100100110----->01001000110----->20
11----->1001010110----->010010100110----->22
12----->101000110----->01010000110----->24
13----->1010010110----->010100100110----->26
14----->1010100110----->010101000110----->28
15----->10101010110----->0101010100110----->30
16----->100000110----->01000000110----->32
17----->1000010110----->010000100110----->34
18----->1000100110----->010001000110----->36
19----->10001010110----->0100010100110----->38
20----->1001000110----->010010000110----->40
21----->10010010110----->0100100100110----->42
22----->10010100110----->0100101000110----->44
23----->100101010110----->01001010100110----->46
24----->1010000110----->010100000110----->48
25----->10100010110----->0101000100110----->50
26----->10100100110----->0101001000110----->52
27----->101001010110----->01010010100110----->54
28----->10101000110----->0101010000110----->56
29----->101010010110----->01010100100110----->58
30----->101010100110----->01010101000110----->60

总结

对算法设计时更一步的加深了对图灵机XN×2的指令运转的理解,熟悉其中的每一个计算步骤,了解每个数据的含义;在进行代码调试时花费较多的时间进行流程追踪,确保了程序按照预期的算法流程进行动作。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值