Use "Structure" to count student scores

Source: Internet
Author: User

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-&GT;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

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.