头文件
包含所有的符号常量定义、类型定义和函数原型声明
每个模块都include这个头文件
注意
链接时,编译器会发现这些类型定义、符号常量和函数原型的声明在程序中反复出现多次
解决方法
需要用到一个新的编译预处理命令:
#ifndef 标识符 … #endif
头文件实现格式
#ifndef _name_h #define _name_h 头文件真正需要写的内容 #endif 石头、剪刀、布游戏的头文件 // 文件:p_r_s.h // 本文件定义了两个枚举类型,声明了本程序包括的所有函数原型 #ifndef P_R_S #define P_R_S #include <iostream> #include <cstdlib> #include <ctime> using namespace std; enum p_r_s { paper, rock, scissor, game, help, quit }; enum outcome { win, lose, tie }; outcome compare(p_r_s player_choice, p_r_s machine_choice); void prn_game_status(); void prn_help(); void report(outcome result); p_r_s selection_by_machine(); p_r_s selection_by_player(); #endif
1.在cpp文件内include自定义的头文件,例如myMath.h,则代码是:#include ___________
2.为了防止对某个头文件的重复包含,需要在每个头文件的开头添加编译预处理指令,指令名是_______和_______
3.c++支持将cpp实现代码混入头文件中,来简化代码编写,c++程序的头文件后缀名是 ____
4.在头文件.h中如果要声明一个int型变量x,需要在代码int x;之前添加 ______ 关键字,并在.cpp文件中定义int x;
主模块的实现
// 文件:main.cpp // 石头、剪子、布游戏的主模块 #include "p_r_s.h" int main() { outcome result; p_r_s player_choice, machine_choice; // seed the random number generator srand(time(NULL)); while ((player_choice = selection_by_player()) != quit) switch (player_choice) { case paper: case rock: case scissor: machine_choice = selection_by_machine(); result = compare(player_choice, machine_choice); report(result); break; case game: prn_game_status(); break; case help: prn_help(); } prn_game_status(); return 0; }
Select模块的实现
//文件:select.cpp //包括机器选择selection_by_machine和玩家选择selection_by_player函数的实现 #include "p_r_s.h" p_r_s selection_by_machine() { int select = (rand() * 3 / (RAND_MAX + 1)); cout << " I am "; switch (select) { case 0: cout << "paper. "; break; case 1: cout << "rock. "; break; case 2: cout << "scissor. "; break; } return ((p_r_s)select); } p_r_s selection_by_player() { char c; p_r_s player_choice; prn_help(); cout << "please select: "; cin >> c; switch (c) { case 'p': player_choice = paper; cout << "you are paper. "; break; case 'r': player_choice = rock; cout << "you are rock. "; break; case 's': player_choice = scissor; cout << "you are scissor. "; break; case 'g': player_choice = game; break; case 'q': player_choice = quit; break; default: player_choice = help; break; } return player_choice; }
Compare模块的实现
//文件:compare.cpp //包括compare函数的实现 #include "p_r_s.h" outcome compare(p_r_s player_choice, p_r_s machine_choice) { outcome result; if (player_choice == machine_choice) return tie; switch (player_choice) { case paper: result = (machine_choice == rock) ? win : lose; break; case rock: result = (machine_choice == scissor) ? win : lose; break; case scissor: result = (machine_choice == paper) ? win : lose; } return result; }
Print模块的实现
/文件:print.cpp //包括所有与输出有关的模块。 //有prn_game_status,prn_help和report函数 #include "p_r_s.h“ static int win_cnt = 0, lose_cnt = 0, tie_cnt = 0; //模块的内部状态 void report(outcome result) { switch (result) { case win: ++win_cnt; cout << "You win. \n"; break; case lose: ++lose_cnt; cout << "You lose.\n"; break; case tie: ++tie_cnt; cout << "A tie.\n"; break; } } void prn_game_status() { cout << endl; cout << "GAME STATUS:" << endl; cout << "win:" << win_cnt << endl; cout << "Lose:" << lose_cnt << endl; cout << "tie:" << tie_cnt << endl; cout << "Total:" << win_cnt + lose_cnt + tie_cnt << endl; } void prn_help() { cout << endl << "The following characters can be used:\n" << " p for paper\n" << " r for rock\n" << " s for scissors\n" << " g print the game status\n" << " h help, print this list\n" << " q quit the game\n"; }