|
|
 |
|
 |
|
|
Hierarchical Records Records in which at least one of the components is itself a record. |
|
|
|
|
|
|
|
|
Let's look at an example where a hierarchical structure is appropriate. A small machine shop keeps information about each of its machines. There is descriptive information, such as the identification number, a description of the machine, the purchase date, and the cost. Statistical information is also kept, such as the number of down days, the failure rate, and the date of last service. What is a reasonable way of representing all this information? First, let's look at a flat (nonhierarchical) record structure that holds this information. |
|
|
|
|
|
|
|
|
typedef char String50[51];
struct MachineRec
{
int idNumber;
String50 description;
float failRate;
int lastServicedMonth; // Assume 1..12
int lastServicedDay; // Assume 1..31
int lastServicedYear; // Assume 1900..2050
int downDays;
int purchaseDateMonth; // Assume 1..12
int purchaseDateDay; // Assume 1..31
int purchaseDateYear; // Assume 1900..2050
float cost;
}; |
|
|
|
|
|
|
|
|
Type MachineRec has 11 members. There is so much detailed information here that it is difficult to quickly get a feeling for what the record represents. Let's see if we can reorganize it into a hierarchical structure that makes more sense. We can divide the information into two groups: information that changes and information that does not. There are also two dates to be kept: date of purchase and date of last service. These observations suggest use of a record describing a date, a record describing the statistical data, and an overall record containing the other two as components. The following type declaration reflects this structure. |
|
|
|
|
|
|
|
|
const int NUM_MACHINES = 25;
typedef char String50[51];
|
|
|
|
|
|