|
|
|
|
|
|
|
Boolean Consistent( /* in */ StudentRec aStudent )
// Precondition:
// 0.0 <= aStudent.gpa <= 4.0
// Postcondition:
// Function value == TRUE, if the course grade is consistent
// with the overall GPA
// == FALSE, otherwise
{
int roundedGPA = int(aStudent.gpa + 0.5);
switch (roundedGPA)
{
case 0: return (aStudent.courseGrade == F);
case 1: return (aStudent.courseGrade == D);
case 2: return (aStudent.courseGrade == C);
case 3: return (aStudent.courseGrade == B);
case 4: return (aStudent.courseGrade == A);
}
} |
|
|
|
|
|
|
|
|
Let's review the basic features of struct data types in the context of another example. A parts wholesaler wants to computerize her operation. Until now, she has kept the inventory on handwritten 8 × 10 cards. A typical inventory card contains the following data:
|
|
|
|
|
|
|
|
|
Part number: 1A3321
Description: Cotter pin
Cost: 0.012
Quantity on hand: 2100 |
|
|
|
|
|
|
|
|
A struct is a natural choice for describing a part. Each item on the inventory card can be a member of the struct. The relevant declarations look like this: |
|
|
|
|
|
|
|
|
typedef char String6[7];
typedef char String20[21];
struct PartType
{
String6 partNumber;
String20 description;
float cost;
int quantity;
};
PartType part; |
|
|
|
|
|