// Based on the content in Chapter 2, the process-oriented method is used to implement the game.
// It took some effort to program carefully for the first time.
// It is always rough. Hope you will give me your advice!
// There is no good communication interface. You can imagine it. I believe you can design it as well.
# Include <iostream>
# Include <vector>
Using namespace STD;
// Check the range
Bool is_size_ OK (INT size );
// Calculate the size elements in the series,
// Return the address of the static container containing these elements
Const vector <int> * maid (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 );
// Check whether the corresponding elements are guessed.
// Bool maid (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; // display the next series
Bool go_for_it = true; // you can guess it again.
Bool got_it = false; // whether the user guessed it
Int num_tries = 0; // The total number of times the user guessed it.
Int num_right = 0; // The total number of user guesses
Const int seq_size = 18; // It is used to display the first three elements of each series and to prompt the user! It can also be implemented using two-dimensional arrays.
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); // define the container instead of the preceding Array
Int cur_tuple = 0; // used to control the first three elements of each series.
// Int ELEM;
Const int max_seq = 6;
// Display the name of a sequence
String seq_names [max_seq] = {
"Maid ",
"Lucas ",
"Pell ",
"Triangular ",
"Square ",
"Pentagonal"
};
// Pointer array of the Function
Const vector <int> * (* seq_array []) (INT) = {
Fibon_seq, lucas_seq, pell_seq,
Triang_seq, square_seq, pent_seq
};
Int seq_index = 0; // used to control the value of the array of function pointers.
Const vector <int> * (* seq_ptr) (INT) = 0; // function pointer to each series
// This part is a bit fuzzy and may be used for optimization. You can optimize it later. Haha!
// Coming soon, learn from you!
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; // continuous guess control of a series.
Bool wrong_guess = false; // used to control the time when an error occurs.
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"
<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 range restriction and Detection
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;
}
// Obtain the 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;
}
// Obtain 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;
}
// Implement 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;
}
// Implement 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;
}
// Implement 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;
}
// Implement 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 = maid (size );
If (! Pseq)
{
ELEM = 0;
Return false;
}
If (ELEM = (* pseq) [size-1])
Return true;
Else
Return false;
}
*/
// Check the right or wrong number of the guess
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;
}