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

📄 tortoiseandhare.cpp

📁 《c++大学教程实验指导书》源码
💻 CPP
字号:
// Chapter 5 of C++ How to Program
// tortoiseandhare.cpp
#include <iostream>

using std::cout;
using std::endl;

#include <cstdlib>

#include <ctime>

#include <iomanip>

using std::setw;

const int RACE_END = 70;

/* Write prototype for moveTortoise here */
/* Write prototype for moveHare here */
/* Write prototype for printCurrentPositions here */

int main()
{
   int tortoise = 1; 
   int hare = 1;
   int timer = 0;

   srand( time( 0 ) );

   cout << "ON YOUR MARK, GET SET\nBANG               !!!!"
        << "\nAND THEY扲E OFF    !!!!\n";
   
   // controls race
   while ( tortoise != RACE_END && hare != RACE_END ) {
      /* Write call for moveTortoise here */
      /* Write call for moveHare here */
      /* Write call for printCurrentPositions here */    
      ++timer;

   } // end while

   // determine winner
   if ( tortoise >= hare )
      cout << "\nTORTOISE WINS!!! YAY!!!\n";

   else
      cout << "Hare wins. Yuch.\n";

   cout << "TIME ELAPSED = " << timer << " seconds" << endl;

   return 0;

   
} // end main

// move tortoise
/* Write function definition header for moveTortoise here */
{
   int x = 1 + rand() % 10;

   // determine which move to make
   if ( x >= 1 && x <= 5 )        // fast plod
      turtlePtr += 3;

   else if ( x == 6 || x == 7 )   // slip
      turtlePtr -= 6;

   else                           // slow plod
      ++( turtlePtr );
   
   // ensure that tortoise remains within subscript range
   if ( turtlePtr < 1 )
      turtlePtr = 1;

   else if ( turtlePtr > RACE_END )
      turtlePtr = RACE_END;

} // end function moveTortoise

// move hare
/* Write function definition for moveHare here */
{
   int y = 1 + rand() % 10;

   /* Write statements that move hare */

   /* Write statements that test if hare is before
      the starting point or has finished the race */

} // end function moveHare

// output positions of animals
/* Write function definition for printCurrentPositions here */
{
   if ( bunnyPtr == snapperPtr ) 
      cout << setw( bunnyPtr ) << "OUCH!!!";      

   else if ( bunnyPtr < snapperPtr ) 
      cout << setw( bunnyPtr ) << "H" 
           << setw( snapperPtr - bunnyPtr ) << "T";

   else
      cout << setw( snapperPtr ) << "T" 
           << setw( bunnyPtr - snapperPtr ) << "H";

   cout << "\n";

} // end function printCurrentPositions



/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *
 * Hall. All Rights Reserved.                                             *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

⌨️ 快捷键说明

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