使用Java模拟XN*2图灵机

本文档详细介绍了如何使用Java编程模拟XN*2图灵机的过程,包括将十进制数转换为收缩扩展二进制编码,然后通过图灵机操作语句进行计算,最后展示每一步的结果。程序设计包括输入验证、二进制转换、图灵机状态转换和循环操作等关键步骤,实现了从输入到输出的完整计算流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Java模拟XN*2图灵机

目录
使用Python求解三天打鱼两天晒网问题。
使用Java模拟XN*2图灵机
问题
对于XN+1或XN*2图灵机进行模拟,任意给定的十进制数a,转换为收缩扩展二进制的编码,再编程模拟此Turing机的运行过程,要求输出从开始运行起的每一步骤的结果。 用C或C++或java或python语言实现程序解决问题。
要求
基本要求:
1.程序风格良好,提供友好的输入输出。
2.输入数据的正确性验证。
作业思路:
根据题意可以将解题过程分为三步:
1)输入数a,并将其转换为二进制编码;
2)编写图灵机操作语句;
3)根据数字长度,循环执行语句;
功能实现
1.准备
题目要求输入,则首先引入scanner库.

import java.util.Scanner;

创建测试类MainFunction与图灵机TuringMachine

MainFunction.java

public class MainFunction {
	public static void main(String[] args) {
	
	}
}
TuringMachine.java

public class TuringMachine {

}

2.输入整数a,将其转换为二进制码

int n = scanner.nextInt();
String s= mFunction.decToBin(n);

其中,String decToBin(int)原型如下:

   /**
	 * <p>Title: decToBin</p>  
	 * <p>Description: translate DEC code to BIN code.</p>  
	 * @param int[DEC]
	 * @return String[BIN string]
	 */
	public String decToBin(int n) {
        String binC = "";
        String binN = Integer.toBinaryString(n); // 十进制数转换为二进制数
        binN += ","; // 用,标识此二进制数到此已完整,后面的0都忽略不计
        System.out.println("二进制表示:" + binN);
        // 利用for循环对二进制数binNum中的字符进行遍历,根据其中的每个字符得出二进制编码binCode
        for (int i = 0; i < binN.length(); i++) {
            	// 0 -> 0
            if (binN.charAt(i) == '0') {
                binC += "0";
                // 1 -> 10
            } else if (binN.charAt(i) == '1') {
                binC += "10";
                // , -> 110
            } else if (binN.charAt(i) == ',') {
                binC += "110";
            }
        }
        binC = "0" + binC + "00";
        System.out.println("二进制编码:" + binC);
        return binC;
    }

3.编写XN*2图灵机操作语句
此处使用switch语句判断内态较为简单
为确保操作位不出错,定义了int类型的指针表示当前操作位,自增表示右移,自减表示左移。
因为采用char[]存储二进制码,故不能使用多位数表示内态,所以使用ABC等进行数字的扩充。

   /**
	 * <p>Title: operate</p>  
	 * <p>Description: to calculate the right BIN code by using equation of XN*2 Turing Machine </p>
	 */
	public void operate() {
		
		switch (this.inwardState[this.pointer]) {
		case '0': {
			if(this.number[this.pointer] == '0') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 0 -> 0, 输入 0 -> 0 R.:");
				this.number[this.pointer] = '0';
				this.inwardState[this.pointer] = '0';
				this.pointer ++;
				break;
			}
			if(this.number[this.pointer] == '1') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 0 -> 1, 输入 1 -> 0 R.:");
				this.number[this.pointer] = '0';
				this.inwardState[this.pointer] = '1';
				this.pointer ++;
				break;
			}
		}
		case '1': {
			if(this.number[this.pointer] == '0') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 1 -> 0, 输入 0 -> 1 R.:");
				this.number[this.pointer] = '1';
				this.inwardState[this.pointer] = '0';
				this.pointer ++;
				break;
			}
			if(this.number[this.pointer] == '1') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 1 -> A, 输入 1 -> 0 R.:");
				this.number[this.pointer] = '0';
				this.inwardState[this.pointer] = 'A';
				this.pointer ++;
				break;
			}
		}
		case 'A': {
			if(this.number[this.pointer] == '0') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 A -> B, 输入 0 -> 1 R.:");
				this.number[this.pointer] = '1';
				this.inwardState[this.pointer] = 'B';
				this.pointer ++;
				break;
			}
		}
		case 'B': {
			if(this.number[this.pointer] == '0') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 B -> 0, 输入 0 -> 1 R.STOP :");
				this.number[this.pointer] = '1';
				this.inwardState[this.pointer] = '0';
				this.pointer ++;
				this.showParam();
				System.exit(0);
			}
		}
		}
	}

4.获取编码长度,进行循环操作

for(int i = 0; i < turingMachine.getLen(); i ++) {
			turingMachine.operate();
			turingMachine.showParam();
		}

5.运行程序
运行结果
完整代码如下
TuringMachine.java

/**<p>Title: TuringMachine.java</p>  
 * <p>Description: </p>  
 * @author:moree
 * @date 2021年4月13日  
 * @version 1.0 
 */ 
package com.boyu.turingmachine;

/**<p>Title: TuringMachine</p>  
 * <p>Description:the class to run as Turing Machine. </p>  
 * @author:moree
 * @date 2021年4月13日  
 */

public class TuringMachine {
	
	private char[] inwardState  = new char[100];
	private char[] number = new char[100];
	private int l;
	private int pointer = 0;
	
	/**
	 * <p>Title:TuringMachine::TuringMachine(char[]) </p>  
	 * <p>Description:to construct the object. </p>  
	 * @param arry
	 */
	public TuringMachine(char[] arry) {
		
		this.l = arry.length;
		for(int i = 0; i < this.l; i ++) {
			this.inwardState[i] = '0';
			this.number[i] = arry[i];
		}
	}
	
	/**
	 * <p>Title: getLen</p>  
	 * <p>Description:get the length of tape. </p>  
	 * @return int 
	 */
	public int getLen() {
		
		return this.l;
	}
	
	/**
	 * <p>Title: showParam</p>  
	 * <p>Description: checking the right input and debugging the project by step in.</p>
	 */
	public void showParam() {
		
		System.out.println("编码:");
		System.out.println(this.number);
		
		System.out.println("内态:");
		System.out.println(this.inwardState);

	}
	
	/**
	 * <p>Title: operate</p>  
	 * <p>Description: to calculate the right BIN code by using equation of XN*2 Turing Machine </p>
	 */
	public void operate() {
		
		switch (this.inwardState[this.pointer]) {
		case '0': {
			if(this.number[this.pointer] == '0') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 0 -> 0, 输入 0 -> 0 R.:");
				this.number[this.pointer] = '0';
				this.inwardState[this.pointer] = '0';
				this.pointer ++;
				break;
			}
			if(this.number[this.pointer] == '1') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 0 -> 1, 输入 1 -> 0 R.:");
				this.number[this.pointer] = '0';
				this.inwardState[this.pointer] = '1';
				this.pointer ++;
				break;
			}
		}
		case '1': {
			if(this.number[this.pointer] == '0') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 1 -> 0, 输入 0 -> 1 R.:");
				this.number[this.pointer] = '1';
				this.inwardState[this.pointer] = '0';
				this.pointer ++;
				break;
			}
			if(this.number[this.pointer] == '1') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 1 -> A, 输入 1 -> 0 R.:");
				this.number[this.pointer] = '0';
				this.inwardState[this.pointer] = 'A';
				this.pointer ++;
				break;
			}
		}
		case 'A': {
			if(this.number[this.pointer] == '0') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 A -> B, 输入 0 -> 1 R.:");
				this.number[this.pointer] = '1';
				this.inwardState[this.pointer] = 'B';
				this.pointer ++;
				break;
			}
		}
		case 'B': {
			if(this.number[this.pointer] == '0') {
				System.out.println("指针位置:" + (this.pointer + 1) + " 进行操作 ==> 内态 B -> 0, 输入 0 -> 1 R.STOP :");
				this.number[this.pointer] = '1';
				this.inwardState[this.pointer] = '0';
				this.pointer ++;
				this.showParam();
				System.exit(0);
			}
		}
		}
	}
}

MainFunction.java

/**<p>Title: MainFunction.java</p>  
 * <p>Description: </p>  
 * <p>Copyright: Copyright (c) 2018</p>  
 * <p>Company: www.baidudu.com</p>  
 * @author:moree
 * @date 2021年4月13日  
 * @version 1.0 
 */ 
package com.boyu.turingmachine;

import java.util.Scanner;

/**<p>Title: MainFunction</p>  
 * <p>Description: </p>  
 * @author:moree
 * @date 2021年4月13日  
 */

public class MainFunction {
	
	
	/**
	 * <p>Title: decToBin</p>  
	 * <p>Description: translate DEC code to BIN code.</p>  
	 * @param int[DEC]
	 * @return String[BIN string]
	 */
	public String decToBin(int n) {
        String binC = "";
        String binN = Integer.toBinaryString(n); // 十进制数转换为二进制数
        binN += ","; // 用,标识此二进制数到此已完整,后面的0都忽略不计
        System.out.println("二进制表示:" + binN);
        // 利用for循环对二进制数binNum中的字符进行遍历,根据其中的每个字符得出二进制编码binCode
        for (int i = 0; i < binN.length(); i++) {
            // 0 -> 0
            if (binN.charAt(i) == '0') {
                binC += "0";
                // 1 -> 10
            } else if (binN.charAt(i) == '1') {
                binC += "10";
                // , -> 110
            } else if (binN.charAt(i) == ',') {
                binC += "110";
            }
        }
        binC = "0" + binC + "00";
        System.out.println("二进制编码:" + binC);
        return binC;
    }

	/**<p>Title: main</p>  
	 * <p>Description: </p>  
	 * @param args  
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MainFunction mFunction = new MainFunction();
		Scanner scanner = new Scanner(System.in);
		System.out.println("XNx2图灵机已构建\n请输入数字:");
		
		int n = scanner.nextInt();
		String s= mFunction.decToBin(n);
		
		TuringMachine turingMachine = new TuringMachine(s.toCharArray());
		
		for(int i = 0; i < turingMachine.getLen(); i ++) {
			turingMachine.operate();
			turingMachine.showParam();
		}
	}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值