|
|
|
|
|
|
|
(text box continued from previous page) |
|
|
|
|
|
|
|
|
else if (currChar != !) // Rule: If currChar isn't
// = or !,
exclamEncountered = FALSE; // switch states
}
else // State == no ! encountered
if (currChar == !) // Rule: If currChar is !,
exclamEncountered = TRUE; // switch states
inFile.get(currChar);
}
cout << count << != operators were found. << endl;
return 0;
} |
|
|
|
| |
|
|
|
|
The thing to notice about this program is that it keeps track of the previous input value without actually storing it into a variable. Instead, it simply remembers the one aspect of the value that pertains to the problemnamely, whether it was an exclamation mark or something else. |
|
|
|
| |
|
|
|
|
Program FiniteState is longer than program NotEqualCount. For such a simple problem, it's clearly less efficient to use a finite state machine approach than to store the previous value. However, for problems that are more complexones that involve many different statesfinite state machines give us a way in which to organize the states so that we don't leave out any combinations. |
|
|
|
| |
|
|
|
|
Finite state machine models are used extensively in the development of compilers and operating systems. They also are an important tool in researching theoretical issues of computing. |
|
|
|
|
|
|
|
|
|
It's one thing to understand how a loop works when you look at it and something else again to design a loop that solves a given problem. In this section, we look at how to design loops. We can divide the design process into two tasks: designing the control flow and designing the processing that takes place in the loop. And we can break each task into three phases: the task itself, initialization, and update. It's also important to specify the state of the program when it exits the loop, since a loop that leaves variables and files in a mess is not well designed. |
|
|
|
|
|
|
|
|
There are seven different points to consider in designing a loop: |
|
|
|
|
|
|
|
|
1. What is the condition that ends the loop? |
|
|
|
|
|
|
|
|
2. How should the condition be initialized? |
|
|
|
|
|
|
|
|
3. How should the condition be updated? |
|
|
|
|
|
|
|
|
4. What is the process being repeated? |
|
|
|
|
|
|
|
|
5. How should the process be initialized? |
|
|
|
|
|
|
|
|
6. How should the process be updated? |
|
|
|
|
|
|
|
|
7. What is the state of the program on exiting the loop? |
|
|
|
|
|