A single structural body type variable is not useful in solving practical problems, usually in the form of an array of struct types
Is. The structure body type array is defined in the form:
struct STU/* Define student structure type */
{
Char name[20]; /* Student Name */
char sex; /* Gender */
Long num; /* School Number */
float score[3]; /* Test results for three subjects */
};
struct Stu stud[20]; Fixed/* Semantic structure body type array stud, * *
/* The array has 2 0 structural body type elements */
The members of the array element are referenced in the form:
Stud[0].name, Stud[0].sex, Stud[0].score[i];
Stud[1].name, Stud[1].sex, Stud[1].score[i];
...
...
Stud[19].name, Stud[19].sex, Stud[19].score[i];
[Example 7-1] set a group of 4 persons, fill in the registration form below, in addition to name, learn extra, there are three subjects, programming to calculate the table, to solve each of the three subjects average results, to find out the average of four students, and according to the average score from high to low output.
The problem requires many problems, using modular programming, the problem is decomposed as follows:
1 The input of the array of the structure body type.
2) to solve the students ' three-section average score.
3) According to the student's average score ranking.
4) According to the table requirements output.
5 to solve the students in the group average score and output.
6 define M a i n () function, call each subroutine.
The first step is to define the structure type according to the situation.
struct STU
{
Char name[20]; * * Name */
Long number; /* School Number */
float score[4]; /* Array to store e n g l i s H, m a t h e m A, P H y S i c s, and a v e r a G e */
} ;
Because the struct type is used for each subroutine, it is shared, so it is defined as an external structure
Type, placed at the front of the program.
The second step is to define an input module for the array of struct types.
void input (ARR,N)/* Input structure body type an array of n elements of R R
struct Stu arr[];
int n;
{int i,j;
Char temp[30];
for (i=0;i<n;i++)
{
printf ("\ninput name,number,english,mathema,physic\n"); /* PRINT hint info */
Gets (Arr[i].name); /The name of the input * *
Gets (temp); /* Admission Number * *
A r R [i]. n u m b e r = a t o L (t e M P);
F o r (j = 0; J < 3; J + +)
{
Gets (temp); /* Enter three subjects */
A r R [i]. S c o r e [j] = a t o I (t e M P);
} ;
}
}
The third step is to solve the students ' average scores in three subjects.
The first three elements of the member S C o r E of the element a r R [i] in the array of struct types are known, and the Fourth AV e r a G e needs
Calculated.
void Aver (Arr,n)
struct Stu arr[];
int n;
{
int i,j;
for (i=0;i<n;i++)
{
A r R [i]. S c o r e [3] = 0;
F o r (j = 0; J < 3; J + +)
ARR[I].SCORE[3]=ARR[I].SCORE[3]+ARR[I].SCORE[J]; sum/*/
ARR[I].SCORE[3]=ARR[I].SCORE[3]/3; Flat/all * achievement * *
}
}