⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 run.cpp

📁 这是个很好的源代码,有用的
💻 CPP
📖 第 1 页 / 共 4 页
字号:
  for (int i = 0; getline (fin, file_line); i++)
  {
    data_o.push_back(vector<string>());
    istringstream isstr (file_line.c_str ());

    copy(
         istream_iterator<string>(isstr),
         istream_iterator<string> (),
         back_inserter (data_o[i])
        );

    cout << "\t  Row#" << setw(3) << (i + 1) << " ---> ";
    copy (
          data_o[i].begin (),
          data_o[i].end (),
          ostream_iterator<string> (cout, " ")
         );
    cout << endl;


  } // while
  cout << "\t" << string (setw23, '-') << endl;
  cout << endl;

  return true;

} // read_file_S


// =========
bool Run::read_metafile_S (const string& filename_i)
{
// ------------------------------------------
// Metafile structure :
// Field#1 : <description file name>
// Field#2 : <number of tapes>
// Field#3 : <states file name>
// Field#4 : <alphabet file name>
// Field#5 : <transitions file name>
// Field#6 : <input words file#1 name>
// Field#7 : <input words file#2 name> [optionaly]
// .........
// Field#n : <input words file#(n-4) name> [optionaly]
// ------------------------------------------
#define MIN_NUMBER_OF_FIELDS_IN_META_FILE_ROW	5

  metafile_name_s = filename_i;
  if (!read_file_S (metafile_name_s , metafile_data_s))
  {
    FATAL_MSG	("Unable to read metafile <"
		<< metafile_name_s
		<< ">"
		);
    assert (metafile_data_s.empty());
    return false;
  }

  if (metafile_data_s.empty())
  {
    FATAL_MSG	("Metafile <"
		<< metafile_name_s
		<< "> is empty"
		);
    metafile_data_s.clear();
    return false;
  }

  for (size_t i = 0; i< metafile_data_s.size(); i++)
  {
    if (metafile_data_s[i].empty())
    {
      FATAL_MSG	("Metafile <"
		<< metafile_name_s
		<< ">, Row#"
		<< (i + 1)
		<< " is empty"
		);
      metafile_data_s.clear();
      return false;
    }

    if (metafile_data_s[i].size() < MIN_NUMBER_OF_FIELDS_IN_META_FILE_ROW)
    {
      FATAL_MSG	("Metafile <"
		<< metafile_name_s
		<< ">, Row#"
		<< (i + 1)
		<< " ---> illegal number of fields : "
		<< metafile_data_s[i].size()
		<< "; MIN ALLOWED = "
		<< MIN_NUMBER_OF_FIELDS_IN_META_FILE_ROW
		);
      metafile_data_s.clear();
      return false;
    }

    // --- Checking ---
    for (size_t j = 0; j < metafile_data_s[i].size(); j++)
    {

      assert (!metafile_data_s[i][j].empty());

      switch (j)
      {
        case METAFILE_FIELD__NUMBER_OF_TAPES :
          if (!(is_udec (const_cast<char*>(metafile_data_s[i][j].c_str()))))
          {
            FATAL_MSG	("Metafile <"
			<< metafile_name_s
			<< ">, Row#"
			<< (j + 1)
			<< " ---> non-numeric Number-Of-Tapes: <"
			<< metafile_data_s[i][j]
			<< ">"
			);
            metafile_data_s.clear();
            return false;
          }
          break;

        case METAFILE_FIELD__DESCR_FILE_NAME :
        case METAFILE_FIELD__STATES_FILE_NAME :
        case METAFILE_FIELD__ALPHABET_FILE_NAME :
        case METAFILE_FIELD__TRANSITIONS_FILE_NAME :
          // Do nothing
          break;

        default :
          // Do nothing
          break;

       } // switch (j)

    } // for (size j = 0; ...

  } // for (size_t i = 0; ...
  // ----------------
  show_metafile_content_S ();
  return true;

} // read_metafile_S

// =========
bool Run::read_alphabet_file ()
{
  assert (!alphabet_file_name_.empty());
  if (!read_file_S (alphabet_file_name_, alphabet_file_data_))
  {
    FATAL_MSG	("Unable to read alphabet file <"
		<< alphabet_file_name_
		<< ">"
		);
    assert (alphabet_file_data_.empty());
    return false;
  }

  if (alphabet_file_data_.empty())
  {
    FATAL_MSG	("Alphabet file <"
		<< alphabet_file_name_
		<< "> is empty"
		);
    alphabet_file_data_.clear();
    return false;
  }

  if (alphabet_file_data_.size() != ALPHABET_FILE__TOTAL_ROWS)
  {
    FATAL_MSG	("Illegal number of rows in Alphabet file <"
		<< alphabet_file_name_
		<< "> : must be "
		<< ALPHABET_FILE__TOTAL_ROWS
		<< ", actulally - "
		<< alphabet_file_data_.size()
		);
    alphabet_file_data_.clear();
    return false;
  }

  assert (alphabet_file_data_.size() == ALPHABET_FILE__TOTAL_ROWS);

  for (size_t i = 0; i < alphabet_file_data_.size(); i++)
  {
    if (alphabet_file_data_[i].empty())
    {
      FATAL_MSG	("Alphabet file <"
		<< alphabet_file_name_
		<< ">, Row#"
		<< (i + 1)
		<< " is empty"
		);
      alphabet_file_data_.clear();
      return false;
    }
  } // for (size_t i = 0; ...


  // ----------------
  show_alphabet_file_content ();

  return true;

} // read_alphabet_file



// =========
bool Run::read_states_file ()
{
  assert (!states_file_name_.empty());
  if (!read_file_S (states_file_name_, states_file_data_))
  {
    FATAL_MSG	("Unable to read states file <"
		<< states_file_name_
		<< ">"
		);
    assert (states_file_data_.empty());
    return false;
  }

  if (states_file_data_.empty())
  {
    FATAL_MSG	("States file <"
		<< states_file_name_
		<< "> is empty"
		);
    states_file_data_.clear();
    return false;
  }

  if (states_file_data_.size() != STATES_FILE__TOTAL_ROWS)
  {
    FATAL_MSG	("Illegal number of rows in States file <"
		<< states_file_name_
		<< "> : must be "
		<< STATES_FILE__TOTAL_ROWS
		<< ", actulally - "
		<< states_file_data_.size()
		);
    states_file_data_.clear();
    return false;
  }

  assert (states_file_data_.size() == STATES_FILE__TOTAL_ROWS);

  for (size_t i = 0; i < states_file_data_.size(); i++)
  {
    if (states_file_data_[i].empty())
    {
      FATAL_MSG	("States file <"
		<< states_file_name_
		<< ">, Row#"
		<< (i + 1)
		<< " is empty"
		);
      states_file_data_.clear();
      return false;
    }
  } // for (size_t i = 0; ...


  // ----------------
  show_states_file_content ();

  return true;

} // read_states_file



// =========
bool Run::read_descr_file ()
{
  assert (!descr_file_name_.empty());
  if (!read_file_S (descr_file_name_, descr_file_data_))
  {
    WARNING_MSG	("Unable to read descr file <"
		<< descr_file_name_
		<< ">"
		);
    assert (descr_file_data_.empty());
    return true;
  }

  if (descr_file_data_.empty())
  {
    WARNING_MSG	("States file <"
		<< descr_file_name_
		<< "> is empty"
		);
    descr_file_data_.clear();
    return true;
  }

  // ----------------
  show_descr_file_content ();

  return true;

} // read_descr_file



// =========
bool Run::read_transitions_file ()
{
  assert (!transitions_file_name_.empty());
  if (!read_file_S (transitions_file_name_, transitions_file_data_))
  {
    FATAL_MSG	("Unable to read transitions file <"
		<< transitions_file_name_ 
		<< ">"
		);
    assert (transitions_file_data_.empty());
    return false;
  }


  if (transitions_file_data_.empty())
  {
    FATAL_MSG	("Transitions file <"
		<< transitions_file_name_
		<< "> is empty"
		);
    transitions_file_data_.clear();
    return false;
  }

  for (size_t i = 0; i < transitions_file_data_.size(); i++)
  {
    if (transitions_file_data_[i].empty())
    {
      FATAL_MSG	("Transitions file <"
		<< transitions_file_name_
		<< ">, Row#"
		<< (i + 1)
		<< " is empty"
		);
      transitions_file_data_.clear();
      return false;
    }
  } // for (size_t i = 0; ...


  for (size_t i = 0; i < transitions_file_data_.size(); i++)
  {
    if (transitions_file_data_[i].size() != TRANSITIONS_FILE__TOTAL_FIELDS(number_of_tapes_))
    {
      FATAL_MSG	("Transitions file <"
		<< transitions_file_name_
		<< ">"
		<< endl
		<< string (22, ' ')
		<< "Row#"
		<< (i + 1)
		<< " ---> Invalid number of fields"
		<< endl
		<< string (22, ' ')
		<< "Must be (for Turing machine with "
		<< number_of_tapes_
		<< " tapes) "
		<< TRANSITIONS_FILE__TOTAL_FIELDS(number_of_tapes_)
		<< ", actually - "
		<< transitions_file_data_[i].size()
		);
      transitions_file_data_.clear();
      return false;
    }
  } // for (size_t i = 0; ...


  // ----------------
  show_transitions_file_content ();


  return true;
} // read_transitions_file

// =========
bool Run::read_input_words_file (const string& filename_i)
{
  input_words_files_data_.push_back (vector<vector<string> >());
  assert (!filename_i.empty());
  if (!read_file_S (filename_i, *input_words_files_data_.rbegin()))
  {
    FATAL_MSG	("Unable to read input words file <"
		<< filename_i
		<< ">"
		);
    assert (input_words_files_data_.rbegin()->empty());
    return false;
  }

  // ----------------
  // show_input_words_files_content ();

  return true;
} // read_input_words_file


// =========
void Run::show_metafile_structure_S ()
{
// ------------------------------------------
// Metafile structure :
// Field#1 : <descr file name>
// Field#2 : <number of tapes>
// Field#3 : <states file name>
// Field#4 : <alphabet file name>
// Field#5 : <transitions file name>
// Field#6 : <input words file#1 name>
// Field#7 : <input words file#2 name> [optionaly]
// .........
// Field#n : <input words file#(n-4) name> [optionaly]
// ------------------------------------------
const string text1 ("Metafile structure");

  cout	<< endl;
  cout	<< space3
	<< string (setw10, '#')
	<< " "
	<< text1
	<< " "
	<< string (setw10, '#')
	<< endl;

  cout	<< space4 << "Each row contains data related to some Turing machine" << endl;
  cout	<< space3 << string (setw10 + 1 + text1.size() + 1 + setw10, '-') << endl;

  cout	<< space4 << "Field#1 : " << "description file name" << endl;
  cout	<< space4 << "Field#2 : " << "number of tapes" << endl;
  cout	<< space4 << "Field#3 : " << "states file name" << endl;
  cout	<< space4 << "Field#4 : " << "alphabet file name" << endl;
  cout	<< space4 << "Field#4 : " << "transitions file name" << endl;
  cout	<< space4 << "Field#6 : " << "input words file#1 name" << endl;
  cout	<< space4 << "Field#7 : " << "input words file#2 name [optional]" << endl;
  cout	<< space4 << "....... : " << "........." << endl;

  cout	<< space4 << "Field#n : " << "input words file#(n-4) name [optional]" << endl;

  cout	<< space3 << string (setw10 + 1 + text1.size() + 1 + setw10, '#') << endl;
} // show_metafile_structure_S


// =========
void Run::show_alphabet_file_structure_S  ()
{
// ------------------------------------------
// States file structure :
// Row#1 : list of empty symbols (empty symbols alphabet)
// Row#2 : list of internal symbols
// Row#3 : list of input symbols
// ------------------------------------------
const string text1 ("Alphabet file structure");

  cout	<< endl;
  cout	<< space3
	<< string (setw10, '=')
	<< " "
	<< text1
	<< " "
	<< string (setw10, '=')
	<< endl;

  cout	<< space4 << "Row#1 : " << "list of empty symbols (empty symbols alphabet)" << endl;
  cout	<< space4 << "Row#2 : " << "list of internal symbols" << endl;
  cout	<< space4 << "Row#3 : " << "list of input symbols" << endl;


  cout	<< space3 << string (setw10 + 1 + text1.size() + 1 + setw10, '=') << endl;

} // show_alphabet_file_structure_S



// =========
void Run::show_descr_file_structure_S  ()
{
const string text1 ("Description file structure");

  cout	<< endl;
  cout	<< space3
	<< string (setw10, '=')
	<< " "
	<< text1
	<< " "
	<< string (setw10, '=')
	<< endl;

  cout	<< space4 << "--- Your text :" << endl;
  cout	<< space4 << "--- Description of your Turing machine" << endl;

  cout	<< space3 << string (setw10 + 1 + text1.size() + 1 + setw10, '=') << endl;

} // show_descr_file_structure_S



// =========
void Run::show_states_file_structure_S  ()
{
// ------------------------------------------
// States file structure :
// Row#1 : list of initial states

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -