essential c++ 基于第二章的内容采用面向过程的方法实现猜字游戏的基本实现代码
2009-04-20 13:25
991 查看
// 基于第二章的内容采用面向过程的方法实现猜字游戏。
//初次认真的编程,还是费了些力气。
//总是有些粗糙的。还望各位大侠们的指教!
//没有良好的沟通交流界面,想像一下也可以的,相信大家也能设计出来 。
#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;
}
//初次认真的编程,还是费了些力气。
//总是有些粗糙的。还望各位大侠们的指教!
//没有良好的沟通交流界面,想像一下也可以的,相信大家也能设计出来 。
#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;
}
相关文章推荐
- 基于Java代码实现游戏服务器生成全局唯一ID的方法汇总
- SQL Server中导入/导出 Excel (包括SQL实现和Net代码实现)的基本方法
- 基于python代码实现简易滤除数字的方法
- 基于jquery实现select选择框内容左右移动添加删除代码分享
- php基于curl实现随机ip地址抓取内容的方法
- C# j基本操作-拷贝文件夹的所有内容到另一个文件夹内: 复制代码 1 public static void CopyDir(string srcPath, string实现文件夹的复制以及删除
- 用Java代码实现栈数据结构的基本方法归纳
- 基于C语言实现五子棋游戏完整实例代码
- 基于JQuery实现相同内容合并单元格的代码
- 基于JavaScript实现新增内容滚动播放效果附完整代码
- SQL Server中导入/导出 Excel (包括SQL实现和Net代码实现)的基本方法 .
- 基于xmpp实现android端实现即时通讯---asmack基本方法(三)
- Java基于Swing实现的打猎射击游戏代码
- 线程同步基本概念+实现线程同步的2种方法(无代码示例)
- spring security采用基于简单加密 token 的方法实现的remember me功能
- 基于Java代码实现判断春节、端午节、中秋节等法定节假日的方法
- 基于xmpp实现android端实现即时通讯---asmack基本方法(三)
- python基于pygame实现响应游戏中事件的方法(附源码)
- 基于C语言实现的迷宫游戏代码
- python基于pygame实现响应游戏中事件的方法(附源码)