C language programming exercise reference answer Chapter 8 Document (2)

Source: Internet
Author: User

/* 8.8retrieve the score from the file ex88_1.txt. Sort the score and store the score in the descending order */
# Include "stdio. H"
# Define N 10
Struct student
{
Int num;
Char name [20];
Int score [3];/* float cannot be used */
Float average;
};

Void sort (struct student STU []);
Void print (File * FP, struct student STU []);
Void printtopfive (File * FP, struct student STU []);

void main ()
{< br> struct student s [N];
int I;
file * FP1, * fp2;
char ch;
If (FP1 = fopen ("C: \ ex88_1.txt", "R") = NULL)
{< br> printf ("file c: \ ex88_1.txt" cannot be opened);
exit (1 );
}< br> If (fp2 = fopen ("C: \ ex88_2.txt", "W") = NULL)
{< br> printf ("file c: \ ex88_2.txt" cannot be opened);
exit (1);
}

for (I = 0; I {< br> fscanf (FP1, "% d % S % d", & S [I]. num, s [I]. name, & S [I]. score [0],
& S [I]. score [1], & S [I]. score [2]);
S [I]. average = (s [I]. score [0] + s [I]. score [1] + s [I]. score [2])/3.0;
}< br> fprintf (fp2, "Original Score report \ n");
Print (fp2, S );
sort (s);
fprintf (fp2, "sorted score report \ n");
Print (fp2, S );
fprintf (fp2, "Top Five score reports \ n");
printtopfive (fp2, S);
}

Void sort (struct student STU [])
{
Int I, K, J;
Struct Student t;
For (I = 0; I <N-1; I ++)
{
K = I;
For (j = I + 1; j <n; j ++)
{
If (STU [K]. Average <STU [J]. Average)
K = J;
If (K! = I)
{
T = STU [I];
STU [I] = STU [k];
STU [k] = T;
}
}

}
}

Void print (File * FP, struct student STU [])
{
Int I;
Fprintf (FP, "studentid studentname score1 score2 score3 average \ n ");
For (I = 0; I <n; I ++)
Fprintf (FP, "%-10D %-12 S % 8d % 8d % 8d % 8.1f \ n", STU [I]. Num, STU [I]. Name,
STU [I]. Score [0], STU [I]. Score [1], STU [I]. Score [2], STU [I]. Average );
}

Void printtopfive (File * FP, struct student STU [])
{
Int I;
Fprintf (FP, "studentname average \ n ");
For (I = 0; I <5; I ++)
Fprintf (FP, "%-12 S % 8.1f \ n", STU [I]. Name, STU [I]. Average );
}

/* Obtain the score from ex88_1.txt?

20001 name (1) 70 80 90
20002 name (2) 71 81 91
20003 name (3) 72 82 92
20004 name (4) 73 83 93
20005 name (5) 74 84 94
20006 name (6) 75 85 95
20007 name (7) 76 86 96
20008 name (8) 77 87 97
20009 name (9) 78 88 98
20010 name (10) 79 89 99
*/

/* After sorting, store ex88_2.txt in descending order

Original Score report
Studentid studentname score1 score2 score3 average
20001 name (1) 70 80 90 80.0
20002 name (2) 71 81 91 81.0
20003 name (3) 72 82 92 82.0
20004 name (4) 73 83 93 83.0
20005 name (5) 74 84 94 84.0
20006 name (6) 75 85 95 85.0
20007 name (7) 76 86 96 86.0
20008 name (8) 77 87 97 87.0
20009 name (9) 78 88 98 88.0
20010 name (10) 79 89 99 89.0
Sorted score report
Studentid studentname score1 score2 score3 average
20010 name (10) 79 89 99 89.0
20009 name (9) 78 88 98 88.0
20008 name (8) 77 87 97 87.0
20007 name (7) 76 86 96 86.0
20006 name (6) 75 85 95 85.0
20005 name (5) 74 84 94 84.0

*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.