8-數據類型_處理5個學生的數據

數據結構練習:
  • 構造一個學生類型 : 姓名, 年齡, 學號, 成績
  • 處理5個學生的數據;
  • 循環輸入學生信息;
  • 找出成績最高的學生, 並且打印其信息(注意:不要排序).
  • 按照成績排序, 並且打印其信息.
#include <stdio.h>

typedef struct student
{
    char name[128];
    int age;
    int id;
    int score;
}stu_t;
void input_info(stu_t *p,int n);
stu_t *get_max_info(stu_t *p, int n);
void max_min(stu_t *p, int n);
void print_info(stu_t *p, int n);
int main(int argc, const char *argv[])
{
    int i;
    stu_t *max;
    stu_t stu[5];
    input_info(stu, 5);
    max = get_max_info(stu,5);
    for(i = 0; i < 5; i++)
    {
        if(max->score == stu[i].score)
        {
            printf("最高成績爲: NAME:%s  AGE:%d  ID:%d  SCORE:%d\n",stu[i].name, stu[i].age, stu[i].id, stu[i].score);
        }
    }

    max_min(stu, 5);
    print_info(stu,5);

    return 0;
}
void input_info(stu_t *p,int n)
{
    int i;

    for(i = 0; i < n; i++)
    {
        printf("NAME\tAGE\tID\tSCORE\n");
        scanf("%s%d%d%d",p[i].name, &p[i].age,&p[i].id, &p[i].score);
    }
}

stu_t *get_max_info(stu_t *p, int n)
{
    int i;
    stu_t *hs = p;
    for(i = 0; i < n; i++)
    {
        if(hs->score < p[i].score)
            hs = &p[i];
    }
    return hs;
}

void max_min(stu_t *p, int n)
{
    int i, j;
    stu_t temp;

    for(i = 0; i < n-1; i++)
    {
        for(j = 0; j < n-1-i; j++)
        {
            if(p[j].score < p[j+1].score)
            {
                temp = p[j];
                p[j] = p[j+1];
                p[j+1] = temp;
            }
        }
    }
}


void print_info(stu_t *p, int n)
{
    int i;

    printf("*********************************\n");
    for(i = 0; i < n; i++)
    {

        printf("NAME:%s  AGE:%d  ID:%d  SCORE:%d\n",p[i].name, p[i].age, p[i].id, p[i].score);
    }
    printf("*********************************\n");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章