# include & ltstring.h & gt
const int MAX _ NUM = 5;
const double MAX _ NUMS = 5.0;
const int MAX _ NAME _ LEN = 127;
const int MAX _ SUBJECT = 3;
const char *g_strSubjects[] =
{
"China people",
"Mathematics",
"English",
"illegal"
};
Typedef structure _ student
{
char SZ NAME[MAX _ NAME _ LEN+ 1];
int nArryScore[MAX _ SUBJECT];
} students;
Student student [max _ num];
Static int read_line (FILE *fp, char *line, int max_length)
{
int I;
char ch
/* Initialize the index as a string character */
I = 0;
/* Read the end of the line and fill in the characters in the string until it
Maximum length, ignoring the rest, if any */
for(; ; )
{
/* Read the next character */
ch = fgetc(FP);
/* Check the error at the end of the file */
if ( ch == EOF)
return- 1;
/* Check the end of the line */
if ( ch == '\n ')
{
/* Terminates the string and returns */
line[I]= ' \ 0 ';
Returns 0;
}
/* If the characters in the string are not filled, please fill them */
If (I < Maximum Length)
line[i++]= ch;
}
/* The program should not arrive here */
return- 1;
}
void menu_add_student()
{
Student * pStudent = NULL
int nCnt = 0;
int nPos = 0;
while(MAX _ NUM & gt; nCnt)
{
Printf ("record of the %d student:", NCNT+1);
Student = & student [NCNT];
scanf("%s ",p student-& gt; SZ name);
for(nPos = 0; nPos & lt。 MAX _ SUBJECTnPos++)
{
Scanf("%d ",& ampp student-> nArryScore[nPos]);
}
ncnt++;
getchar();
}
}
void query_student(int n)
{
Student * p student = & student [n];
printf("%s ",p student->; SZ name);
int nPos = 0;
for(nPos = 0; nPos & lt。 MAX _ SUBJECTnPos++)
{
printf("%d ",p student-& gt; nArryScore[nPos]);
}
printf(" \ n ");
}
void menu_query_detail()
{
int n = 0;
scanf("%d ",& ampn);
getchar();
int nPos = 0;
for(nPos = 0; nPos & lt。 MAX _ SUBJECTnPos++)
{
printf("%s ",g _ str subjects[nPos]);
}
printf(" \ n ");
Query _ student (n-1);
}
void menu_query_all()
{
int nPos = 0;
for(nPos = 0; nPos & lt。 MAX _ SUBJECTnPos++)
{
printf("%s ",g _ str subjects[nPos]);
}
printf(" \ n ");
int n = 0;
for(n = 0; n & ltMAX _ NUMn++)
{
query _ student(n);
}
}
Void Menu _ Theme _ Average ()
{
Student * pStudent = NULL
int n3 score[MAX _ SUBJECT]= { 0 };
int nCnt = 0;
int nPos = 0;
while(MAX _ NUM & gt; nCnt)
{
Student = & student [NCNT];
for(nPos = 0; nPos & lt。 MAX _ SUBJECTnPos++)
{
if(n3 score[nPos]& lt; P student-> nArryScore[nPos])
{
n3 score[nPos]+= p student-& gt; nArryScore[nPos];
}
}
ncnt++;
}
for(nPos = 0; nPos & lt。 MAX _ SUBJECTnPos++)
{
Printf("%s average score (%.2f) \ n ",g _ str subjects [npos], n3 score [npos]/max _ nums);
}
}
void menu_subject_max()
{
Student * pStudent = NULL
int n3Cnt[MAX _ SUBJECT]= { 0 };
int n3 score[MAX _ SUBJECT]= { 0 };
int nCnt = 0;
int nPos = 0;
while(MAX _ NUM & gt; nCnt)
{
Student = & student [NCNT];
for(nPos = 0; nPos & lt。 MAX _ SUBJECTnPos++)
{
if(n3 score[nPos]& lt; P student-> nArryScore[nPos])
{
n3 score[nPos]= p student-& gt; nArryScore[nPos];
n3Cnt[nPos]= nCnt+ 1;
}
}
ncnt++;
}
for(nPos = 0; nPos & lt。 MAX _ SUBJECTnPos++)
{
Printf("%s highest score (%d), serial number (%d) \ n ",g _ str subjects [npos], N3score [npos], N3cnt [npos]);
}
}
/* menu code */
# define ADD_CODE 0
# Define print code 1
# Definition Details _ Code 2
# define SUB_MAX_CODE 3
# Define SUB _ AVE _ code4
# Define exit code 5
int main (void)
{
for(; ; )
{
Int selection, result;
Char line [301];
// FILE *fp 1=fopen("input2 "," r ");
/* Print menu to standard error */
Fprintf (stderr, "\nOption: \ n");
Fprintf (stderr, "%d: enter grades \n", add _ code);
Fprintf (stderr, "%d: output the grade of the whole class \n", print _ code);
Fprintf (stderr, "%d: enter the serial number and output the corresponding students' scores of three courses \n", detail _ code);
Fprintf (stderr, "%d: output the highest score of each course and the serial number of the corresponding student \n", sub _ max _ code);
Fprintf (stderr, "%d: output the average score of each course \n", sub _ ave _ code);
Fprintf (stderr, "%d: exit system \n", exit _ code);
Fprintf (stderr, "\ nInput option:");
if ( read_line ( stdin,line,300)! = 0) Continue;
// read_line (fp 1,line,300);
//printf("\n - %s - \n ",line);
Result = sscanf (line, "%d", & select);
If (the result! = 1 )
{
Fprintf (stderr, "Bad menu option \ n");
Continue;
}
Switch (selection)
{
Case Add _ Code:/* Add books to the database */
menu _ add _ student();
Break;
Caseproint _ code:/* Get book details from the database */
menu _ query _ all();
Break;
Case Details _ Code:/* Delete books from the database */
menu_query_detail()。
Break;
Case SUB_MAX_CODE: /* Print the database contents to the screen.
(standard output) */
menu _ subject _ max();
Break;
Case SUB_AVE_CODE: /* Print the tree to the screen (standard output) */
menu_subject_average()。
Break;
/* Exit */
Case Exit _ Code:
Break;
Default value:
Fprintf (stderr, "Illegal selection of %d\n", choice);
Break;
}
/* Check the exit menu option */
If (select = = exit code)
Break;
}
Returns 0;
}