📄 walk.cpp
字号:
#include "walk.h"#include "dice.h"RandomWalk::RandomWalk(int maxSteps) : myPosition(0), mySteps(0), myMaxSteps(maxSteps)// postcondition: no walk has been taken, but walk is ready to go { // work done in initializer list}void RandomWalk::TakeStep()// postcondition: one step of random walk taken{ Dice coin(2); switch (coin.Roll()) { case 1: myPosition--; break; case 2: myPosition++; break; } mySteps++;}void RandomWalk::Init()// postcondition: first step of random walk taken{ myPosition = 0; mySteps = 0; TakeStep();}bool RandomWalk::HasMore()// postcondition: returns true when random walk still going// i.e., when # of steps taken < max. # of steps { return mySteps < myMaxSteps;}void RandomWalk::Next()// postcondition: next step in random walk simulated { TakeStep();}void RandomWalk::Simulate()// postcondition: one simulation completed { for(Init(); HasMore(); Next()) { // simulation complete using iterator methods }}int RandomWalk::Position() const// postcondition: returns position of walker (x coordinate){ return myPosition;}int RandomWalk::Current() const// postcondition: retrns position of walker (x coordinate){ return Position();}int RandomWalk::TotalSteps() const// postcondition: returns number of steps taken by walker { return mySteps;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -