Students ' information including student number, name and five lessons, required to write input and output
The function of student information. After entering a group of student information, the score from the student's score is higher
Output student information to a low order.
This is relatively simple, can create a student's structure, the structure of the body has three members: Student number, name and five subjects of the results. Take a look at my overall code below. This idea is also relatively simple, mainly in order to practice the use of structural bodies.
#include <stdio.h>#include <stdlib.h>#include <string.h>#define N#define SCORES 5#define Numlen/** * Students ' information including student number, name and score of five subjects is required to write the function of input/output * Student information. After entering a group of student information, students ' scores are exported from high to low in order to output student information. */structstudent{CharNumber[numlen];/ * Study number * / Char*name;/ * Student name * / intScores[scores];/ * Results of five lessons for students * /};structStudent Stus[n];intOrder[n];intTotal[n];intReadstu (structStudent *stu) {intLen,j;Charbuf[ -];//Buffer printf("Number:");if(scanf('%s ', buf) = =1){strncpy(stu->number,buf,numlen-1); }Else{return 0;//ctrl+z Exit}printf("Name:");if(scanf('%s ', buf) = =1) {len =strlen(BUF); Stu->name = (Char*)malloc(len+1);strcpy(STU->NAME,BUF); }Else{return 0; }printf("Scores:"); for(j =0; J < scores;j++)if(scanf("%d", stu->scores+j)! =1) Break;if(J = =0){//A score is not entered, emptying the memory occupied by name Free(Stu->name);return 0; } for(; j<scores;j++) stu->scores[j] =0;return 1;}/ * Output Student information * /voidWritestu (structStudent *stu) {intIprintf("Number:%s\n", Stu->number);printf("Name:%s\n", stu->name);printf("Scores:"); for(i =0; i < scores;i++)printf("%4D", Stu->scores[i]);printf("\ n");}intMain () {intN,i,j,t;//Get student information for(n =0; Readstu (Stus+n); n++); for(i =0; i < n;i++) {Order[i] = i; for(t =0, j =0; J < scores;j++) {T + = stus[i].scores[j]; } Total[i] = t; }//bubble sort Output for(i =0; i < n;i++) { for(j =0; J < N-1; j + +) {if(Total[order[j]] < total[order[j+1]]){//Exchanget = order[j]; ORDER[J] = order[j+1]; order[j+1] = t; } } } for(j =0; J <n;j++) {Writestu (stus+order[j]); }return 0;}
Here is the result of my output:
Use "Structure" to count student scores