This week, were working towards a program that can load a set of criteria from one file (the criteria file) and use it as a basis for operation on another file (the data file). Well develop this finished program through a series of steps, starting with our previous work. Well be working within the example concept of a census to be tabulated.
crit.txt
sample criteria
census.txt
census data
The criteria file is a text file with a long name and 2-character abbreviation of the state. There is one state record per line, with each field separated by a space (state names with more than one word have the underscore character (_) in place of the space).
Example criteria file:
New_Jersey NJ New_York NY Ohio OH
The census file is a text file with the first and last name, plus the state of residence for each census respondent. There is one record per line, and each field is separated by a space.
Example data file:
Michael Platner DC Red Cavaney DC John Felten MA Kathleen McCarthy MA Nancy Brickley MA Arthur Brownstein NJ Gary Hamilton NJ
Example 1Basic setup for reading census data into an array of appropriate class objects.
Example 2Tallying two states in quick and dirty fashion. Notice how code is duplicated to add additional states.
Example 3Streamlining by pulling the Tally out as a separate function
Example 4Showing a whole bunch of states being tallied, as well as calculating other responses.
Example 5Streamlining by creating a new class to hold criteria data.
Example 6Tallying two states using the new class. Notice how code is duplicated to add additional states.
Example 7Streamlining by using an array of states to hold the criteria
Example 8Streamlining by pulling out the calculations and display routines as separate functions. This is easy now that both the data and criteria are in arrays.
Example 9Streamlining by loading the criteria data from an external file.
Example 10Slick Trick: Using the last element of the criteria array to hold other responses.
After finally getting to Example 10, were left with one pretty large program. However, some of the helper functions we wrote to streamline things are obviously related to the criteria class, others are obviously related to the data class, and a third type seem to be dependent on both.
The typical way to clean up that big file is to move all the related functions, prototypes and class definitions into their own set of files. Heres the way I would break up Example 10: