8-数据类型_处理5个学生的数据

2017-02-03  本文已影响0人  ibo
数据结构练习:
#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");
}

上一篇下一篇

猜你喜欢

热点阅读