< previous page page_269 next page >

Page 269
        if (currChar == = &&  // Test for event
            prevChar == !)
            count++;            // Increment counter
        prevChar = currChar;    // Replace previous value
                                //   with current value
        inFile.get(currChar);   // Get next value
    }
    cout << count <<  != operators were found. << endl;
    return 0;
}
Study this loop carefully. It's going to come in handy. There are many times when you must keep track of the last value read in addition to the current value.
THEORETICAL FOUNDATIONS
Finite State Machines
Our program for counting not-equal operators is only one way of accomplishing the task. Another is to design the program using a finite state machine. A finite state machine is an idealized model of a very simple computer. It consists of a set of states and a set of transition rules for changing from one state to another.
For example, a thermostat is a finite state machine with two states: on and off. And it has just two transition rules:
If the measured temperature is less than the set temperature, switch from off to on.
If the measured temperature is more than a degree warmer than the set temperature, switch from on to off.
Only one transition rule can be valid at a time.
0269-01.gif

(text box continues on next page)

 
< previous page page_269 next page >