walk.h

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C头文件 代码 · 共 47 行

H
47
字号
#ifndef _RANDOMWALK_H#define _RANDOMWALK_H// Owen Astrachan, 6/20/96, modified 5/1/99// class for implementing a one dimensional random walk//// constructor specifies number of steps to take, random walk// goes left or right with equal probability//// two methods for running simulation://// void Simulate()  -- run a complete simulation//// Init(); HasMore(); Next() -- idiom for starting and iterating//                              one step at a time// accessor functions://// int Position()    -- returns x coordinate//                      (# steps left/right from origin)// int Current()     -- alias for GetPosition()//// int TotalSteps()  -- returns total # steps takenclass RandomWalk{  public:    RandomWalk(int maxSteps);  // constructor, parameter = max # steps    void Init();               // take first step of walk    bool HasMore();            // returns false if walk finished, else true    void Next();               // take next step of random walk        void Simulate();           // take all steps in simulation        int Position()   const;     // returns position (x coord) of walker    int Current()    const;     // same as position    int TotalSteps() const;     // returns total # of steps taken      private:      void TakeStep();           // simulate one step of walk    int myPosition;            // current x coordinate    int mySteps;               // # of steps taken    int myMaxSteps;            // maximum # of steps allowed};#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?