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

📄 life.cpp

📁 数据结构与程序设计教材源码 数据结构与程序设计教材源码
💻 CPP
字号:
Life::Life()
/* 
Post: The members of a Life object are
      dynamically allocated and initialized.
Uses: The class Hash_table and the class List.
*/
{
   living = new List<Cell *>;
   is_living = new Hash_table;
}
 
Life::~Life()
/* 
Post: The dynamically allocated members of a Life object
      and all ell objects that they reference are deleted.
Uses: The class Hash_table and the class List.
*/
{
   Cell *old_cell;
   for (int i = 0; i < living->size(); i++) {
      living->retrieve(i, old_cell);
      delete old_cell;
   }
   delete is_living;         //  Calls the Hash_table destructor
   delete living;            //  Calls the List destructor
}
 
int Life::neighbor_count(int x, int y) const
{
   int count = 0;
   for (int x_add = -1; x_add < 2; x_add++)
     for (int y_add = -1; y_add < 2; y_add++)
        if (is_living->retrieve(x + x_add, y + y_add)) count++;
   if (is_living->retrieve(x, y)) count--;
   return count;
}
 
bool Life::retrieve(int x, int y) const
{
   return is_living->retrieve(x, y);
}
 
Error_code Life::insert(int row, int col)
/* 
Pre:   The cell with coordinates row and col does not
      belong to the Life configuration.
Post: The cell has been added to the configuration.  If insertion into
      either the List or the Hash_table fails, an error
      code is returned.
Uses: The class List, the class Hash_table, and the struct Cell
*/
{
   Error_code outcome;
   Cell *new_cell = new Cell(row, col);

   int index = living->size();
   outcome = living->insert(index, new_cell);
   if (outcome == success)
      outcome = is_living->insert(new_cell);
   if (outcome != success)
      cout << " Warning: new Cell insertion failed" << endl;
   return outcome;
}
 
void Life::print()
/* 
Post: A central window onto the Life object is displayed.
Uses: The auxiliary function Life::retrieve.
*/
{
   int row, col;
   cout << endl << "The current Life configuration is:" << endl;
   for (row = 0; row < 20; row++) {
      for (col = 0; col < 80; col++)
         if (retrieve(row, col)) cout << '*';
         else cout << ' ';
      cout << endl;
   }
   cout << "There are " << living->size() << " living cells."
        <<  endl;
}
 
void Life::update()
/* 
Post: The Life object contains the next generation of configuration.
Uses: The class Hash_table and the class Life and its
      auxiliary functions.
*/
{
   Life new_configuration;
   Cell *old_cell;
   for (int i = 0; i < living->size(); i++) {
      living->retrieve(i, old_cell);      //  Obtain a living cell.
      for (int row_add = -1; row_add < 2; row_add ++)
         for (int col_add = -1; col_add < 2; col_add++) {
            int new_row = old_cell->row + row_add,
                new_col = old_cell->col + col_add;
//  new_row, new_col is now a living cell or a neighbor of a living cell,

            if (!new_configuration.retrieve(new_row, new_col))
               switch (neighbor_count(new_row, new_col)) {
               case 3:  //  With neighbor count 3, the cell becomes alive.
                  new_configuration.insert(new_row, new_col);
                  break;

               case 2:  //  With count 2, cell keeps the same status.
                  if (retrieve(new_row, new_col))
                     new_configuration.insert(new_row, new_col);
                  break;

               default: //  Otherwise, the cell is dead.
                  break;
               }
         }
   }

//  Exchange data of current configuration with data of new_configuration.
   List<Cell *> *temp_list = living;
   living = new_configuration.living;
   new_configuration.living = temp_list;
   Hash_table *temp_hash = is_living;
   is_living = new_configuration.is_living;
   new_configuration.is_living = temp_hash;
}
 
void Life::initialize()
/* 
Post:
Uses:
*/
{
   Cell new_cell;
   int row, col;
   int read_rows, read_cols;
   char c;

   cout << "Please enter the number of rows and columns to be read in.\n";
   cout << "Note: The map for any cells that are not within the range:\n";
   cout << "0 <= row < 20 and 0 <= column < 80 will not be printed to the terminal\n";
   cout << "Such cells will, however, still be included within the calculations.\n";

   cout << "Please enter number of rows: " << flush;
   cin >> read_rows;
   cout << "Please enter number of columns: " << flush;
   cin >> read_cols;
   do {
      cin.get(c);
   } while (c != '\n');

   cout << "Please enter blanks to signify empty cells.\n";
   for (row = 0; row < read_rows; row++) {
      cout << row << " : ";
      for (col = 0; col < read_cols; col++) {
         cin.get(c);
         if (c == '\n') break;
         if (c != ' ') {
            insert(row, col);
         }
      }
      while (c != '\n') cin.get(c);
   }
}

⌨️ 快捷键说明

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