正在加载图片...
$20.3 A FUNCTIONAL,TOP-DOWN SOLUTION 681 This is a typical transition diagram traversal algorithm.(The reader who has written a lexical analyzer will recognize the pattern.)At each stage we are in a state state, originally set to initial;the process terminates when state satisfies is final.For a non-final state we executeexecute state,which takes the current state and returns the user's transition choice through its second argument next,which the function transition uses, together with state,to determine the next state. The→notation is a The technique using a procedure execute state that changes the value of one of its temporary comven- arguments would never be appropriate in good O-O design,but here it is the most tion,used only for this expedient.To signal it clearly,the notation flags an "out"argument such as next with an particular procedure and forread below. arrow-.Instead of a procedure which modifies an argument,C developers would make execute state a side-effect-producing function called as next :execute state (state )we will see that this practice is subject to criticism too. Since execute state does not show any information about any particular interactive application,you must fill in the application-specific properties appearing on level 2 in the figure:transition function;initial state;is final predicate. To complete the design,we must refine the execute state routine describing the actions to be performed in each state.Its body is essentially an abstracted form of the contents of the successive blocks in the initial goto-based version: execute state (in s:INTEGER;out c:INTEGER)is --Execute the actions associated with state s. --returning into c the user's choice for the next state. local a:ANSWER;ok:BOOLEAN do repeat display (s) read(s,→a) ok:correct (s,a) if not ok then message (s,a)end until ok end process (s,a) c :next choice (a) end This assumes level 1 routines with the following roles: display (s)outputs the panel associated with state s. read (s,-a)reads into a the user's answer to the display panel of state s. correct (s,a)returns true if and only if a is an acceptable answer to the question displayed in state s;if so,process(s,a)processes answer a,for example by updating a database or displaying more information;if not,message (s,a)outputs the relevant error message.§20.3 A FUNCTIONAL, TOP-DOWN SOLUTION 681 This is a typical transition diagram traversal algorithm. (The reader who has written a lexical analyzer will recognize the pattern.) At each stage we are in a state state, originally set to initial; the process terminates when state satisfies is_final. For a non-final state we execute execute_state, which takes the current state and returns the user’s transition choice through its second argument next, which the function transition uses, together with state, to determine the next state. The technique using a procedure execute_state that changes the value of one of its arguments would never be appropriate in good O-O design, but here it is the most expedient. To signal it clearly, the notation flags an “out” argument such as next with an arrow →. Instead of a procedure which modifies an argument, C developers would make execute_state a side-effect-producing function called as next := execute_state (state); we will see that this practice is subject to criticism too. Since execute_state does not show any information about any particular interactive application, you must fill in the application-specific properties appearing on level 2 in the figure: transition function; initial state; is_final predicate. To complete the design, we must refine the execute_state routine describing the actions to be performed in each state. Its body is essentially an abstracted form of the contents of the successive blocks in the initial goto-based version: execute_state (in s: INTEGER; out c: INTEGER) is -- Execute the actions associated with state s, -- returning into c the user’s choice for the next state. local a: ANSWER; ok: BOOLEAN do repeat display (s) read (s, →a) ok := correct (s, a) if not ok then message (s, a) end until ok end process (s, a) c := next_choice (a) end This assumes level 1 routines with the following roles: • display (s) outputs the panel associated with state s. • read (s, →a) reads into a the user’s answer to the display panel of state s. • correct (s, a) returns true if and only if a is an acceptable answer to the question displayed in state s; if so, process (s, a) processes answer a, for example by updating a database or displaying more information; if not, message (s, a) outputs the relevant error message. The → notation is a temporary conven￾tion, used only for this particular procedure and for read below
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有