# include <stdio.h>
# include <malloc.h>

struct Student
{
    int age;
    float score;
    char name[100];
};

void InputArr(struct Student * Arr, int len);
void SortArr(struct Student * Arr, int len);
void OutputArr(struct Student * pArr, int len);

int main(void)
{
    int len;
    struct Student * pArr;

    //动态的构造一维数组
    printf("请输入学生的个数:\n");
    printf("len = ");
    scanf("%d", &len);
    pArr = (struct Student *)malloc(len * sizeof(struct Student));

    InputArr(pArr, len);  //输入
    SortArr(pArr, len);  //排序
    OutputArr(pArr, len);  //输出

    return 0;
}

void InputArr(struct Student * Arr, int len)
{
    int i;
    for (i = 0; i < len; ++i)
    {
        printf("请输入第%d个学生的信息\n", i + 1);
        printf("age =");
        scanf("%d", &Arr[i].age);
        printf("score =");
        scanf("%f", &Arr[i].score);
        printf("name =");
        scanf("%s", Arr[i].name);

    }
}


void SortArr(struct Student * Arr, int len)
{
    int i, j;
    struct Student t;

    for (i = 0; i < len - 1; ++i)
    {
        for (j = 0; j < len - 1 - i; ++j)
        {
            if (Arr[j].score > Arr[j + 1].score)
            {
                t = Arr[j];
                Arr[j] = Arr[j + 1];
                Arr[j + 1] = t;
            }
        }
    }
    
}


void OutputArr(struct Student * Arr, int len)
{
    int i;
    for (i = 0; i < len; ++i)
    {
        printf("\n\n");
        printf("第%d个学生的信息是\n", i + 1);
        printf("age = %d\n", Arr[i].age);
        printf("score = %.1f\n", Arr[i].score);
        printf("name = %s\n", Arr[i].name);
    }
}

标签: none

仅有一条评论

  1. nice

添加新评论