📄 run.cpp
字号:
// ==============================================================
//
// Copyright (c) 2002-2003 by Alex Vinokur.
//
// For conditions of distribution and use, see
// copyright notice in version.h
//
// ==============================================================
// ##############################################################
//
// SOFTWARE : Turing Machine (C++ Implementation)
// FILE : run.cpp
//
// DESCRIPTION :
// Class Run (Implementation)
//
// ##############################################################
// ###############
#include "run.h"
#define setw3 3
#define setw4 4
#define setw5 5
#define setw7 7
#define setw8 8
#define setw9 9
#define setw10 10
#define setw12 12
#define setw13 13
#define setw14 14
#define setw15 15
#define setw23 23
#define setw43 43
const string space3 (3, ' ');
const string space4 (4, ' ');
const string space5 (5, ' ');
const string space6 (6, ' ');
const string space8 (8, ' ');
const string space9 (9, ' ');
const string prefixa ("%%");
enum MetafileFields
{
// --- DON'T CHANGE ---
METAFILE_FIELD__DESCR_FILE_NAME,
METAFILE_FIELD__NUMBER_OF_TAPES,
METAFILE_FIELD__STATES_FILE_NAME,
METAFILE_FIELD__ALPHABET_FILE_NAME,
METAFILE_FIELD__TRANSITIONS_FILE_NAME,
METAFILE_FIELD__INPUT_WORDS_FILE1_NAME
};
enum StatesFileRows
{
// --- DON'T CHANGE ---
STATES_FILE_ROW__INITIAL_STATES,
STATES_FILE_ROW__HALTING_STATES,
STATES_FILE_ROW__INTERNAL_STATES,
STATES_FILE__TOTAL_ROWS
};
enum AlphabetFileRows
{
// --- DON'T CHANGE ---
ALPHABET_FILE_ROW__EMPTY_SYMBOLS,
ALPHABET_FILE_ROW__INTERNAL_SYMBOLS,
ALPHABET_FILE_ROW__INPUT_SYMBOLS,
ALPHABET_FILE__TOTAL_ROWS
};
// ---- Tapes nummeration : 0, 1, 2, ...
#define TRANSITIONS_FILE__TOTAL_FIELDS(number_of_tapes) (3*number_of_tapes + 2)
#define TRANSITIONS_FILE__CUR_STATE_FIELD 0
#define TRANSITIONS_FILE__NEXT_STATE_FILED(number_of_tapes) (number_of_tapes + 1)
#define TRANSITIONS_FILE__CUR_SYMBOL_FIELD(tape_no) (tape_no + 1)
#define TRANSITIONS_FILE__NEXT_SYMBOL_FIELD(number_of_tapes, tape_no) (number_of_tapes + 2*tape_no + 2)
#define TRANSITIONS_FILE__SHIFT_FIELD(number_of_tapes, tape_no) (number_of_tapes + 2*tape_no + 3)
// =========
// =========
// Constructor-0
Aux::Aux ()
{
init();
}
// =========
// Constructor-0
Aux::~Aux ()
{
}
// =========
void Aux::init ()
{
assert (help_request_keys_.empty());
help_request_keys_.push_back ("-h");
help_request_keys_.push_back ("--help");
help_request_keys_.push_back ("?");
}
// =========
// =========
string Run::metafile_name_s;
vector<vector<string> > Run::metafile_data_s;
Aux aux;
const vector<string> Run::help_request_keys_s (aux.help_request_keys_);
// =========
// =========
// Constructor-1
Run::Run (
size_t serial_number_i,
size_t number_of_tapes_i,
const string& descr_file_name_i,
const string& states_file_name_i,
const string& alphabet_file_name_i,
const string& transitions_file_name_i,
const vector<string>& input_words_files_names_i
)
:
serial_number_ (serial_number_i)
{
ostringstream oss1, oss21, oss22;
cout << endl;
oss1 << space8
<< prefixa
<< string (setw12, '=')
<< " Turing Machine# "
<< serial_number_
<< " "
<< string (setw12, '=');
oss21 << space8
<< prefixa
<< string (setw3, '=')
<< " PreProcessing Metafile Data : ";
oss22 << " "
<< string (setw3, '=');
cout << space8 << prefixa << string (setw43, '=') << endl;
cout << oss1.str() << endl;
cout << oss21.str() << "BEGIN" << oss22.str() << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
check_results_ = init_number_of_tapes (number_of_tapes_i);
if (check_results_) check_results_ = init_descr (descr_file_name_i);
if (check_results_) check_results_ = init_states (states_file_name_i);
if (check_results_) check_results_ = init_alphabet (alphabet_file_name_i);
if (check_results_) check_results_ = init_transitions (transitions_file_name_i);
if (check_results_) check_results_ = create_transitions();
if (check_results_) check_results_ = init_input_words (input_words_files_names_i);
cout << space8 << prefixa << string (setw43, '=') << endl;
cout << oss21.str() << " END " << oss22.str() << endl;
cout << oss1.str() << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
if (check_results_)
{
check_results_ = invoke_machine ();
}
if (!check_results_)
{
FATAL_MSG ("Can't invoke this Turing Machine");
}
}
// =========
// Destructor
Run::~Run ()
{
}
// =========
bool Run::init_number_of_tapes (size_t number_of_tapes_i)
{
number_of_tapes_ = number_of_tapes_i;
if (number_of_tapes_ == 0)
{
FATAL_MSG ("Illegal number of tapes : "
<< number_of_tapes_
);
return false;
}
return true;
}
// =========
bool Run::init_descr(const string& descr_file_name_i)
{
bool ret_bool_value;
descr_file_name_ = descr_file_name_i;
ret_bool_value = read_descr_file ();
if (!ret_bool_value)
{
cout << endl;
show_descr_file_structure_S ();
show_descr_file_sample_S ();
cout << endl;
}
return ret_bool_value;
}
// =========
bool Run::init_states(const string& states_file_name_i)
{
bool ret_bool_value;
states_file_name_ = states_file_name_i;
ret_bool_value = read_states_file ();
if (!ret_bool_value)
{
cout << endl;
show_states_file_structure_S ();
show_states_file_sample_S ();
cout << endl;
}
return ret_bool_value;
}
// =========
bool Run::init_alphabet(const string& alphabet_file_name_i)
{
bool ret_bool_value;
alphabet_file_name_ = alphabet_file_name_i;
ret_bool_value = read_alphabet_file ();
if (!ret_bool_value )
{
cout << endl;
show_alphabet_file_structure_S ();
show_alphabet_file_sample_S ();
cout << endl;
}
return ret_bool_value;
}
// =========
bool Run::init_transitions(const string& transitions_file_name_i)
{
bool ret_bool_value;
transitions_file_name_ = transitions_file_name_i;
ret_bool_value = read_transitions_file ();
if (!ret_bool_value)
{
cout << endl;
show_transitions_file_structure ();
show_transitions_file_sample_S ();
cout << endl;
}
return ret_bool_value;
}
// =========
bool Run::init_input_words (const vector<string>& input_words_files_names_i)
{
for (size_t i = 0; i < input_words_files_names_i.size(); i++)
{
input_words_files_names_.push_back (input_words_files_names_i[i]);
}
assert (!input_words_files_names_.empty());
for (size_t i = 0; i < input_words_files_names_.size(); i++)
{
if (!read_input_words_file (input_words_files_names_[i]))
{
cout << endl;
show_input_words_file_structure_S ();
show_input_words_file_sample_S ();
cout << endl;
return false;
}
}
show_input_words_files_content ();
return true;
}
// =========
bool Run::create_transitions()
{
state_t cur_state;
vector<symbol_t> cur_symbols;
state_t next_state;
vector<SymbolAndShift> next_symbols_and_shifts;
typedef Transitions_t::value_type value_type;
pair<Transitions_t::iterator, bool> the_pair;
transitions_.clear();
assert (!transitions_file_data_.empty());
for (size_t i = 0; i < transitions_file_data_.size(); i++)
{
assert (transitions_file_data_[i].size() == TRANSITIONS_FILE__TOTAL_FIELDS(number_of_tapes_));
cur_symbols.clear();
next_symbols_and_shifts.clear();
cur_state = transitions_file_data_[i][TRANSITIONS_FILE__CUR_STATE_FIELD];
next_state = transitions_file_data_[i][TRANSITIONS_FILE__NEXT_STATE_FILED(number_of_tapes_)];
for (size_t j = 0; j < number_of_tapes_; j++)
{
cur_symbols.push_back (
transitions_file_data_[i][TRANSITIONS_FILE__CUR_SYMBOL_FIELD(j)]);
next_symbols_and_shifts.push_back (
SymbolAndShift (
transitions_file_data_[i][TRANSITIONS_FILE__NEXT_SYMBOL_FIELD(number_of_tapes_, j)],
transitions_file_data_[i][TRANSITIONS_FILE__SHIFT_FIELD(number_of_tapes_, j)]
)
);
}
the_pair = transitions_.insert (value_type (CurSituation (cur_state, cur_symbols), NextSituation (next_state, next_symbols_and_shifts)));
if (!the_pair.second)
{
const size_t the_offset = 21;
FATAL_MSG ("Problematic transition data file."
<< endl
<< string (the_offset, ' ')
<< " See row#"
<< i
<< " (Note. First row has #0)."
<< endl
<< string (the_offset, ' ')
<< " Some row has the same current state and symbols on tape"
);
return false;
}
}
return true;
}
// =========
bool Run::invoke_machine ()
{
ostringstream oss1, oss21, oss22;
cout << endl;
oss1 << space8
<< prefixa
<< string (setw12, '=')
<< " Turing Machine# "
<< serial_number_
<< " "
<< string (setw12, '=');
oss21 << space8
<< prefixa
<< string (setw7, '=')
<< " Machine Definition : ";
oss22 << " "
<< string (setw8, '=');
cout << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
cout << oss1.str() << endl;
cout << oss21.str() << "BEGIN" << oss22.str() << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
TuringMachine turing_machine (
number_of_tapes_,
// ---------------
descr_file_data_,
// ---------------
states_file_data_[STATES_FILE_ROW__INITIAL_STATES], // initial states
states_file_data_[STATES_FILE_ROW__HALTING_STATES], // halting states
states_file_data_[STATES_FILE_ROW__INTERNAL_STATES], // internal states
// ---------------
alphabet_file_data_[ALPHABET_FILE_ROW__EMPTY_SYMBOLS], // empty symbols
alphabet_file_data_[ALPHABET_FILE_ROW__INTERNAL_SYMBOLS], // internal symbols
alphabet_file_data_[ALPHABET_FILE_ROW__INPUT_SYMBOLS], // input symbols
// ---------------
transitions_
);
if (!turing_machine.get_check_results())
{
FATAL_MSG ("Invalid Turing Machine definition");
}
cout << space8 << prefixa << string (setw43, '=') << endl;
cout << oss21.str() << " END " << oss22.str() << endl;
cout << oss1.str() << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
if (!turing_machine.get_check_results()) return false;
assert (turing_machine.get_check_results());
// ------------------------
for (size_t i = 0; i < input_words_files_data_.size(); i++)
{
ostringstream oss31, oss32, oss41, oss42;
oss31 << space8
<< prefixa
<< string (setw9, '=')
<< " Processing Input Words ";
oss41 << space8
<< prefixa
<< string (setw13, '=')
<< " Set# "
<< (i + 1)
<< " : ";
oss32 << " "
<< string (setw9, '=');
oss42 << " "
<< string (setw14, '=');
cout << endl;
cout << endl;
cout << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
cout << oss1.str() << endl;
cout << oss31.str() << oss32.str() << endl;
cout << oss41.str() << "BEGIN" << oss42.str() << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
turing_machine.process_input (input_words_files_data_[i]);
cout << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
cout << oss41.str() << " END " << oss42.str() << endl;
cout << oss31.str() << oss32.str() << endl;
cout << oss1.str() << endl;
cout << space8 << prefixa << string (setw43, '=') << endl;
}
return true;
} // invoke_machine
// =========
bool Run::read_file_S (const string& filename_i, vector<vector<string> >& data_o)
{
string file_line;
assert (!filename_i.empty());
ifstream fin (filename_i.c_str());
if (!fin)
{
DIAGNOSTIC_MSG ("Unable to open file <"
<< filename_i
<<
">"
);
return false;
}
cout << endl;
cout << "\t" << string (setw23, '-') << endl;
cout << "\t--- File " << filename_i << endl;
cout << "\t" << string (setw23, '-') << endl;
data_o.clear();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -