Essential C++ 第一章,猜数代码分析ch1.h

 分析

  1. /**************************************************
  2.  * Essential C++ -- Stanley Lippman
  3.  * Addison-Wesley 
  4.  * ISBN 0-201-48518-4
  5.  * homepage: www.objectwrite.com
  6.  * email: slippman@objectwrite.com
  7.  *************************************************/
  8. #ifndef CH1_H_
  9. #define CH1_H_
  10. #include <vector>
  11. #include <iostream>
  12. #include <string>
  13. using namespace std;
  14. // external type definitions
  15. typedef const vector<unsigned int>* (*pfunc)(int);
  16. typedef const vector<unsigned int>*   pvec;
  17. enum num_sequence { ns_unk,
  18.      ns_fib, ns_pell, ns_lucas, ns_tri, ns_sq, ns_pent,
  19.      ns_cnt = 6, ns_wrong_msg = 4
  20. };
  21. struct user_profile {
  22.     string       name;
  23.     int          guesses;
  24.     int          correct;
  25.     int          level;
  26.     num_sequence cur_seq;
  27.     pvec         cur_seq_vec;
  28.     int          pos;
  29. };
  30.            
  31. // external structures
  32. extern pfunc       gen_elems[];
  33. extern const char *name_seq[]; 
  34. extern const char *wrong_msg[];
  35. extern int         level_size[];
  36. extern bool greet_user( user_profile* );
  37. extern void play_game( user_profile* );
  38. extern void display_statistics( user_profile* );
  39. extern pvec Fibonacci_gen_elems( int ); 
  40. extern pvec Pell_gen_elems( int );     
  41. extern pvec Lucas_gen_elems( int ); 
  42. extern pvec Triangular_gen_elems( int ); 
  43. extern pvec Square_gen_elems( int ); 
  44. extern pvec Pentagonal_gen_elems( int ); 
  45. extern void        set_seq( user_profile*, int );
  46. extern const char* seq_id( user_profile* );
  47. extern void        init_user( user_profile *puser, const string& nm, int level );
  48. extern bool        correct_guess( user_profile *puser, int guess );
  49. inline void set_seq_vec( user_profile *pu, pvec p ) { pu->cur_seq_vec = p; }
  50. inline pvec seq_vec( user_profile *pu ) { return pu->cur_seq_vec; }
  51. inline string 
  52. user_name( user_profile *puser )
  53.         { return puser->name; }
  54. inline num_sequence 
  55. seq( user_profile *puser )
  56.         { return puser->cur_seq; }
  57. inline void 
  58. bump_guess( user_profile *puser )
  59.         { ++puser->guesses; }
  60. inline void 
  61. bump_correct( user_profile *puser )
  62. {
  63.     bump_guess( puser );
  64.     ++puser->correct;
  65. }
  66. #include <cstdlib>
  67. inline num_sequence 
  68. gen_seq_id( unsigned int ui )
  69. {
  70.     srand( ui++ );
  71.     return static_cast<num_sequence>( ( rand() % ns_cnt ) + 1 );
  72. }
  73. inline void trace( const string& where, const string& msg, const string& data )
  74. {
  75.      cerr << where << " " << msg << " " << data << endl;
  76. }
  77. inline void trace( const string& where, const string& msg, int val )
  78. {
  79.      cerr << where << " " << msg << " " << val << endl;
  80. }
  81. inline void trace( const string& where, const string& msg, int val, int val2 )
  82. {
  83.      cerr << where << " " << msg << " " << val << ' ' << val2 << endl;
  84. }
  85. inline void trace( const string& where, int val1, int val2 )
  86. {
  87.      cerr << where << " " << val1 << " " << val2 << endl;
  88. }
  89. inline bool 
  90. check_integrity( int pos ) 
  91. {
  92.     const int max_elems = 512;
  93.     if ( pos <= 0 || pos > max_elems )
  94.     {
  95.          cerr << "!! invalid position: " << pos
  96.               << " Cannot honor request/n";
  97.          return false;
  98.     }
  99.     return true;
  100. }
  101. inline num_sequence check_sequence( int id )
  102. {
  103.     if ( id <= 0 || id > ns_cnt )
  104.     {
  105.          cerr << "!! invalid sequence value: " << id
  106.               << " Choosing one at random/n";
  107.          id = ( id % ns_cnt ) + 1;
  108.     }
  109.     return static_cast<num_sequence>( id );
  110. }
  111. #endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值