home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / ~Solutions Submitted / Problem 09 - Three and One / Solution.p < prev   
Encoding:
Text File  |  1998-06-19  |  2.7 KB  |  65 lines  |  [TEXT/CWIE]

  1. (*
  2. Problem 09 - State Machine
  3.  
  4. type GetNextCharProc = function( curstate: UInt32 ): UInt32;
  5.  
  6. procedure StateMachineInit( var state_machine: Handle; states: UInt32; chars:
  7. UInt32 );
  8. procedure AddTransition( state_machine: Handle; state1, state2: UInt32;
  9. first_char, last_char: UInt32 );
  10. procedure RunStateMachine( state_machine: Handle; start_state: UInt32;
  11. GetNextChar: GetNextCharProc; var stop_state: UInt32 );
  12.  
  13. This problem requires you to maintain and operate a state machine, consisting
  14. of rules that change the current machine state based on the current input.  Our
  15. state machines do not require that you produce any output other than the
  16. machine state.
  17.  
  18. StateMachineInit creates a new, empty state machine ready to accept state
  19. transitions, and containing states states (numbers 1 to states) and chars
  20. characters (numbered 1 to chars).  All the state machine information must be
  21. stored in the real Mac memory manager handle - it will be disposed with
  22. DisposeHandle and that must release all memory, so dont store any extra
  23. information outside the handle.  Also, you must be able to deal with having
  24. multiple state machine instantiated simultaneously.
  25.  
  26. AddTransition adds a transition from state1 to state2 for characters between
  27. (inclusive) first_char and last_char.  When you get to state1 and get a
  28. character between first_char and last_char you should proceed to state2.  The
  29. new transition overrides and previous transitions for these characters from
  30. state1 (ie, if you get transition 1->2 for chars 1-10, and then 1->3 for chars
  31. 5-6, you now have transition 1->2 for chars 1-4 and 7-10 and transition 1->3
  32. for chars 5-6).
  33.  
  34. RunStateMachine starts in start_state and calls GetNextChar repeatedly until
  35. either it returns 0 or until it returns a character for which there is no
  36. transition at the current state.  Each time GerNewChar supplies a character,
  37. the current state is updated based on the transition rule that applies to that
  38. combination of current state and current input.  When GetNextChar supplies a
  39. state for which no transition rule exists, the state machine halts and the
  40. final state is returned in stop_state.
  41. *)
  42.  
  43. unit Solution;
  44.  
  45. interface
  46.  
  47. // Do not modify the interface
  48.  
  49.     uses
  50.         Types, Files;
  51.         
  52. type GetNextCharProc = function( state_machine: Handle): UInt32;
  53.  
  54. procedure StateMachineInit( var state_machine: Handle; states: UInt32; chars: UInt32 );
  55. procedure AddTransition( state_machine: Handle; state1, state2: UInt32; first_char, last_char: UInt32 );
  56. procedure RunStateMachine( state_machine: Handle; start_state: UInt32; GetNextChar: GetNextCharProc; var stop_state: UInt32 );
  57.  
  58. implementation
  59.  
  60. // Fill in your solution and then submit this folder
  61.  
  62. // Team Name: FILL IN YOUR TEAM NAME!
  63.  
  64. end.
  65.