本文介绍了不知道如何将想法转化为C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好。我的程序需要修改。 实际上,它是一台通用的图灵机,我的目标是让它平行。 并行图灵机执行从1.txt到其磁带的指令,只打印一步(这很重要!),后来它与2.txt,3.txt和4.txt一样。在该机器重复这些操作之后,如果一个磁带完成(暂停),其他操作将不会停止,直到指令说明。 因此,并行机器打印由执行的指令磁带逐步更改: 1.txt第一次更改 2.txt第一次更改 3.txt第一次更改 4.txt第一次更改 1.txt第二次更改 2.txt第二次更改 ... 我有一些想法,但我不知道如何将其翻译成C ++: 创建机器类的向量,每个输入一个,然后在每一步上遍历整个机器列表,如果不是end_of_tape,则调用执行。 有人可以帮我翻译吗? 我尝试过: #include < iostream > #include < 算法 > #include < vector > #include < fstream > #include < iterator > 使用 namespace std; struct 指令{ string current_state; char current_char; char next_char; int move; string next_state; }; istream&运算符>>(istream& is,instruction& i){ is>> i.current_state>> i.current_char>> i.next_char; char move_ch;是>> move_ch; if (move_ch == ' R' )i.move = 1 ; else if (move_ch == ' L')i.move = - 1 ; else i.move = 0 ; 返回是>> i.next_state; } struct machine { int 的位置; 字符串状态; string tape; bool 执行( const 指令& i); bool end_of_tape() const { 返回位置< 0 ||位置> tape.size(); } }; bool machine :: execute( const instruction& i){ if (i.current_state!= state || tape [position]!= i.current_char) return false ; state = i.next_state; tape [position] = i.next_char; position + = i.move; return true ; } istream&运算符>>(istream& is,machine& m){ m.state = 0 ; 是>> m.position; 返回是>> m.tape; } int main(){ string filename; cout<< 输入文件名:; cin>>文件名; ifstream ifs(filename,ifstream :: in ); machine m; ifs>>米; vector< instruction>程序; copy(istream_iterator< instruction>(ifs), istream_iterator< instruction>(), back_inserter(program)); for ( size_t i = 0 ;!m.end_of_tape()&& i< program.size();){ if (m.execute(program [ i]))i = 0 ; else i ++; cout<< m.tape<< ENDL; } cout<< HALT!; return 0 ; } 解决方案 您的概念不是您的任务的首要任务。 你只需要一个带有运行标志,指令缓冲区(向量)和执行命令的机器类。 类计算机 { bool 正在运行; vector< instruction>说明; bool loadInstructions(string file); bool 执行(); // pop或者你需要一个计数器 }; 这些机器可以在创建后添加到一个向量循环的向量中。 输出并使用调试器。 函数出了什么问题 执行( int machine, int 指令) 和一个类似的循环 for (i = 0 ; i< max_program_size; ++ i) for (m = 0 ; m< machines; ++ m)执行(m,i); ? 为了使算法平行,我会考虑使用OpenMP。它使用起来非常简单,因为它只需要几个pragma来使代码并行。你只需要决定哪些变量是共享的和私有的每个线程,并在pragma中相应地标记它们。 这是对OpenMP的一个有用的介绍: OpenMP实践 [ ^ ] Hi guys. My program needs to be modified.Actually, it's a universal turing machine and my aim is to make it parallel.Parallel turing machine executes instructions from 1.txt to its tape and prints it with only one step of changes (that's important!), later it does the same with 2.txt, 3.txt and 4.txt. After that machine repeats those operations with a condition if one tape is done (halted) the others won't stop until the instruction says.So, parallel machine prints step by step changed by executed instructions tapes:1.txt 1st change2.txt 1st change3.txt 1st change4.txt 1st change1.txt 2nd change2.txt 2nd change...I have some ideas but I don't know how to translate it into C++:Create vector of machine class, one for each input and then on each step go through the whole list of machines and if not end_of_tape, call execute.Could someone help me and translate it?What I have tried:#include <iostream>#include <algorithm>#include <vector>#include <fstream>#include <iterator>using namespace std;struct instruction { string current_state; char current_char; char next_char; int move; string next_state;}; istream& operator>>(istream& is, instruction& i) { is >> i.current_state >> i.current_char >> i.next_char; char move_ch; is >> move_ch; if(move_ch == 'R') i.move = 1; else if(move_ch == 'L') i.move = -1; else i.move = 0; return is >> i.next_state;} struct machine { int position; string state; string tape; bool execute(const instruction &i); bool end_of_tape() const { return position < 0 || position > tape.size(); }}; bool machine::execute(const instruction &i) { if(i.current_state != state || tape[position] != i.current_char) return false; state = i.next_state; tape[position] = i.next_char; position += i.move; return true;} istream & operator>>(istream &is, machine &m) { m.state = "0"; is >> m.position; return is >> m.tape;} int main() { string filename; cout << "Enter filename: "; cin >> filename; ifstream ifs(filename, ifstream::in); machine m; ifs >> m; vector<instruction> program; copy(istream_iterator<instruction>(ifs), istream_iterator<instruction>(), back_inserter(program)); for(size_t i = 0; !m.end_of_tape() && i < program.size();) { if (m.execute(program[i])) i = 0; else i++; cout << m.tape << endl; } cout << "HALT!"; return 0;} 解决方案 Your concept is not top down of your task. You only need a machine class with a running flag, a instructions buffer (vector) and an execute command.class Machine{ bool running; vector <instruction> instructions; bool loadInstructions(string file); bool execute();//pop or you need a counter};These machines could be added after creation in a vector which than is looped for the execution.Make some output and use the debugger.What's wrong with a functionexecute( int machine, int instruction)and a loop likefor (i=0; i<max_program_size; ++i) for (m=0; m<machines; ++m) execute(m,i);?To make an algorithm parallel I would look into using OpenMP. It is very simple to use as it only takes a few pragmas to make the code parallel. You just have to decide which variables are shared and private to each thread and 'label' them accordingly in the pragma.Here is a useful introduction to OpenMP : OpenMP Hands-on[^] 这篇关于不知道如何将想法转化为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 07:10