main( ) - PowerPoint PPT Presentation

About This Presentation
Title:

main( )

Description:

... RandomInit(); // cout – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 56
Provided by: educ5490
Category:
Tags: hare | main | tortoise

less

Transcript and Presenter's Notes

Title: main( )


1
?9? ?????
  • ???????
  • ????
  • ???????
  • ????

2
??????
  • ??
  • ??????
  • ??????????,????,???????
  • ?????,???????????

3
????
  • ??????????????????????

main( ) ?????? ???
????????????????,??????,?????????? void
prn_instruction() void play()
4
int main() prn_instruction() play()
return 0
5
prn_instruction???
  • prn_instruction?????????,???????????????????????

void prn_instruction() cout ltlt
"?????????????.\n" cout ltlt "???????,???.\n"
cout ltlt "?????,??,?????\n"
6
play?????
  • Play?????????,????,??????,??????????

void play() char flag y while (flag
Y flag y) coin ?????
??????? if (???? coin) ??????????
else ??????????
7
play?????
  • ????????0????,1????,?????????????0?1???
  • ???????????????????,?????????????????????????????,
    ?????????????,????????,????0????1,?????????,??????
    ???????????????,????????????get_call_from_user?

8
void play() int coin char flag 'Y'
srand(time(NULL)) //??????? while (flag 'Y'
flag 'y') coin rand() 2 /
RAND_MAX//???????? if
(get_call_from_user() coin) cout ltlt "???"
else cout ltlt "???" cout ltlt
"\n????(Y?y)?" cin gtgt flag
9
get_call_from_user???
  • ????????????????????????0?1,?????,????????

int get_call_from_user() int guess // 0
head, 1 tail do cout ltlt "\n??????(0????,1??
??)" cin gtgt guess while
(guess !0 guess !1) return guess
10
????
?????????????. ???????,???. ?????,??,????? ??????
(0????,1????)1 ??? ????(Y?y)?y ??????(0????,1????
)6 ??????(0????,1????)1 ??? ????(Y?y)?n Press
any key to continue
11
?9? ?????
  • ???????
  • ????
  • ???????
  • ????

12
????
  • ??????????,?????????????????????????
  • ???????????????????????????????????????????
  • ??????????????,????????

13
?????????
  • ????
  • ?????
  • ??????
  • ?????
  • ???????????????????,??????????,????,????,????????
    ?????????,????????????????????

14
??????
While (???? ! quit) switch(?????)
case paper, rock, scissor ????
????
???? case game
??????? case help ??????
default ???? ????
15
????
  • ??????selection_by_player
  • ??????selection_by_machine
  • ????compare
  • ???????????report
  • ??????prn_game_status
  • ??????prn_help????

16
???????
  • ??????????,??????????
  • enum p_r_s paper, rock, scissor, game,
  • help, quit
  • enum outcome win, lose, tie, error

17
????
  • ??????
  • ??? main??
  • ??????? selection_by_player?selection_by_machine
  • ???? compare
  • ???? report?prn_game_status?prn_help??

18
Select?????
  • selection_by_player????????????????????,???
  • p_r_s selection_by_player()
  • selection_by_machine??????????????????,??????,???
  • p_r_s selection_by_machine()

19
Compare?????
  • compare?????????????????,?????
  • ???????,??p_r_s???,??????????,??????? ?
  • ???outcome compare(p_r_s, p_r_s)

20
print?????
  • prn_help???????????,???????????????,????????????
  • Report????????,??????????????????????????????????
    ?????,???????
  • prn_game_status???????????,??????????????????????
    ,???????

21
print????????
  • ????????????????Report?prn_game_status?????????
  • Report??????????,prn_game_status???????????
  • ???????????????????,????????????
  • ????????????????????????????,??????????????
  • ????????????????
  • ??report?prn_game_status??????????????

22
??????
  • ?????,????????????????????????????????,??????inclu
    de??????
  • ??,????????????????????
  • ?????????????,???????????,????????????????????????
    ???????????,
  • ???????????????????
  • ifndef ???
  • endif

23
??????
ifndef _name_h define _name_h
??????????? endif
24
?????????????
// ??p_r_s.h // ????????????,??????????????? if
ndef P_R_S define P_R_S include ltiostreamgt
include ltcstdlibgt include ltctimegt using
namespace std enum p_r_s paper, rock,
scissor, game, help, quit enum outcome
win, lose, tie, error outcome
compare(p_r_s player_choice, p_r_s
machine_choice) void prn_final_status()
void prn_game_status() void prn_help()
void report(outcome result) p_r_s
selection_by_machine() p_r_s
selection_by_player() endif
25
??????
//??main.cpp // ????????????? include
"p_r_s.h" int main(void) outcome result
p_r_s player_choice, machine_choice // seed
the random number generator
srand(time(NULL))
26
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() break
default coutltlt " PROGRAMMER
ERROR!\n\n" exit(1)
prn_game_status() return 0
27
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
ltlt " I am " switch(select) case 0 cout
ltlt "paper. " break case 1 cout ltlt "rock.
" break case 2 cout ltlt "scissor. "
break return ((p_r_s) select)
28
p_r_s selection_by_player() char c p_r_s
player_choice prn_help() //?????? cout ltlt
"please select " cin gtgt c switch(c)
case 'p' player_choice paper cout ltlt "you
are paper. " break case 'r'
player_choice rock cout ltlt "you are rock. "
break case 's' player_choice scissor
cout ltlt "you are scissor. "break case 'g'
player_choice game break case 'q'
player_choice quit break default
player_choice help break return
player_choice
29
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
break default cout ltlt " PROGRAMMER
ERRORUnexpected choice!\n\n"
exit(1) return result
30
Print?????
//??print.cpp //????????????? //?prn_game_status
,prn_help?report?? include "p_r_s.h" int
win_cnt 0, lose_cnt 0, tie_cnt 0 //???????
31
void report(outcome result) switch(result)
case win win_cnt
cout ltlt "You win. \n" break case lose
lose_cnt cout ltlt "You
lose.\n" break case tie tie_cnt
cout ltlt"A tie.\n"
break default cout
ltlt " PROGRAMMER ERROR!\n\n"
exit(1)
32
void prn_game_status() cout ltlt endl cout ltlt
"GAME STATUS" ltlt endl cout ltlt "win " ltlt
win_cnt ltlt endl cout ltlt "Lose " ltlt lose_cnt
ltlt endl cout ltlt "tie " ltlt tie_cnt ltlt
endl cout ltlt "Total" ltlt win_cnt
lose_cnt tie_cnt ltlt endl
33
void prn_help() cout ltlt endl ltlt
"The following characters can be used\n"
ltlt " p for paper\n" ltlt " r
for rock\n" ltlt " s for
scissors\n" ltlt " g print the game
status\n" ltlt " h help, print this
list\n" ltlt " q quit the game\n"
34
?9? ?????
  • ???????
  • ????
  • ???????
  • ????

35
??????
  • ??????????????????,?????????
  • ??????????????????????????????????iostream????????
    ,cmath?????????????????????????
  • ?????????????????????????????,????????,???????????
    ?????????????????????????,???????????????

36
???????
  • ??????
  • ???????????,???????????????????????????
  • ??????????
  • ??????????????????
  • ??????????????

37
????????
?9.1??,???????????????????????????????????????????
,?????????????????
  • ????
  • ?9.1?,???????0?1
  • ?9.2?,???????0?2
  • ??????,???????0?3?????0?9
  • ?????????low?high??????
  • int RandomInteger(int low, int high)
  • ?????RandomInit()????????????

38
????
  • ??????????????????????????
  • ????,??????????????,?????????????

39
??????
//??Random.h //????????? ifndef
_random_h define _random_h //??RandomInit //??
RandomInit() //????????????? void
RandomInit() //??RandomInteger //??n
RandomInteger(low, high) //?????????low?high?????
?,??low?high int RandomInteger(int low, int
high) endif
40
????
  • ???????????????????????Random.h,??????Random.cpp?
  • ???????
  • ???????????????
  • include?cpp?????????
  • ?????????????,????????????????????????
  • ?????????????????????????????

41
//??Random.cpp //??????Random? include
ltcstdlibgt include ltctimegt include
"Random.h" //??RandomInit //????????????????????
? void RandomInit() srand(time(NULL))
42
// ??RandomInteger // ????0?RAND_MAX???????high
- low 1 ? // ????????????????????,????low? //
???????????,???high?????i????? //(i?0?high-low),??
??low i int RandomInteger(int low, int high)
return (low (high - low 1) rand() /
(RAND_MAX 1))
43
?9? ?????
  • ???????
  • ????
  • ???????
  • ????

44
???? -- ????
?? ???? ???? ???
?? ?? 50 ???3?
?? ?? 20 ???6?
?? ?? 30 ?????
?? ?? 20 ??
?? ??? 20 ???9?
?? ?? 10 ???14?
?? ??? 30 ???3?
?? ??? 20 ???2?
45
????????
  • ?????tortoise?hare????????????
  • ??????
  • ???????????????????,?????????????????
  • ???????70??

46
?????
main() int hare 0, tortoise 0, timer 0
//timer????,?0???? while (hare lt RACE_END
tortoise lt RACE_END) tortoise
????????????????? hare
????????????????? ??????????????
timer if (hare gt tortoise) cout ltlt
"\n hare wins!" else cout ltlt "\n tortoise
wins!"
47
????
  • ???????????
  • int move_tortoise()
  • ???????????
  • int move_hare()
  • ??????????????
  • void print_position(int timer, int tortoise,
    int hare)

48
????
  • ???
  • ????
  • move_tortoise
  • move_hare()
  • ????
  • print_position

49
???
include "Random.h" //?????? include
ltiostreamgt using namespace std const int
RACE_END 70 //??????? int move_tortoise() int
move_hare() void print_position(int, int, int)
50
int main() int hare 0, tortoise 0, timer
0 RandomInit() //?????? cout ltlt
"timer tortoise hare\n" //???? while (hare lt
RACE_END tortoise lt RACE_END) tortoise
move_tortoise() //???? hare
move_hare() //???? print_position(timer,
tortoise, hare) timer if
(hare gt tortoise) cout ltlt "\n hare wins!\n"
else cout ltlt "\n tortoise wins!\n" return
0
51
??????????
  • ?????,???????????
  • ????????0-9??????,????????0-4?,????????,5-6??
    ????,7-9???????????????????????????

?? 50
?? 20
?? 30
52
Move??
// ???move.cpp include "Random.h"
//??????????? int move_tortoise() int
probability RandomInteger(0,9) //??0?9??????
if (probability lt 5) return 3 //?? else if
(probability lt 7) return -6 //?? else
return 1 //??
53
int move_hare() int probability
RandomInteger(0,9) if (probability lt 2) return
0 //?? else if (probability lt 4) return -9
//??? else if (probability lt 5) return 14
//?? else if (probability lt 8) return
3 //??? else return -2 //???
54
Print??
// ???print.cpp include ltiostreamgt using
namespace std void print_position(int timer,
int t, int h) if (timer 6 0) cout ltlt
endl //??6???? cout ltlt timer ltlt '\t' ltlt t
ltlt '\t' ltlt h ltlt '\n'
55
??
  • ?????
  • ???????????????????????
  • ????????????????????????????????????????
  • ????????????
  • ????????????????????
Write a Comment
User Comments (0)
About PowerShow.com