essential c++ 基於第二章的內容採用面向過程的方法實現猜字遊戲的基本實現代碼

來源:互聯網
上載者:User

 

// 基於第二章的內容採用面向過程的方法實現猜字遊戲。
//初次認真的編程,還是費了些力氣。
//總是有些粗糙的。還望各位大俠們的指教!
//沒有良好的溝通交流介面,想像一下也可以的,相信大家也能設計出來 。

 

#include < iostream >
#include < vector >

using namespace std;

//範圍檢查
bool is_size_ok( int size );

 //計算數列中size個元素,
//並返回含有這些元素的靜態容器的地址
const vector < int > * fibon_seq( int size );
const vector < int > *lucas_seq( int size );
const vector < int > *pell_seq( int size );
const vector < int > *triang_seq( int size );
const vector < int > *square_seq( int size );
const vector < int > *pent_seq( int size );

//核對是否猜對相應的元素。
//bool fibon_elem( int size, int &elem );

bool seq_elem( int size, int &elem ,const vector < int > * ( *seq_ptr )( int ));

 

#include < string >

using namespace std;

int main()
{
 bool next_seq = true ; //顯示下一組數列
 bool go_for_it = true;  //使用者想再猜一次
 bool got_it = false;//使用者是否猜對
 int num_tries = 0;   // 使用者猜過的總次數
 int num_right = 0;   //使用者猜對的總次數
 const int seq_size=18;  //用於顯示每個數列的前三個元素,用來提示使用者!也可用二維數組實現的。
 int arr[seq_size]={
  1,1,2,
  1,3,4,
  1,2,5,
  1,3,6,
  1,4,9,
  1,5,12
 };
 vector< int > elem_seq( arr, arr+seq_size );  //定義容器代替上面的數組
 int cur_tuple = 0;   //用於控制顯示每個數列的前三個元素。
 // int elem;

 

 const int max_seq = 6;

 //用以顯示數列的名字
 string seq_names[ max_seq ] = {
  "Fibonacci",
   "Lucas",
   "Pell",
   "Triangular",
   "Square",
   "Pentagonal"
 };

 //函數的指標數組
 const vector < int > * ( * seq_array[] ) ( int ) = {
  fibon_seq,lucas_seq,pell_seq,
   triang_seq,square_seq,pent_seq
 };
 int seq_index = 0;   //用以控制函數指標數組的值。
 const vector < int > * ( *seq_ptr ) ( int )= 0; //指向各個數列的函數指標

 

 //這一塊就有些模糊了,可能還可以用來最佳化,待想好了再來最佳化吧。呵呵!
 //初來乍到,向各位學習!
 while ( next_seq == true && cur_tuple < seq_size )
 {
  
  seq_ptr = seq_array[ seq_index ];

  while ( ( got_it == false ) && ( go_for_it == true ) )
  {
   int user_guess = 0;
   
   
   int pos = 4;
   
    cout<< " the first three elements of the sequence are: "
     <<elem_seq[ cur_tuple ]<< ", "
     <<elem_seq[ cur_tuple+1 ]<< " , "
     <<elem_seq[cur_tuple+2]
     <<"/n what is the next element? ";

    bool go_on = true;//一個數列的續猜控制。
    bool wrong_guess = false;//用於控制猜錯時
    while( go_on )
    {
     num_tries++;
     cout<<" /n please entry you guess number :";
     cin>>user_guess;

     if( seq_elem( pos, user_guess, seq_ptr ) )
     {
      ++num_right;
      got_it = true;
      wrong_guess = true;
      cout<<" /n Very good.Yes "
       <<user_guess
       <<" is the next element in the "
       << seq_names[ seq_index ]<<" sequence. /n";

      cout<<"/nDo you want to guess the next numbers.Y/N ?";
       char conti_guess;
      cin>>conti_guess;
      if( conti_guess == 'n' || conti_guess == 'N' )
       go_on = false;
      else
      {
        pos++;
        continue;
      }
     }
     
     else
     {
      //cout<<" you guess is wrong ! ";
      cout<<" Sorry ! you are wrong!"
       <<"/n do you want to try it,Y/N ?";
      char usr_rsp;
      cin>>usr_rsp;
      if( usr_rsp == 'N' || usr_rsp == 'n' )
      {
       go_on = false;
       go_for_it = false;
      }
      
     }
    }        
  }
 

  got_it = false;
  go_for_it = true;
  cout<<"/n Do you want to try another sequence ? Y/N :";
   char try_again;
  cin>> try_again;
  if( try_again == 'N' || try_again == 'n' )
   next_seq = false;
   cur_tuple+=3;
  cout<<endl;
  seq_index++;
  if( seq_index == 5  )
   cout<< " The game is over ! " << endl;
 }

   cout<<" Ok! That is all, As below is your result :
   cout<< " The try_times is : " << num_tries <<" and the right_times is "<< num_right <<endl;
   cout<<" the score is " << float(num_right)/float(num_tries) << endl;;
 return 0;
}

//pos範圍的限制與檢測
bool is_size_ok( int size )
{
 const int max_size = 1024;
 if( size <= 0 || size > 1024 )
 {
  cerr << " the size is  out of the area 0 ~ 1024 ";
  return false;
 }

 return true;
}

//求得Fibonic
const vector < int > * fibon_seq( int size )
{
 if( !is_size_ok( size ) )
  return 0;
 static vector < int > elem;

 for( int ix = elem.size(); ix < size; ix++ )
 {
  if( ix == 1 || ix == 0 )
   elem.push_back(1);
  else
   elem.push_back( elem[ ix-1 ] + elem[ ix-2 ]);
 }

 return &elem;
}

//求得lucas
const vector < int > *lucas_seq( int size )
{
 if( !is_size_ok( size ) )
  return 0;
 static vector < int > elem;

 for( int ix = elem.size(); ix < size; ix++ )
 {
  if( ix == 0 )
   elem.push_back( 1 );
  else
  
   if( ix == 1 )
   elem.push_back( 3 );
  
  else
   elem.push_back( elem[ ix-1 ] + elem[ ix-2 ]);
 }

 return &elem;
}

//實現pell_seq

const vector < int > *pell_seq( int size )
{
 if( !is_size_ok( size ) )
  return 0;
 static vector < int > elem;

 for( int ix = elem.size(); ix < size; ix++ )
 {
  if( ix == 0 )
   elem.push_back( 1 );
  else
  
   if( ix == 1 )
   elem.push_back( 2 );
  
  else
   elem.push_back( 2 * elem[ ix-1 ] + elem[ ix-2 ]);
 }

 return &elem;
}

//實現 triang_seq
const vector < int > *triang_seq( int size )
{
 if( !is_size_ok( size ) )
  return 0;
 static vector < int > elem;

 for( int ix = elem.size(); ix < size; ix++ )
 {
  if( ix == 0 )
   elem.push_back( 1 );
  //if( ix == 1 )
  // elem.push_back( 3 );
  else
   elem.push_back( elem[ ix-1 ] + ix + 1 );
 }

 return &elem;
}

//實現square_seq
const vector < int > *square_seq( int size )
{
 if( !is_size_ok( size ) )
  return 0;
 static vector < int > elem;

 for( int ix = elem.size(); ix < size; ix++ )
 {
  if( ix == 0 )
   elem.push_back( 1 );
  //if( ix == 1 )
  // elem.push_back( 4 );
  else
   elem.push_back( elem[ ix-1 ] + 2*ix + 1 );
 }

 return &elem;
}

//實現pent_seq
const vector < int > *pent_seq( int size )
{
 if( !is_size_ok( size ) )
  return 0;
 static vector < int > elem;

 for( int ix = elem.size(); ix < size; ix++ )
 {
  if( ix == 0 )
   elem.push_back( 1 );
  //if( ix == 1 )
  // elem.push_back( 3 );
  else
   elem.push_back( elem[ ix-1 ] + 3*ix + 1 );
 }

 return &elem;
}

/*
bool fibon_elem( int size, int &elem )
{
 const vector < int > *pseq = fibon_seq( size );
 if( !pseq )
 {
  elem = 0;
  return false;
 }

  if( elem = ( *pseq )[ size-1 ] )
 return true;
  else
   return false;
}
*/

//用於核對猜的數位對錯
bool seq_elem( int size, int &elem, const vector <int > * ( *seq_ptr )( int ) = 0 )
{
 if( !seq_ptr )
  cout<<" Inernal Error: seq_ptr is set to null! ";
 const vector < int > *pseq = seq_ptr( size );
 if( !pseq )
 {
  elem = 0;
  return false;
 }

  if( elem == ( *pseq )[ size-1 ] )
 return true;
  else
   return false;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.