日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

如何用Java模擬XN*2圖靈機(jī)

瀏覽:19日期:2022-08-14 15:05:26
題目描述:

對(duì)于XN*2圖靈機(jī)進(jìn)行模擬,任意給定的十進(jìn)制數(shù),轉(zhuǎn)換為收縮擴(kuò)展二進(jìn)制的編碼,再編程模擬此Turing機(jī)的運(yùn)行過程,要求輸出從開始運(yùn)行起的每一步驟的結(jié)果。用C或C++或Java或Python語(yǔ)言實(shí)現(xiàn)程序解決問題。

要求:1. 程序風(fēng)格良好(使用自定義注釋模板);

2. 提供友好的輸入輸出,并進(jìn)行輸入數(shù)據(jù)的正確性驗(yàn)證。

算法分析:

1. 將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制數(shù);

2. 將二進(jìn)制數(shù)轉(zhuǎn)換為收縮擴(kuò)展二進(jìn)制的編碼;

3. 根據(jù)當(dāng)前的內(nèi)態(tài)和輸入執(zhí)行XN*2圖靈機(jī)的指令;

4. 將結(jié)果的二進(jìn)制編碼轉(zhuǎn)換為二進(jìn)制數(shù);

5. 將二進(jìn)制數(shù)轉(zhuǎn)換為十進(jìn)制數(shù),實(shí)現(xiàn)乘2運(yùn)算功能。

概要設(shè)計(jì):

算法流程圖如下:

如何用Java模擬XN*2圖靈機(jī)

測(cè)試:

輸入的十進(jìn)制數(shù)

正確的二進(jìn)制編碼

輸出的二進(jìn)制編碼

正確的運(yùn)算結(jié)果

輸出的運(yùn)算結(jié)果

0

0011000

0011000

0

0

3

0101011000

0101011000

6

6

18

0100010011000

0100010011000

36

36

運(yùn)行結(jié)果:

如何用Java模擬XN*2圖靈機(jī)

如何用Java模擬XN*2圖靈機(jī)

如何用Java模擬XN*2圖靈機(jī)

調(diào)試:

①對(duì)調(diào)用指令的方法進(jìn)行調(diào)試,開始時(shí)binCodeList的size為0,導(dǎo)致執(zhí)行binCodeList.set(i, “0”)時(shí)出現(xiàn)錯(cuò)誤,進(jìn)過調(diào)試后發(fā)現(xiàn)是因?yàn)闆]給方法設(shè)置binCodeList的參數(shù),導(dǎo)致方法中用的是類中空的binCodeList。在方法的參數(shù)中加上List<String> binCodeList就可以解決。

如何用Java模擬XN*2圖靈機(jī)

②對(duì)將二進(jìn)制編碼轉(zhuǎn)換為十進(jìn)制數(shù)的方法進(jìn)行調(diào)試,開始時(shí)運(yùn)算結(jié)果出現(xiàn)錯(cuò)誤,調(diào)試后發(fā)現(xiàn)是判斷第i個(gè)字符為1,第i+1個(gè)字符為0后,沒有將i再加1,導(dǎo)致下次循環(huán)又遍歷到i+1的0,于是有些步驟結(jié)果就會(huì)多出0。在if (binCode.charAt(i + 1) == ’0’){…}代碼塊中加上i++就可以解決。

如何用Java模擬XN*2圖靈機(jī)

源代碼:

import java.util.*; /** * @description: 該類模擬XN*2圖靈機(jī),對(duì)任意給定的十進(jìn)制數(shù),轉(zhuǎn)換為收縮擴(kuò)展二進(jìn)制的編碼,并可輸出運(yùn)行中每一步驟的結(jié)果 */public class TuringMachine { private int internalState; // 圖靈機(jī)的內(nèi)態(tài) private String binCode; // 二進(jìn)制編碼 Function f = new Function(); // 需要用到的方法 List<String> binCodeList = new ArrayList<>(); // 用來存放二進(jìn)制編碼 static int r = 0; // 當(dāng)r為1時(shí)機(jī)器向右移動(dòng)一格 static int s = 0; // 當(dāng)s為1時(shí)機(jī)器停止運(yùn)行 TuringMachine() {internalState = 0;binCode = '0'; } public int getInternalState() {return internalState; } public void setInternalState(int internalState) {this.internalState = internalState; } public String getBinCode() {return binCode; } public void setBinCode(String binCode) {this.binCode = binCode; } /** * @description: 模擬圖靈機(jī)的運(yùn)行過程 * @param: [binCode 二進(jìn)制編碼] * @return: void */ public void runProcess(String binCode) {binCodeList = f.toArrayList(binCode); // 將二進(jìn)制碼binCode轉(zhuǎn)換為ArrayList類型存放在binCodeList中// for循環(huán)對(duì)binCodeList進(jìn)行遍歷,根據(jù)當(dāng)前內(nèi)態(tài)的值判斷該執(zhí)行哪條指令for (int i = 0; i < binCodeList.size(); i++) { r = 1; // 當(dāng)s==1時(shí)機(jī)器停止,跳出循環(huán) if (s == 1) {break; } switch (getInternalState()) {// 內(nèi)態(tài)為0時(shí)執(zhí)行指令1case 0: instruction_1(binCodeList.get(i), i, binCodeList); break;// 內(nèi)態(tài)為1時(shí)執(zhí)行指令2case 1: instruction_2(binCodeList.get(i), i, binCodeList); break;// 內(nèi)態(tài)為10時(shí)執(zhí)行指令3case 10: instruction_3(binCodeList.get(i), i, binCodeList); break;// 內(nèi)態(tài)為11時(shí)執(zhí)行指令4case 11: instruction_4(binCodeList.get(i), i, binCodeList); break;default: break; }}System.out.println('XN*2圖靈機(jī)計(jì)算的最終結(jié)果為:');f.toDecNum(f.toString(binCodeList)); // 將binCodeList轉(zhuǎn)換為String類型的二進(jìn)制編碼binCode,再轉(zhuǎn)換為int類型的十進(jìn)制數(shù)decNum } /** * @description: 根據(jù)指令對(duì)每一步驟結(jié)果進(jìn)行打印 * 指令1: 0 0 -> 0 0 R * 0 1 -> 1 0 R * 指令2: 1 0 -> 0 1 R * 1 1 -> 10 0 R * 指令3: 10 0 -> 11 1 R * 指令4: 11 0 -> 0 1 STOP * @param: [input 輸入, i 循環(huán)的次數(shù)從0開始, binCodeList 存放二進(jìn)制編碼binCode] * @return: void */ private void instruction_1(String input, int i, List<String> binCodeList) {System.out.println('當(dāng)前的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + input);if (input.equals('0')) { System.out.println('執(zhí)行此條指令后的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + binCodeList.get(i) + ',右移'); System.out.println('此步驟的結(jié)果為:'); System.out.println(f.toString(binCodeList));}if (input.equals('1')) { setInternalState(1); binCodeList.set(i, '0'); System.out.println('執(zhí)行此條指令后的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + binCodeList.get(i) + ',右移'); System.out.println('此步驟的結(jié)果為:'); System.out.println(f.toString(binCodeList));}System.out.println(); } private void instruction_2(String input, int i, List<String> binCodeList) {System.out.println('當(dāng)前的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + input);if (input.equals('0')) { setInternalState(0); binCodeList.set(i, '1'); System.out.println('執(zhí)行此條指令后的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + binCodeList.get(i) + ',右移'); System.out.println('此步驟的結(jié)果為:'); System.out.println(f.toString(binCodeList));}if (input.equals('1')) { setInternalState(10); binCodeList.set(i, '0'); System.out.println('執(zhí)行此條指令后的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + binCodeList.get(i) + ',右移'); System.out.println('此步驟的結(jié)果為:'); System.out.println(f.toString(binCodeList));}System.out.println(); } private void instruction_3(String input, int i, List<String> binCodeList) {System.out.println('當(dāng)前的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + input);if (input.equals('0')) { setInternalState(11); binCodeList.set(i, '1'); System.out.println('執(zhí)行此條指令后的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + binCodeList.get(i) + ',右移'); System.out.println('此步驟的結(jié)果為:'); System.out.println(f.toString(binCodeList));}System.out.println(); } private void instruction_4(String input, int i, List<String> binCodeList) {System.out.println('當(dāng)前的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + input);if (input.equals('0')) { setInternalState(0); binCodeList.set(i, '1'); System.out.println('執(zhí)行此條指令后的內(nèi)態(tài)為:' + getInternalState() + ',輸入為:' + binCodeList.get(i) + ',STOP'); System.out.println('此步驟的結(jié)果為:'); System.out.println(f.toString(binCodeList));}s = 1;System.out.println(); } public static void main(String[] args) {TuringMachine tm = new TuringMachine(); // 創(chuàng)建TuringMachine的實(shí)例tmSystem.out.println('請(qǐng)輸入一個(gè)十進(jìn)制數(shù):');Scanner scanner = new Scanner(System.in);try { int decNum = scanner.nextInt(); tm.setBinCode(tm.f.toBinCode(decNum)); // 將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制編碼并賦值給binCode System.out.println(); tm.runProcess(tm.getBinCode()); // 運(yùn)行圖靈機(jī)} catch (InputMismatchException ex) { System.out.println('輸入有誤!');} } } /** * @description: 該類具有圖靈機(jī)TuringMachine運(yùn)行過程中所需要的一些方法 */class Function { /** * @description: 將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制編碼 * @param: [decNum 十進(jìn)制數(shù)] * @return: java.lang.String */ public String toBinCode(int decNum) {String binCode = '';String binNum = Integer.toBinaryString(decNum); // 十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制數(shù)binNum += ','; // 用,標(biāo)識(shí)此二進(jìn)制數(shù)到此已完整,后面的0都忽略不計(jì)System.out.println('這個(gè)數(shù)的二進(jìn)制表示為:' + binNum);// 利用for循環(huán)對(duì)二進(jìn)制數(shù)binNum中的字符進(jìn)行遍歷,根據(jù)其中的每個(gè)字符得出二進(jìn)制編碼binCodefor (int i = 0; i < binNum.length(); i++) { // 0 -> 0 if (binNum.charAt(i) == ’0’) {binCode += '0';// 1 -> 10 } else if (binNum.charAt(i) == ’1’) {binCode += '10';// , -> 110 } else if (binNum.charAt(i) == ’,’) {binCode += '110'; }}binCode = '0' + binCode + '00';System.out.println('這個(gè)數(shù)的二進(jìn)制編碼為:' + binCode);return binCode; } /** * @description: 將二進(jìn)制編碼轉(zhuǎn)換為十進(jìn)制數(shù) * @param: [binCode 二進(jìn)制編碼] * @return: int */ public int toDecNum(String binCode) {int decNum = 0;String binNum = '';// 先利用for循環(huán)對(duì)ArrayList類型的binCode進(jìn)行遍歷,根據(jù)其中的每個(gè)元素得出二進(jìn)制編碼binCodefor (int i = 0; i < binCode.length(); i++) { // 0 -> 0 if (binCode.charAt(i) == ’0’) {binNum += '0'; } else if (binCode.charAt(i) == ’1’) {// 10 -> 1if (binCode.charAt(i + 1) == ’0’) { binNum += '1'; i++; // 110 -> ,} else if (binCode.charAt(i + 1) == ’1’) { binNum += ','; break;} }}System.out.println('二進(jìn)制表示:' + binNum);decNum = Integer.parseInt(binNum.substring(0, binNum.length() - 1), 2); // 將二進(jìn)制編碼binCode轉(zhuǎn)化為十進(jìn)制數(shù)System.out.println('十進(jìn)制表示:' + decNum);return decNum; } /** * @description: 將二進(jìn)制編碼binCode存放到binCodeList中 * @param: [binCode 二進(jìn)制編碼] * @return: java.util.List<java.lang.String> */ public List<String> toArrayList(String binCode) {binCode = binCode.replaceAll('', ' ').trim(); // 將binCode中的每個(gè)字符用空格分隔開,并去掉首尾的空格// 根據(jù)分隔符空格分隔出binCode中的每個(gè)字符存放到binCodeList中List<String> binCodeList = new ArrayList<>(Arrays.asList(binCode.split(' ')));return binCodeList; } /** * @description: 將binCodeList轉(zhuǎn)換為二進(jìn)制編碼binCode * @param: [binCodeList 存放binCode的容器] * @return: java.lang.String */ public String toString(List<String> binCodeList) {String binCode = String.join('', binCodeList);return binCode; } }總結(jié)

本次測(cè)試是模擬圖靈機(jī)對(duì)十進(jìn)制數(shù)進(jìn)行乘2運(yùn)算,并輸出每一步驟的結(jié)果。

本次測(cè)試的關(guān)鍵問題在于圖靈機(jī)運(yùn)行過程和算法的理解,圖靈機(jī)判斷當(dāng)前內(nèi)態(tài)和輸入后執(zhí)行指令,在這里我才用switch語(yǔ)句根據(jù)內(nèi)態(tài)的值判斷執(zhí)行哪個(gè)指令方法,再根據(jù)輸入判斷具體執(zhí)行什么指令,通過for循環(huán)模擬右移操作。到此這篇關(guān)于如何用Java模擬XN*2圖靈機(jī)的文章就介紹到這了,更多相關(guān)Java內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
91九色精品| 久久精品资源| 国产日韩中文在线中文字幕| 国产精品视频一区二区三区| 91久久国产| 国产一区二区三区四区| 亚洲一区导航| 999久久久精品国产| 国产精品第十页| 亚洲精品视频一二三区| 精品一区免费| 久久精选视频| 欧美日韩国产免费观看视频| 国产色噜噜噜91在线精品| 午夜在线精品| 丝瓜av网站精品一区二区 | 国精品产品一区| 欧美一区二区三区久久精品| 日本少妇一区二区| 97久久亚洲| 日韩1区2区3区| 亚洲乱码一区| 亚洲tv在线| 日本精品国产| 久久精品97| 国产一区一一区高清不卡| 成人黄色av| 午夜av不卡| 国产真实久久| 亚洲中午字幕| 91九色综合| 精品国产麻豆| 99tv成人| 日欧美一区二区| 精品视频高潮| 精品视频免费| av不卡在线| 国产探花在线精品| 福利一区二区| 亚洲激情av| 国产欧美69| 日本蜜桃在线观看视频| 91精品一区二区三区综合在线爱| 最新日韩av| 国产视频一区二| 国产麻豆久久| 亚洲精品国产精品粉嫩| 美女视频黄免费的久久| 在线日韩视频| 国产精品hd| 91九色精品| 综合在线一区| av资源亚洲| 日韩一区二区三免费高清在线观看 | 在线天堂资源www在线污| 1024精品一区二区三区| 国产麻豆一区| av一区二区高清| 久久亚洲图片| 国产精品亚洲欧美一级在线| 日韩精品午夜| 欧美激情日韩| 婷婷综合成人| 国产精品97| 日韩精品一卡二卡三卡四卡无卡| 国产午夜一区| 日日夜夜免费精品视频| 视频一区中文| 激情久久一区二区| 日韩国产一区二| 天使萌一区二区三区免费观看| а√天堂8资源在线| 国产日韩免费| 亚洲精品极品| 91精品成人| 免费观看久久av| zzzwww在线看片免费| 日本国产欧美| 伊人久久亚洲| 精品视频91| 欧美aa在线视频| 国产欧美日韩一级| 日韩精品电影一区亚洲| 一区在线免费| 久久久久亚洲| 欧美中文一区二区| 成人av二区| 日韩午夜av在线| 欧美亚洲国产激情| 久久国产免费| 亚洲深爱激情| 另类激情亚洲| 欧美日一区二区三区在线观看国产免| 日韩午夜视频在线| 99精品视频在线观看免费播放| 日韩在线短视频| 一区二区视频欧美| 久久福利在线| 国产一区调教| 91欧美在线| 五月天综合网站| 中文字幕日韩亚洲| 蜜桃久久av一区| 成人黄色av| 尹人成人综合网| 久久狠狠久久| 国产一区一一区高清不卡| a日韩av网址| 亚洲日产国产精品| 午夜亚洲福利在线老司机| 欧美一级网址| 欧美精品高清| 日韩一区精品| 久久精品日韩欧美| 久久久久国产精品一区三寸| 国产麻豆精品| 最新日韩av| 精品亚洲a∨一区二区三区18| 亚洲午夜视频| 免费一级欧美在线观看视频 | 亚洲成人三区| 国产精品久久久久久久免费软件| 你懂的亚洲视频| 偷拍亚洲精品| 在线一区二区三区视频| 色婷婷综合网| 日韩精品一级| 国产精品人人爽人人做我的可爱| 日韩免费视频| 国产剧情在线观看一区| 亚洲精品电影| 久久uomeier| 日韩欧美二区| 国产精品.xx视频.xxtv| 国产视频一区三区| 日韩av在线中文字幕| 国产精品xx| 亚洲精品成人一区| 天堂资源在线亚洲| 成人在线视频免费| 91视频一区| 成人午夜亚洲| 日韩精品免费一区二区夜夜嗨| 好吊视频一区二区三区四区| 视频一区在线播放| 99久久九九| 日韩欧美综合| 亚洲黄色网址| 首页国产欧美久久| 亚洲一区免费| 午夜欧美理论片| 国产一区观看| 欧美日韩国产探花| 999国产精品999久久久久久| 美女一区网站| 三上悠亚国产精品一区二区三区| 国产精品不卡| 亚洲三区欧美一区国产二区| 亚洲一区日韩| 综合一区在线| 麻豆成人91精品二区三区| 天堂av在线| 999国产精品视频| 国产精品嫩草99av在线| 免费的成人av| 日韩欧美综合| 在线亚洲欧美| 日韩精品永久网址| 婷婷精品进入| 国产精品xvideos88| 久久精品国产久精国产爱| 91精品韩国| 亚洲精品在线国产| 久久国产电影| 亚洲一区二区三区中文字幕在线观看| 亚洲免费一区三区| 免费在线观看一区| 老司机精品久久| 国产激情欧美| 999久久久91| 国产精品nxnn| 亚洲一区国产一区| 日韩欧美精品一区二区综合视频| 日韩欧美中文字幕电影| 日韩国产欧美一区二区三区| 国产精品久久久久av电视剧| 红桃视频欧美| 免费在线观看一区| 久久亚洲图片| 欧美精品一区二区久久| 日日夜夜免费精品| 日韩精品一区二区三区免费观影 | 日本在线观看不卡视频| 亚洲成人va| 亚洲精品黄色| 亚洲1区在线| 欧美/亚洲一区| 久久99影视| 亚洲精品一二| 欧美一级网址|